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
 	gap = n // 2
29
 	gap = n // 2
30
 
30
 
31
 	# comienza el algoritmo
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
 		gap = gap // 2 
52
 		gap = gap // 2 
47
 
53
 
48
 	return lista
54
 	return lista
49
 
55
 
50
 maxValor=1000 	#define el valor maximo de los elementos de la lista
56
 maxValor=1000 	#define el valor maximo de los elementos de la lista
51
 largoLista=1000 #define el largo de las listas a ordenar
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
 acumulaMerge=0 	#variable para acumular el tiempo de ejecucion del mergesort
60
 acumulaMerge=0 	#variable para acumular el tiempo de ejecucion del mergesort
55
 acumulaHeap=0 	#variable para acumular el tiempo de ejecucion del heapsort
61
 acumulaHeap=0 	#variable para acumular el tiempo de ejecucion del heapsort
65
 	t1 = time.process_time()					#tomamos el tiempo inicial
71
 	t1 = time.process_time()					#tomamos el tiempo inicial
66
 	mergeSort(mergelista,0,len(mergelista)-1) 	#ejecutamos el algoritmo mergeSort
72
 	mergeSort(mergelista,0,len(mergelista)-1) 	#ejecutamos el algoritmo mergeSort
67
 	acumulaMerge+=time.process_time() - t1		#acumulamos el tiempo de ejecucion
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
 	t1 = time.process_time()				#tomamos el tiempo inicial
76
 	t1 = time.process_time()				#tomamos el tiempo inicial
71
 	heapSort(heaplista)					    #ejecutamos el algoritmo heapSort
77
 	heapSort(heaplista)					    #ejecutamos el algoritmo heapSort
72
 	acumulaHeap+=time.process_time() - t1 	#acumulamos el tiempo de ejecucion
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
 	t1 = time.process_time()				#tomamos el tiempo inicial
81
 	t1 = time.process_time()				#tomamos el tiempo inicial
76
 	quickSort(quicklista)					#ejecutamos el algoritmo quickSort
82
 	quickSort(quicklista)					#ejecutamos el algoritmo quickSort
77
 	acumulaQuick+=time.process_time() - t1	#acumulamos el tiempo de ejecucion
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
 	t1 = time.process_time()				#tomamos el tiempo inicial
86
 	t1 = time.process_time()				#tomamos el tiempo inicial
81
 	shellSort(searchlista)					#ejecutamos el algoritmo shellSort
87
 	shellSort(searchlista)					#ejecutamos el algoritmo shellSort