Browse Source

Merge branch 'QuickSort' into sortingFinal

root 1 year ago
parent
commit
29d2a0f304
4 changed files with 59 additions and 18 deletions
  1. 21
    18
      sorting.py
  2. 14
    0
      test_sortAlgo.py
  3. BIN
      utils/__pycache__/qsortUtils.cpython-38.pyc
  4. 24
    0
      utils/qsortUtils.py

+ 21
- 18
sorting.py View File

@@ -8,6 +8,7 @@ Al final se imprimen los promedios de cada algortimo
8 8
 """
9 9
 from random import randint
10 10
 import time
11
+import utils.qsortUtils as qsortUtils
11 12
 
12 13
 def mergeSort(lista):
13 14
 	#definan el algoritmo de ordenamiento mergesort
@@ -67,8 +68,8 @@ def heapSort(lista):
67 68
 	return lista
68 69
 
69 70
 def quickSort(lista):
70
-	#definan el algoritmo de ordenamiento quicksort
71
-	return lista
71
+	 # Se aplica quicksort a la lista desde el primer elemento hasta el último
72
+	return qsortUtils.qSort(lista, 0, len(lista) - 1)
72 73
 
73 74
 def shellSort(lista):
74 75
 	#definan el algoritmo de ordenamiento shellsort
@@ -103,6 +104,8 @@ veces=100 		#define las veces que se va a hacer el ordenamiento
103 104
 acumulaMerge=0 	#variable para acumular el tiempo de ejecucion del mergesort
104 105
 acumulaHeap=0 	#variable para acumular el tiempo de ejecucion del heapsort
105 106
 acumulaQuick=0 	#variable para acumular el tiempo de ejecucion del quicksort
107
+
108
+
106 109
 acumulaShell=0 	#variable para acumular el tiempo de ejecucion del shellsort
107 110
 
108 111
 for i in range(veces):
@@ -111,25 +114,25 @@ for i in range(veces):
111 114
 	quicklista=list(mergelista)
112 115
 	searchlista=list(mergelista)
113 116
 
114
-	t1 = time.clock() 				#seteamos el tiempo al empezar
117
+	t1 = time.perf_counter() 				#seteamos el tiempo al empezar
115 118
 	mergeSort(mergelista) 				#ejecutamos el algoritmo mergeSort
116
-	acumulaMerge+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
117
-
118
-	t1 = time.clock()				#seteamos el tiempo al empezar
119
+	acumulaMerge+=time.perf_counter()-t1 	#acumulamos el tiempo de ejecucion
120
+	
121
+	t1 = time.perf_counter()			#seteamos el tiempo al empezar
119 122
 	heapSort(heaplista)					#ejecutamos el algoritmo heapSort
120
-	acumulaHeap+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
121
-
122
-	t1 = time.clock()				#seteamos el tiempo al empezar
123
+	acumulaHeap+=time.perf_counter()-t1 	#acumulamos el tiempo de ejecucion
124
+	
125
+	t1 = time.perf_counter()				#seteamos el tiempo al empezar
123 126
 	quickSort(quicklista)				#ejecutamos el algoritmo quickSort
124
-	acumulaQuick+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
125
-
126
-	t1 = time.clock()				#seteamos el tiempo al empezar
127
+	acumulaQuick+=time.perf_counter()-t1 	#acumulamos el tiempo de ejecucion
128
+	
129
+	t1 = time.perf_counter()				#seteamos el tiempo al empezar
127 130
 	shellSort(searchlista)				#ejecutamos el algoritmo shellSort
128
-	acumulaShell+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
131
+	acumulaShell+=time.perf_counter()-t1 	#acumulamos el tiempo de ejecucion
129 132
 
130 133
 #imprimos los resultados
131
-print "Promedio de tiempo de ejecucion de "+ str(veces) +" listas de largo " + str(largoLista)
132
-print "MergeSort " + str(acumulaMerge/veces) + " segundos"
133
-print "HeapSort " + str(acumulaHeap/veces) + " segundos"
134
-print "QuickSort " + str(acumulaQuick/veces) + " segundos"
135
-print "ShellSort " + str(acumulaShell/veces) + " segundos"
134
+print (f"Promedio de tiempo de ejecucion de {veces} listas de largo {largoLista}")
135
+print (f"MergeSort {str(acumulaMerge/veces)} segundos")
136
+print (f"HeapSort {str(acumulaHeap/veces)} segundos")
137
+print (f"QuickSort {str(acumulaQuick/veces)} segundos")
138
+print (f"ShellSort {str(acumulaShell/veces) }segundos")

+ 14
- 0
test_sortAlgo.py View File

@@ -0,0 +1,14 @@
1
+from email import utils
2
+
3
+
4
+from utils import qsortUtils
5
+from random import randint
6
+
7
+enteros = [0,7,3,1,4,5,2]
8
+negativos = [8,-1,3,2,-4,9]
9
+aleatoria =[randint(-49,50) for r in range(10)]
10
+
11
+print(qsortUtils.qSort(enteros,0,len(enteros)-1) == enteros.sort())
12
+print(qsortUtils.qSort(negativos,0,len(negativos)-1) == negativos.sort())
13
+print(qsortUtils.qSort(aleatoria,0,len(aleatoria)-1) == aleatoria.sort())
14
+

BIN
utils/__pycache__/qsortUtils.cpython-38.pyc View File


+ 24
- 0
utils/qsortUtils.py View File

@@ -0,0 +1,24 @@
1
+# Función partition: Selecciona el pivote y divide la lista en dos conjuntos: Elementos menores o iguales al pivote y elementos mayores al pivote
2
+def partition(lista, p, r): 
3
+
4
+  x = lista[r] # Selección del último elemento en la lista como el pivote
5
+  i = p - 1 # El conjunto de elementos menores o iguales al pivote está inicialmente vacío
6
+
7
+  for j in range (p, r):
8
+
9
+    if lista[j] <= x: # Comparación de elemento en la lista con el pivote
10
+      i += 1 # Hacer espacio para el elemento A[j], se coloca un espacio luego del último elemento del conjunto de elementos menores o iguales al pivote.
11
+      lista[j], lista[i] = lista[i], lista[j] #Swap A[j] con A[i]
12
+
13
+  lista[i+1], lista[r] = lista[r], lista[i+1] # Colocar el pivote como último elemento del conjunto de menores o iguales
14
+
15
+  return i + 1 # Devuelve el índice del pivote actual
16
+
17
+# Función qSort: Aplica el algoritmo de Quicksort a una lista
18
+def qSort(lista, p, r):
19
+
20
+  if p < r: # Caso Base: Se llegó a un elemento individual. La lista es un solo elemento
21
+    q = partition(lista, p, r) # Divide/Combine: Particiona la lista y se obtiene el índice del pivote
22
+    qSort(lista, p, q - 1) # Conquer: Se aplica qSort al conjunto de elementos menores o iguales al pivote actual
23
+    qSort(lista, q + 1, r) # Conquer: Se aplica qSort al conjunto de elementos mayores al pivote actual
24
+