diegoaperez 3 yıl önce
ebeveyn
işleme
62d2acca10
1 değiştirilmiş dosya ile 1 ekleme ve 112 silme
  1. 1
    112
      sorting.py

+ 1
- 112
sorting.py Dosyayı Görüntüle

@@ -11,71 +11,11 @@ from random import randint
11 11
 import time
12 12
 
13 13
 def mergeSort(lista):
14
-        #Camila Vazquez Rodriguez 
15 14
 	#definan el algoritmo de ordenamiento mergesort
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
15
+	return lista
48 16
 
49 17
 def heapSort(lista):
50 18
 	#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)
79 19
 	return lista
80 20
 
81 21
 def quickSort(lista):
@@ -105,29 +45,6 @@ def quickSort(lista):
105 45
 
106 46
 def shellSort(lista):
107 47
 	#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
-
131 48
 	return lista
132 49
 
133 50
 maxValor=1000 	#define el valor maximo de los elementos de la lista
@@ -140,7 +57,6 @@ acumulaQuick=0 	#variable para acumular el tiempo de ejecucion del quicksort
140 57
 acumulaShell=0 	#variable para acumular el tiempo de ejecucion del shellsort
141 58
 
142 59
 for i in range(veces):
143
-<<<<<<< HEAD
144 60
     lista = [randint(0,maxValor) for r in range(largoLista)] #creamos una lista con valores al azar
145 61
 
146 62
     # creamos copias de la lista para cada algoritmo
@@ -171,30 +87,3 @@ print "MergeSort " + str(acumulaMerge/veces) + " segundos"
171 87
 print "HeapSort " + str(acumulaHeap/veces) + " segundos"
172 88
 print "QuickSort " + str(acumulaQuick/veces) + " segundos"
173 89
 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