Skip to content

Commit d7b49a0

Browse files
committed
Shell Sort added
1 parent 9da7f2e commit d7b49a0

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#Author : Robin Singh
2+
#Shell sort is an algorithm that first sorts the elements far apart from each other and successively reduces
3+
#the interval between the elements to be sorted. It is a generalized version of insertion sort
4+
#In shell sort, elements at a specific interval are sorted. The interval between the elements is gradually decreased
5+
#based on the sequence used. The performance of the shell sort depends on the type of sequence used for a given input array
6+
#We can use different sequences methods
7+
#Shell's original sequence: N/2 , N/4 , …, 1
8+
#Knuth's increments: 1, 4, 13, …, (3k – 1) / 2
9+
#Sedgewick's increments: 1, 8, 23, 77, 281, 1073, 4193, 16577...4j+1+ 3·2j+ 1
10+
11+
#Complexity : Best Case = Average Case = O(nLogn)
12+
# Worst Case = O(n*n)
13+
14+
15+
16+
17+
18+
def shellsort(a,n):
19+
20+
g = n // 2
21+
while g > 0:
22+
for i in range(g, n):
23+
t = a[i]
24+
j = i
25+
while j >= g and a[j - g] > t:
26+
a[j] = a[j - g]
27+
j -= g
28+
29+
a[j] = t
30+
g =g// 2
31+
32+
if __name__ == '__main__':
33+
a = [8,76,65,5,8,95,3,2,1,0]
34+
shellsort(a,len(a))
35+
print(a)

0 commit comments

Comments
 (0)