Browse Source

Merge branch 'sortingShell' into sortingFinal

Orlando04 2 years ago
parent
commit
d0d076a38b
1 changed files with 37 additions and 7 deletions
  1. 37
    7
      sorting.py

+ 37
- 7
sorting.py View File

@@ -109,13 +109,43 @@ def quickSort(lista):
109 109
 	lista = left + [lista[current_position]] + right #Merging everything together
110 110
 	return lista
111 111
 
112
+''' 
113
+	This algorithm was taken from: https://www.programiz.com/dsa/shell-sort 
114
+	and was adapted in order to work for this assigment.
115
+'''
116
+
117
+''' 
118
+	This algorithm was taken from: https://www.programiz.com/dsa/shell-sort 
119
+	and was adapted in order to work for this assigment.
120
+'''
121
+
112 122
 def shellSort(lista):
113
-		#definan el algoritmo de ordenamiento shellsort
114
-		return lista
123
+	#definan el algoritmo de ordenamiento shellsort
124
+
125
+	# determening the size of the list and calculates the gap value. 
126
+	n = len(lista)
127
+	gap = n // 2
128
+
129
+	# this algorithm will run until gap reaches 1 
130
+	while gap > 0:
131
+		for i in range(gap, n):
132
+			temp = lista[i] # storing all items from the list into temp 
133
+			j = i 
134
+
135
+			# compares the number in temp with the 0th possition (start of list)
136
+			# if temp is larger than the first element then we swap them
137
+			while j >= gap and lista[j - gap] > temp: 
138
+				lista[j] = lista[j - gap]
139
+				j -= gap
140
+
141
+			lista[j] = temp
142
+		gap = gap // 2 # decreases the gap to continue the loop 
143
+
144
+	return lista
115 145
 
116 146
 maxValor=1000 	#define el valor maximo de los elementos de la lista
117
-largoLista=1000 	#define el largo de las listas a ordenar
118
-veces=100    		#define las veces que se va a hacer el ordenamiento
147
+largoLista=1000 #define el largo de las listas a ordenar
148
+veces=100 		#define las veces que se va a hacer el ordenamiento 
119 149
 
120 150
 acumulaMerge=0 	#variable para acumular el tiempo de ejecucion del mergesort
121 151
 acumulaHeap=0 	#variable para acumular el tiempo de ejecucion del heapsort
@@ -131,17 +161,17 @@ for i in range(veces):
131 161
 	t1 = time.process_time()					#tomamos el tiempo inicial
132 162
 	mergeSort(mergelista,0,len(mergelista)-1) 	#ejecutamos el algoritmo mergeSort
133 163
 	acumulaMerge+=time.process_time() - t1		#acumulamos el tiempo de ejecucion
134
-	print(mergelista)							#desplegamos la lista
164
+	#print(mergelista)							#desplegamos la lista
135 165
 
136 166
 	t1 = time.process_time()				#tomamos el tiempo inicial
137 167
 	heapSort(heaplista)					    #ejecutamos el algoritmo heapSort
138 168
 	acumulaHeap+=time.process_time() - t1 	#acumulamos el tiempo de ejecucion
139
-	print(heaplista)						#desplegamos la lista
169
+	#print(heaplista)						#desplegamos la lista
140 170
 
141 171
 	t1 = time.process_time()				#tomamos el tiempo inicial
142 172
 	quickresult = quickSort(quicklista)					#ejecutamos el algoritmo quickSort
143 173
 	acumulaQuick+=time.process_time() - t1	#acumulamos el tiempo de ejecucion
144
-	print(quickresult)						#desplegamos la lista
174
+	print(quicklista)						#desplegamos la lista
145 175
 
146 176
 	t1 = time.process_time()				#tomamos el tiempo inicial
147 177
 	shellSort(searchlista)					#ejecutamos el algoritmo shellSort