Skip to content
Closed
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):
for startposition in range(sublistcount):
gapInsertionSort(test,startposition,sublistcount)
print("After increments of size",sublistcount,"The list is",test)
sublistcount = sublistcount // 2

def gapInsertionSort(test,start,gap):
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]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

document the code

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I'll do that!

position = position-gap
test[position]=currentvalue

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