Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Competitive Coding/Sorting/Shell_Sort/shellsort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
The algorithm uses a procedure called Shell Sort
to sort the lists. It breaks the list to smaller sublists
and each sublist is further sorted by insertion sort.
The sublists are made using a gap and elements at gap i
is often selected and forms a sublist.
"""

def shellSort(test):
sublistcount = len(test)//2 #Finds gap to create sublists
while(sublistcount > 0): #Condition runs until there's a gap
for startposition in range(sublistcount):
gapInsertionSort(test,startposition,sublistcount)
print("After increments of size",sublistcount,"The list is",test)
sublistcount = sublistcount // 2 #value os sublistcount changes after every iteration

def gapInsertionSort(test,start,gap): #Function which sorts the sublists
for i in range(start+gap,len(test),gap):
currentvalue = test[i]
position = i
while(position>=gap and test[position-gap]>currentvalue):
test[position]=test[position-gap] #changes in values occurs here due to sorting
position = position-gap
test[position]=currentvalue

if __name__=="__main__":
test = [99,109,29,89,44,98,20,55,31]
shellSort(test)
print test