Browse Source

quickSort-ED

eduardo.figueroa7 1 year ago
commit
39bccd90b7
1 changed files with 89 additions and 0 deletions
  1. 89
    0
      sorting.py

+ 89
- 0
sorting.py View File

@@ -0,0 +1,89 @@
1
+"""
2
+Carlos J Corrada Bravo
3
+Este programa calcula el promedio de tiempo de ejecucion de cuatro algoritmos de ordenamiento
4
+La variable maxValor define el valor maximo de los elementos de la lista
5
+La variable largoLista define el largo de las listas a ordenar
6
+La variable veces define las veces que se va a hacer el ordenamiento 
7
+Al final se imprimen los promedios de cada algortimo
8
+"""
9
+from random import randint
10
+import time
11
+
12
+def mergeSort(lista):
13
+	#definan el algoritmo de ordenamiento mergesort
14
+	return lista
15
+
16
+def heapSort(lista):
17
+	#definan el algoritmo de ordenamiento heapsort
18
+	return lista
19
+
20
+def quickSort(lista):
21
+    #definan el algoritmo de ordenamiento quicksort
22
+    elements = len(lista)
23
+
24
+    #Base case
25
+    if elements < 2:
26
+        return lista
27
+
28
+    current_position = 0 #Position of the partitioning element
29
+
30
+    for i in range(1, elements): #Partitioning loop
31
+         if lista[i] <= lista[0]:
32
+              current_position += 1
33
+              temp = lista[i]
34
+              lista[i] = lista[current_position]
35
+              lista[current_position] = temp
36
+
37
+    temp = lista[0]
38
+    lista[0] = lista[current_position]
39
+    lista[current_position] = temp #Brings pivot to it's appropriate position
40
+
41
+	left = quickSort(lista[0:current_position]) #Sorts the elements to the left of pivot
42
+	right = quickSort(lista[current_position+1:elements]) #sorts the elements to the right of pivot
43
+
44
+	lista = left + [lista[current_position]] + right #Merging everything together
45
+    
46
+	return lista
47
+
48
+def shellSort(lista):
49
+	#definan el algoritmo de ordenamiento shellsort
50
+	return lista
51
+
52
+maxValor=1000 	#define el valor maximo de los elementos de la lista
53
+largoLista=1000 #define el largo de las listas a ordenar
54
+veces=100 		#define las veces que se va a hacer el ordenamiento 
55
+
56
+acumulaMerge=0 	#variable para acumular el tiempo de ejecucion del mergesort
57
+acumulaHeap=0 	#variable para acumular el tiempo de ejecucion del heapsort
58
+acumulaQuick=0 	#variable para acumular el tiempo de ejecucion del quicksort
59
+acumulaShell=0 	#variable para acumular el tiempo de ejecucion del shellsort
60
+
61
+for i in range(veces):
62
+	mergelista = [randint(0,maxValor) for r in range(largoLista)] #creamos una lista con valores al azar
63
+	heaplista=list(mergelista)
64
+	quicklista=list(mergelista)
65
+	searchlista=list(mergelista)
66
+
67
+	t1 = time.clock() 				#seteamos el tiempo al empezar
68
+	mergeSort(mergelista) 				#ejecutamos el algoritmo mergeSort
69
+	acumulaMerge+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
70
+	
71
+	t1 = time.clock()				#seteamos el tiempo al empezar
72
+	heapSort(heaplista)					#ejecutamos el algoritmo heapSort
73
+	acumulaHeap+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
74
+	
75
+	t1 = time.clock()				#seteamos el tiempo al empezar
76
+	quickSort(quicklista)				#ejecutamos el algoritmo quickSort
77
+	acumulaQuick+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
78
+	
79
+	t1 = time.clock()				#seteamos el tiempo al empezar
80
+	shellSort(searchlista)				#ejecutamos el algoritmo shellSort
81
+	acumulaShell+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
82
+
83
+#imprimos los resultados
84
+print "Promedio de tiempo de ejecucion de "+ str(veces) +" listas de largo " + str(largoLista)
85
+print "MergeSort " + str(acumulaMerge/veces) + " segundos"
86
+print "HeapSort " + str(acumulaHeap/veces) + " segundos"
87
+print "QuickSort " + str(acumulaQuick/veces) + " segundos"
88
+print "ShellSort " + str(acumulaShell/veces) + " segundos"
89
+