eduardo.figueroa7 преди 2 години
родител
ревизия
479654dbf9
променени са 2 файла, в които са добавени 78 реда и са изтрити 5 реда
  1. 1
    0
      .gitignore
  2. 77
    5
      sorting.py

+ 1
- 0
.gitignore Целия файл

@@ -0,0 +1 @@
1
+.DS_Store

+ 77
- 5
sorting.py Целия файл

@@ -9,13 +9,83 @@ Al final se imprimen los promedios de cada algortimo
9 9
 from random import randint
10 10
 import time
11 11
 
12
-def mergeSort(lista):
12
+def merge(lista, start, middle, end):
13
+	# Create Temp arrays with specifically needed sizes foe the given array
14
+	sizeOfHalf1 = middle - start + 1
15
+	sizeOfHalf2 = end - middle
16
+	half1 = [0] * (sizeOfHalf1)
17
+	half2 = [0] * (sizeOfHalf2)
18
+    # Copy data from the received array to the temporary working arrays
19
+	for i in range(0, sizeOfHalf1):
20
+		half1[i] = lista[start + i]
21
+	for t in range(0,sizeOfHalf2):
22
+		half2[t] = lista[middle + 1 + t] 
23
+	
24
+	# Merge the temporary arrays back into the recieved list
25
+	i = 0 # initial index of firs subarray 
26
+	t = 0 # initial index of second subarray
27
+	k = start # initial index of merged subarray
28
+
29
+	while i < sizeOfHalf1 and t < sizeOfHalf2:
30
+		if half1[i] <= half2[t]:
31
+			lista[k] = half1[i]
32
+			i += 1
33
+		else:
34
+			lista[k] = half2[t]
35
+			t += 1
36
+		k += 1
37
+	# Copy the remaining elements on half1 if there are any
38
+	while i < sizeOfHalf1:
39
+		lista[k] = half1[i]
40
+		i += 1
41
+		k += 1
42
+
43
+	# Copy the remaining elements on half2 if there are any
44
+	while t < sizeOfHalf2:
45
+		lista[k] = half2[t]
46
+		t += 1
47
+		k += 1
48
+
49
+# start is for left index and end is the right index of the subarray
50
+# of lista to be stored 
51
+
52
+def mergeSort(lista,l,r):
13 53
 	#definan el algoritmo de ordenamiento mergesort
54
+	if l < r:
55
+		# Same as (l+r)//2, but avoids overflow for
56
+        # large l and h
57
+		m = l+(r-l)//2
58
+
59
+		# Sort each half
60
+		mergeSort(lista, l, m)
61
+		mergeSort(lista, m+1, r)
62
+		# Merge the sorted halves
63
+		merge(lista, l, m, r)
64
+
14 65
 	return lista
15 66
 
67
+# https://en.wikipedia.org/wiki/Heapsort
68
+def heapify(lista, n, i):
69
+	largest = i
70
+	l = 2 * i + 1
71
+	r = 2 * i + 2
72
+	if l < n and lista[i] < lista[l]:
73
+		largest = l
74
+	if r < n and lista[largest] < lista[r]:
75
+		largest = r
76
+	if largest != i:
77
+		lista[i], lista[largest] = lista[largest], lista[i]
78
+		heapify(lista, n, largest)
79
+
16 80
 def heapSort(lista):
17
-	#definan el algoritmo de ordenamiento heapsort
18
-	return lista
81
+	n = len(lista)
82
+
83
+	for i in range(n//2, -1, -1):
84
+		heapify(lista, n, i)
85
+
86
+	for i in range(n-1, 0, -1):
87
+		lista[i], lista[0] = lista[0], lista[i]
88
+		heapify(lista, i, 0)
19 89
 
20 90
 def quickSort(lista):
21 91
     #definan el algoritmo de ordenamiento quicksort
@@ -65,7 +135,9 @@ for i in range(veces):
65 135
 	searchlista=list(mergelista)
66 136
 
67 137
 	t1 = time.clock() 				#seteamos el tiempo al empezar
68
-	mergeSort(mergelista) 				#ejecutamos el algoritmo mergeSort
138
+	l = 0
139
+	r = len(mergelista) - 1
140
+	finished = mergeSort(mergelista, l,r) 				#ejecutamos el algoritmo mergeSort
69 141
 	acumulaMerge+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
70 142
 	
71 143
 	t1 = time.clock()				#seteamos el tiempo al empezar
@@ -81,7 +153,7 @@ for i in range(veces):
81 153
 	acumulaShell+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
82 154
 
83 155
 #imprimos los resultados
84
-print "Promedio de tiempo de ejecucion de "+ str(veces) +" listas de largo " + str(largoLista)
156
+print "Promedio de tiempo de ejecucion de " + str(veces) +" listas de largo " + str(largoLista)
85 157
 print "MergeSort " + str(acumulaMerge/veces) + " segundos"
86 158
 print "HeapSort " + str(acumulaHeap/veces) + " segundos"
87 159
 print "QuickSort " + str(acumulaQuick/veces) + " segundos"