Browse Source

Merge remote-tracking branch 'git.ccom.uprrp.edu/Eduardo-quickSort'

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

+ 25
- 1
sorting.py View File

@@ -88,7 +88,31 @@ def heapSort(lista):
88 88
 		heapify(lista, i, 0)
89 89
 
90 90
 def quickSort(lista):
91
-	#definan el algoritmo de ordenamiento quicksort
91
+    #definan el algoritmo de ordenamiento quicksort
92
+    elements = len(lista)
93
+
94
+    #Base case
95
+    if elements < 2:
96
+        return lista
97
+
98
+    current_position = 0 #Position of the partitioning element
99
+
100
+    for i in range(1, elements): #Partitioning loop
101
+         if lista[i] <= lista[0]:
102
+              current_position += 1
103
+              temp = lista[i]
104
+              lista[i] = lista[current_position]
105
+              lista[current_position] = temp
106
+
107
+    temp = lista[0]
108
+    lista[0] = lista[current_position]
109
+    lista[current_position] = temp #Brings pivot to it's appropriate position
110
+
111
+	left = quickSort(lista[0:current_position]) #Sorts the elements to the left of pivot
112
+	right = quickSort(lista[current_position+1:elements]) #sorts the elements to the right of pivot
113
+
114
+	lista = left + [lista[current_position]] + right #Merging everything together
115
+    
92 116
 	return lista
93 117
 
94 118
 def shellSort(lista):