Selaa lähdekoodia

Merge branch 'ShellSort' into sortingFinal

Merge ShellSort
Alex Ortiz 2 vuotta sitten
vanhempi
commit
213c5676e6
1 muutettua tiedostoa jossa 22 lisäystä ja 0 poistoa
  1. 22
    0
      sorting.py

+ 22
- 0
sorting.py Näytä tiedosto

@@ -72,7 +72,29 @@ def quickSort(lista):
72 72
 
73 73
 def shellSort(lista):
74 74
 	#definan el algoritmo de ordenamiento shellsort
75
+	Size = len(lista) # Contains the complete size of the list
76
+	Diff = Size/2 # Contains the number of half of the list
77
+
78
+	# Does a insertion sort by ordering in base of the Diff.
79
+	while Diff > 0:
80
+		# Begins a loop to sort the elements added to sorted array.
81
+		for i in range(Diff, Size):
82
+			# Saves the element sorted in a temporary variable
83
+			Tmp = lista[i]
84
+			j = i # Shifts the location of the elements
85
+			# Looks for the locations
86
+			while j >= Diff and lista[j - Diff] > Tmp:
87
+				# Gives the new element to the location
88
+				lista[j] = lista[j - Diff]
89
+				# The size of the array is ajusted
90
+				j -= Diff
91
+			# Puts the Tmp variable in is correct location
92
+			lista[j] = Tmp
93
+		# Reduces again the list
94
+		Diff /= 2
95
+
75 96
 	return lista
97
+	# Code was taken from GeeksforGeeks
76 98
 
77 99
 maxValor=1000 	#define el valor maximo de los elementos de la lista
78 100
 largoLista=1000 #define el largo de las listas a ordenar