Browse Source

Merge branch 'sortingFinal' of https://git.ccom.uprrp.edu/CCOM4030/AnCyDyJa into sortingFinal

Adding HeapSort to sortingFinal
dylan.cedres 2 years ago
parent
commit
7eb6233c87
1 changed files with 28 additions and 3 deletions
  1. 28
    3
      sorting.py

+ 28
- 3
sorting.py View File

@@ -61,9 +61,34 @@ def quickSort(lista):
61 61
 	#definan el algoritmo de ordenamiento quicksort
62 62
 	return lista
63 63
 
64
-def shellSort(lista):
65
-	#definan el algoritmo de ordenamiento shellsort
66
-	return lista
64
+# inplace
65
+# complexity: O(N^2)
66
+def shellSort(lst):
67
+    # initial gap
68
+    gap = len(lst)
69
+
70
+    while 0 < gap:
71
+        # sort every sublist with given gap
72
+        for start in range(gap):
73
+            f = range(start, len(lst), gap)
74
+            s = range(start + gap, len(lst), gap)
75
+
76
+            # bubble sort on sublist
77
+            swapped = True
78
+            while swapped:
79
+                swapped = False
80
+
81
+                # iterate through every adjacent pair in sublist
82
+                for c, n in zip(f, s):
83
+                    if lst[n] < lst[c]:
84
+                        lst[c], lst[n] = lst[n], lst[c]
85
+                        swapped = True
86
+
87
+        # reduce gap towards 0
88
+        gap = gap // 2
89
+
90
+    return lst
91
+
67 92
 
68 93
 maxValor=1000 	#define el valor maximo de los elementos de la lista
69 94
 largoLista=1000 #define el largo de las listas a ordenar