From 143302129a4bf0065d4329fec1ee28ff0e85adbb Mon Sep 17 00:00:00 2001 From: Sunny Pate Date: Sat, 13 Oct 2018 13:37:58 +1100 Subject: [PATCH] shell sort --- sorting/shellSort.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 sorting/shellSort.py diff --git a/sorting/shellSort.py b/sorting/shellSort.py new file mode 100644 index 0000000..f0d38e5 --- /dev/null +++ b/sorting/shellSort.py @@ -0,0 +1,28 @@ + +# coding: utf-8 + +# In[2]: + + +alist = [54,26,93,17,77,31,44,55,20] +def shellSort(alist): + sublistcount = len(alist)//2 + while sublistcount > 0: + for startposition in range(sublistcount): + gapInsertionSort(alist,startposition,sublistcount) + sublistcount = sublistcount // 2 + return alist + +def gapInsertionSort(alist,start,gap): + for i in range(start+gap,len(alist),gap): + + currentvalue = alist[i] + position = i + + while position>=gap and alist[position-gap]>currentvalue: + alist[position]=alist[position-gap] + position = position-gap + + alist[position]=currentvalue +print(shellSort(alist)) +