소스 검색

Merge branch 'Jantony-shellSort' into sortingFinal

부모
커밋
3129ed1a64
1개의 변경된 파일27개의 추가작업 그리고 3개의 파일을 삭제
  1. 27
    3
      sorting.py

+ 27
- 3
sorting.py 파일 보기

@@ -76,9 +76,33 @@ def quickSort(lista):
76 76
       			elif(l == None):
77 77
         			return lista[lista.index(p):lista.index(p) + 1] + m
78 78
 
79
-def shellSort(lista):
80
-	#definan el algoritmo de ordenamiento shellsort
81
-	return lista
79
+# inplace
80
+# complexity: O(N^2)
81
+def shellSort(lst):
82
+    # initial gap
83
+    gap = len(lst)
84
+
85
+    while 0 < gap:
86
+        # sort every sublist with given gap
87
+        for start in range(gap):
88
+            f = range(start, len(lst), gap)
89
+            s = range(start + gap, len(lst), gap)
90
+
91
+            # bubble sort on sublist
92
+            swapped = True
93
+            while swapped:
94
+                swapped = False
95
+
96
+                # iterate through every adjacent pair in sublist
97
+                for c, n in zip(f, s):
98
+                    if lst[n] < lst[c]:
99
+                        lst[c], lst[n] = lst[n], lst[c]
100
+                        swapped = True
101
+
102
+        # reduce gap towards 0
103
+        gap = gap // 2
104
+
105
+    return lst
82 106
 
83 107
 # timeCode function/thunk -> (duration, return value)
84 108
 # measures the time it takes for a function/thunk to return