Quellcode durchsuchen

Updated the algorithm

Made changes to the algorithm so that it can run better.
Orlando04 vor 2 Jahren
Ursprung
Commit
14ce8f0145
1 geänderte Dateien mit 16 neuen und 21 gelöschten Zeilen
  1. 16
    21
      sorting.py

+ 16
- 21
sorting.py Datei anzeigen

@@ -23,33 +23,28 @@ def quickSort(lista):
23 23
 
24 24
 def shellSort(lista):
25 25
 	#definan el algoritmo de ordenamiento shellsort
26
+	# este algoritmo fue tomado de https://www.programiz.com/dsa/shell-sort 
27
+	# y adaptado para que funcione en este programa.
28
+	 
26 29
 	
27 30
 	# determinamos el tamaño de la lista para poder buscar el numero gap. 
28 31
 	n = len(lista)
29 32
 	gap = n // 2
30 33
 
31
-	# comienza el algoritmo
34
+	# this algorithm will run until gap reaches 1 
32 35
 	while gap > 0:
33
-		j = gap
34
-
35
-		while j < n: 
36
-			i = j - gap 
37
-			
38
-			while i >= 0: 
39
-				if lista[i+gap] > lista[i]:
40
-					break
41
-                
42
-				else:
43
-					lista[i+gap], lista[i] = lista[i], lista[i+gap]
44
-					
45
-				i=i-gap # To check left side also
46
-                            # If the element present is greater than current element 
47
-							
48
-			
49
-			j+=1
50
-			
51
-		# se disminuye el gap
52
-		gap = gap // 2 
36
+		for i in range(gap, n):
37
+			temp = lista[i] # storing all items from lista into temp 
38
+			j = i 
39
+
40
+			# compares the number in temp with the 0th possition (start of list)
41
+			# if temp is larger than the first element then we swap them
42
+			while j >= gap and lista[j - gap] > temp: 
43
+				lista[j] = lista[j - gap]
44
+				j -= gap
45
+
46
+			lista[j] = temp
47
+		gap = gap // 2 # decreases the gap to continue the loop 
53 48
 
54 49
 	return lista
55 50