Преглед изворни кода

Squashed commit of the following:

commit 96a1a46370
Author: Ricardo-gonzalez32 <ricardo.gonzalez32@upr.edu>
Date:   Tue Oct 11 20:02:51 2022 -0400

    added quicksort function
Ricardo-gonzalez32 пре 2 година
родитељ
комит
8a4387dac6
1 измењених фајлова са 26 додато и 3 уклоњено
  1. 26
    3
      sorting.py

+ 26
- 3
sorting.py Прегледај датотеку

@@ -83,8 +83,31 @@ def heapSort(lista):
83 83
 		return lista
84 84
 
85 85
 def quickSort(lista):
86
-		#definan el algoritmo de ordenamiento quicksort
86
+	#definan el algoritmo de ordenamiento quicksort
87
+	elements = len(lista)
88
+    
89
+    #Base case
90
+	if elements < 2:
87 91
 		return lista
92
+    
93
+	current_position = 0 #Position of the partitioning element
94
+
95
+	for i in range(1, elements): #Partitioning loop
96
+		if lista[i] <= lista[0]:
97
+			current_position += 1
98
+			temp = lista[i]
99
+			lista[i] = lista[current_position]
100
+			lista[current_position] = temp
101
+
102
+	temp = lista[0]
103
+	lista[0] = lista[current_position] 
104
+	lista[current_position] = temp #Brings pivot to it's appropriate position
105
+    
106
+	left = quickSort(lista[0:current_position]) #Sorts the elements to the left of pivot
107
+	right = quickSort(lista[current_position+1:elements]) #sorts the elements to the right of pivot
108
+
109
+	lista = left + [lista[current_position]] + right #Merging everything together
110
+	return lista
88 111
 
89 112
 def shellSort(lista):
90 113
 		#definan el algoritmo de ordenamiento shellsort
@@ -116,9 +139,9 @@ for i in range(veces):
116 139
 	print(heaplista)						#desplegamos la lista
117 140
 
118 141
 	t1 = time.process_time()				#tomamos el tiempo inicial
119
-	quickSort(quicklista)					#ejecutamos el algoritmo quickSort
142
+	quickresult = quickSort(quicklista)					#ejecutamos el algoritmo quickSort
120 143
 	acumulaQuick+=time.process_time() - t1	#acumulamos el tiempo de ejecucion
121
-	print(quicklista)						#desplegamos la lista
144
+	print(quickresult)						#desplegamos la lista
122 145
 
123 146
 	t1 = time.process_time()				#tomamos el tiempo inicial
124 147
 	shellSort(searchlista)					#ejecutamos el algoritmo shellSort