Browse Source

Update sorting.py

fixed problem where algorithm would not sort half of the array
Orly J. Mediro 2 years ago
parent
commit
68e46d36ee
1 changed files with 24 additions and 18 deletions
  1. 24
    18
      sorting.py

+ 24
- 18
sorting.py View File

@@ -29,27 +29,33 @@ def shellSort(lista):
29 29
 	gap = n // 2
30 30
 
31 31
 	# comienza el algoritmo
32
-	while gap > 0: 
33
-
34
-		# asignamos el valor de gap a un arreglo temporero
35
-		for i in range(gap,n):
36
-			temp = lista[i]
37
-			j = i
38
-
39
-			# areglando 
40
-			while j >= gap and lista[j-gap] > temp: 
41
-				j -= gap
42
-
43
-			lista[j] = temp
44
-		
45
-		# se disminuye el gap 
32
+	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
46 52
 		gap = gap // 2 
47 53
 
48 54
 	return lista
49 55
 
50 56
 maxValor=1000 	#define el valor maximo de los elementos de la lista
51 57
 largoLista=1000 #define el largo de las listas a ordenar
52
-veces=100 		#define las veces que se va a hacer el ordenamiento 
58
+veces=100		#define las veces que se va a hacer el ordenamiento 
53 59
 
54 60
 acumulaMerge=0 	#variable para acumular el tiempo de ejecucion del mergesort
55 61
 acumulaHeap=0 	#variable para acumular el tiempo de ejecucion del heapsort
@@ -65,17 +71,17 @@ for i in range(veces):
65 71
 	t1 = time.process_time()					#tomamos el tiempo inicial
66 72
 	mergeSort(mergelista,0,len(mergelista)-1) 	#ejecutamos el algoritmo mergeSort
67 73
 	acumulaMerge+=time.process_time() - t1		#acumulamos el tiempo de ejecucion
68
-	print(mergelista)							#desplegamos la lista
74
+	#print(mergelista)							#desplegamos la lista
69 75
 
70 76
 	t1 = time.process_time()				#tomamos el tiempo inicial
71 77
 	heapSort(heaplista)					    #ejecutamos el algoritmo heapSort
72 78
 	acumulaHeap+=time.process_time() - t1 	#acumulamos el tiempo de ejecucion
73
-	print(heaplista)						#desplegamos la lista
79
+	#print(heaplista)						#desplegamos la lista
74 80
 
75 81
 	t1 = time.process_time()				#tomamos el tiempo inicial
76 82
 	quickSort(quicklista)					#ejecutamos el algoritmo quickSort
77 83
 	acumulaQuick+=time.process_time() - t1	#acumulamos el tiempo de ejecucion
78
-	print(quicklista)						#desplegamos la lista
84
+	#print(quicklista)						#desplegamos la lista
79 85
 
80 86
 	t1 = time.process_time()				#tomamos el tiempo inicial
81 87
 	shellSort(searchlista)					#ejecutamos el algoritmo shellSort