Browse Source

Merge Diego

diegoaperez 2 years ago
parent
commit
0e0c4dd304
1 changed files with 112 additions and 1 deletions
  1. 112
    1
      sorting.py

+ 112
- 1
sorting.py View File

@@ -11,11 +11,71 @@ from random import randint
11 11
 import time
12 12
 
13 13
 def mergeSort(lista):
14
+        #Camila Vazquez Rodriguez 
14 15
 	#definan el algoritmo de ordenamiento mergesort
15
-	return lista
16
+        if len(lista) > 1:
17
+                mid = len(lista)//2
18
+                L = lista[:mid]
19
+                R = lista[mid:]
20
+
21
+                mergeSort(L)
22
+                mergeSort(R)
23
+
24
+                i = 0
25
+                j = 0
26
+                k = 0
27
+
28
+        while i < len(L) and j < len(R):
29
+                if L[i] <= R[j]:
30
+                        lista[k] = L[i]
31
+                        i += 1
32
+                else:
33
+                        lista[k] = R[j]
34
+                        j += 1
35
+                k += 1
36
+ 
37
+        while i < len(L):
38
+                lista[k] = L[i]
39
+                i += 1
40
+                k += 1
41
+ 
42
+        while j < len(R):
43
+                lista[k] = R[j]
44
+                j += 1
45
+                k += 1
46
+                
47
+        return lista
16 48
 
17 49
 def heapSort(lista):
18 50
 	#definan el algoritmo de ordenamiento heapsort
51
+	"""
52
+	Carlos Hernández
53
+	Implementación de heapSort.
54
+	"""
55
+	def max_heapify(lista, idx, heap_size):
56
+		"""Convertir el nodo `idx` y sus descendientes en un max heap."""
57
+		left_idx = 2 * idx + 1
58
+		right_idx = 2 * idx + 2
59
+		largestval_idx = idx
60
+		if left_idx < heap_size and lista[idx] < lista[left_idx]:
61
+			largestval_idx = left_idx
62
+		if right_idx < heap_size and lista[largestval_idx] < lista[right_idx]:
63
+			largestval_idx = right_idx
64
+		if largestval_idx != idx:
65
+			lista[idx], lista[largestval_idx] = lista[largestval_idx], lista[idx]
66
+			max_heapify(lista, largestval_idx, heap_size)
67
+
68
+	def build_max_heap(lista, heap_size):
69
+		"""Construir un max heap the un heap dado."""
70
+		for idx in range((heap_size - 1) // 2, -1, -1):
71
+			max_heapify(lista, idx, heap_size)
72
+
73
+	heap_size = len(lista)
74
+	build_max_heap(lista, heap_size)
75
+	for idx in range(len(lista) - 1, 0, -1):
76
+		lista[0], lista[idx] = lista[idx], lista[0]
77
+		heap_size -= 1
78
+		max_heapify(lista, 0, heap_size)
19 79
 	return lista
20 80
 
21 81
 def quickSort(lista):
@@ -45,6 +105,29 @@ def quickSort(lista):
45 105
 
46 106
 def shellSort(lista):
47 107
 	#definan el algoritmo de ordenamiento shellsort
108
+
109
+	#El algoritmo compara elementos de una lista 
110
+	#que tienen una distancia fija entre ellos, la
111
+	#differencia en distancia se minimiza por cada 
112
+	#iterazion del algoritmo
113
+
114
+	num = int(len(lista))
115
+	dif = int(num/2)
116
+
117
+	while dif > 0:
118
+		for i in range(int(dif),int(num)):
119
+			a = lista[i]
120
+			j = i 
121
+
122
+			#por cada intervalo se reorganizara los elementos 
123
+			#si se cumple la declaracion dentro del while loop
124
+			while j >= dif and lista[int(j - dif)] > a:
125
+				lista[int(j)] = lista[int(j - dif)]
126
+				j -= dif
127
+
128
+			lista[int(j)] = a
129
+		dif/= 2
130
+
48 131
 	return lista
49 132
 
50 133
 maxValor=1000 	#define el valor maximo de los elementos de la lista
@@ -57,6 +140,7 @@ acumulaQuick=0 	#variable para acumular el tiempo de ejecucion del quicksort
57 140
 acumulaShell=0 	#variable para acumular el tiempo de ejecucion del shellsort
58 141
 
59 142
 for i in range(veces):
143
+<<<<<<< HEAD
60 144
     lista = [randint(0,maxValor) for r in range(largoLista)] #creamos una lista con valores al azar
61 145
 
62 146
     # creamos copias de la lista para cada algoritmo
@@ -87,3 +171,30 @@ print "MergeSort " + str(acumulaMerge/veces) + " segundos"
87 171
 print "HeapSort " + str(acumulaHeap/veces) + " segundos"
88 172
 print "QuickSort " + str(acumulaQuick/veces) + " segundos"
89 173
 print "ShellSort " + str(acumulaShell/veces) + " segundos"
174
+=======
175
+	lista = [randint(0,maxValor) for r in range(largoLista)] #creamos una lista con valores al azar
176
+
177
+	t1 = time.clock() 				#seteamos el tiempo al empezar
178
+	mergeSort(lista) 				#ejecutamos el algoritmo mergeSort
179
+	acumulaMerge+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
180
+
181
+	t1 = time.clock()				#seteamos el tiempo al empezar
182
+	heapSort(lista)					#ejecutamos el algoritmo heapSort
183
+	acumulaHeap+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
184
+
185
+	t1 = time.clock()				#seteamos el tiempo al empezar
186
+	quickSort(lista)				#ejecutamos el algoritmo quickSort
187
+	acumulaQuick+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
188
+
189
+	t1 = time.clock()				#seteamos el tiempo al empezar
190
+	shellSort(lista)				#ejecutamos el algoritmo shellSort
191
+	acumulaShell+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
192
+
193
+#imprimos los resultados
194
+print ("Promedio de tiempo de ejecucion de " + str(veces) + " listas de largo " + str(largoLista))
195
+print ("MergeSort " + str(acumulaMerge/veces) + " segundos")
196
+print ("HeapSort " + str(acumulaHeap/veces) + " segundos")
197
+print ("QuickSort " + str(acumulaQuick/veces) + " segundos")
198
+print ("ShellSort " + str(acumulaShell/veces) + " segundos")
199
+
200
+>>>>>>> ae9a1771cc651ae782615bb83aee73d658f88b39