Luis Jusino пре 3 година
родитељ
комит
c6569f8eb8
1 измењених фајлова са 105 додато и 24 уклоњено
  1. 105
    24
      sorting.py

+ 105
- 24
sorting.py Прегледај датотеку

@@ -2,11 +2,13 @@
2 2
 
3 3
 """
4 4
 Programadores:
5
-
5
+Carlos J Corrada Bravo
6
+Diego Rodríguez
7
+Joel González
8
+Javier Santiago
9
+Luis Jusino
6 10
 """
7
-
8 11
 """
9
-Carlos J Corrada Bravo
10 12
 Este programa calcula el promedio de tiempo de ejecución de cuatro algoritmos de ordenamiento
11 13
 La variable maxValor define el valor maximo de los elementos de la lista
12 14
 La variable largoLista define el largo de las listas a ordenar
@@ -17,10 +19,69 @@ Al final se imprimen los promedios de cada algortimo
17 19
 from random import randint
18 20
 import time
19 21
 
22
+
23
+#===============================
24
+#Modificación a código: Diego
25
+#Añado función heapify
26
+#===============================
27
+def heapify(listaHeap, largoLista, i):
28
+	largest = i
29
+	left = 2 * i + 1
30
+	right = 2 * i + 2
31
+
32
+	if left < largoLista and listaHeap[i] < listaHeap[left]:
33
+		largest = left
34
+
35
+	if right < largoLista and listaHeap[largest] < listaHeap[right]:
36
+		largest = right
37
+
38
+
39
+def merge(lista, p, q, r):
40
+	left = lista[p:q+1] # copy the left subarray
41
+	right = lista[q+1:r+1] # copy the right subarray
42
+	i = 0 # index for the left subarray
43
+	j = 0 # index for the right subarray
44
+	k = p # index for the sorted list
45
+
46
+	# Keep adding to the sorted list, while both lists have elements
47
+	while i < len(left) and j < len(right):
48
+		if left[i] <= right[j]: 
49
+			lista[k] = left[i]
50
+			i += 1
51
+		else:
52
+			lista[k] = right[j]
53
+			j += 1            
54
+
55
+		k += 1
56
+
57
+	# If right finished first, then fill up the rest with the left subarray
58
+	while i < len(left):
59
+		lista[k] = left[i]
60
+		i += 1
61
+		k += 1
62
+
63
+	# If left finished first, then fill up the rest with the right subarray
64
+	while j < len(right):
65
+		lista[k] = right[j]
66
+		j += 1
67
+		k += 1        
68
+
69
+
70
+def mergeSortAux(lista, p, r):
71
+	# If array has one element or less, return
72
+	if p >= r:
73
+		return 
74
+	# Else, split the array in half
75
+	q = int((p+r)/2) # find the middle
76
+	mergeSortAux(lista, p, q) # Sort the left subarray
77
+	mergeSortAux(lista, q+1, r) # Sort the right subarray
78
+	merge(lista, p, q, r) # Combine both subarrays 
79
+
80
+
20 81
 def mergeSort(listaMerge):
21 82
 	#definan el algoritmo de ordenamiento mergesort
22
-
23
-	return lista
83
+	mergeSortAux(listaMerge, 0, len(listaMerge)-1)
84
+	return listaMerge
24 85
 
25 86
 #===============================
26 87
 #Modificación a código: Diego
@@ -55,31 +116,51 @@ def heapSort(listaHeap):
55 116
 		heapify(listaHeap, i, 0)
56 117
 	return listaHeap
57 118
 
58
-
59 119
 	return lista
60 120
 #Se le hace referencia y se le da credito al codigo que utilice de referencia
61 121
 
122
+	return lista
123
+#Se le da credito al programador de la funcion al final del codigo
124
+
125
+
62 126
 def quickSort(lista):
63
-	#definan el algoritmo de ordenamiento quicksort
64
-        menor = []
65
-        igual = []
66
-        mayor = []
67
-
68
-        if len(lista) > 1:
69
-                pivot = lista[0]
70
-                for i in lista:
71
-                        if i < pivot:
72
-                                menor.append(i)
73
-                        elif i == pivot:
74
-                                igual.append(i)
75
-                        else:
76
-                                mayor.append(i)
77
-                return quickSort(menor)+igual+quickSort(mayor)
78
-        else:
79
-                return lista
127
+	#definan el algoritmo de ordenamiento quicksort                        
128
+	return lista
80 129
 
81 130
 def shellSort(lista):
82
-	#definan el algoritmo de ordenamiento shellsort
131
+	# Subarrays are sorted according to intervals
132
+	# After each set of subarrays is sorted, interval value is updated and process repeats
133
+	# Function stops once iteration with interval = 1 has executed
134
+	# print(lista)
135
+	interval = len(lista) // 2
136
+	while interval > 0:
137
+		# Process repeats for each value between 1 -> interval
138
+		for i in range(0, interval):
139
+			# Starting index determines initial portion of the array that is sorted
140
+			sortedIndex = i
141
+			# Process repeats as long as the current value being considered is greater than the value to its left
142
+			# Being greater than the value to its left means that it is not in the correct location
143
+			j = i
144
+			while j + interval < len(lista):
145
+				if lista[j] > lista[j + interval]:
146
+					# Swapping values so that smaller value is to the left
147
+					temp = lista[j]
148
+					lista[j] = lista[j + interval]
149
+					lista[j + interval] = temp
150
+					# print(lista)
151
+					n = j
152
+					# Continue comparing value that was swapped left to other values to the left to make sure it is placed in the correct location
153
+					while n - interval >= 0 and lista[n] < lista[n - interval]:
154
+						# Swapping values so that smaller value is to the left
155
+						temp = lista[n]
156
+						lista[n] = lista[n - interval]
157
+						lista[n - interval] = temp
158
+						n -= interval
159
+				# print(lista)
160
+				# Update index to continue comparison with the next value in the sub array
161
+				j += interval
162
+		interval //= 2
163
+	# print(lista)
83 164
 	return lista
84 165
 
85 166
 maxValor=1000 	#define el valor maximo de los elementos de la lista