Browse Source

quickSort -ED

eduardo.figueroa7 2 years ago
parent
commit
28d7a040d2
1 changed files with 25 additions and 1 deletions
  1. 25
    1
      sorting.py

+ 25
- 1
sorting.py View File

@@ -18,7 +18,31 @@ def heapSort(lista):
18 18
 	return lista
19 19
 
20 20
 def quickSort(lista):
21
-	#definan el algoritmo de ordenamiento quicksort
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
+    
22 46
 	return lista
23 47
 
24 48
 def shellSort(lista):