Skip to content

Commit 3ce2039

Browse files
Ascending quick sort
1 parent bee067f commit 3ce2039

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

‎main.py‎

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from ast import Yield
12
import pygame
23
import random
34
from math import ceil
@@ -204,11 +205,40 @@ def mergeSort(self, start=0, end=False):
204205
c += 1
205206
yield True
206207

207-
# TODO Quick Sort https://www.geeksforgeeks.org/quick-sort/
208-
def quickSort(self):
209-
pass
208+
def quickSort(self, low=0, high=None):
209+
if high is None:
210+
high = self.bars - 1
211+
partition = self.quickSortPartition(low, high)
212+
while low < high:
213+
while True:
214+
try:
215+
pi = next(partition)
216+
break
217+
except StopIteration:
218+
partition = self.quickSortPartition(low, high)
219+
break
220+
if pi:
221+
if pi - low < high - pi:
222+
yield from self.quickSort(low, pi - 1)
223+
low = pi + 1
224+
else:
225+
yield from self.quickSort(pi + 1, high)
226+
high = pi - 1
227+
else:
228+
yield True
229+
230+
# TODO add descending
231+
def quickSortPartition(self, l, h):
232+
pivot = self.list[h]
233+
i = l - 1
234+
for j in range(l, h):
235+
if self.list[j] <= pivot:
236+
i = i + 1
237+
(self.list[i], self.list[j]) = (self.list[j], self.list[i])
238+
yield False
210239

211-
# TODO check sort against https://clementmihailescu.github.io/Sorting-Visualizer/
240+
(self.list[i + 1], self.list[h]) = (self.list[h], self.list[i + 1])
241+
yield i + 1
212242

213243
# Main function
214244
def run(self):

0 commit comments

Comments
 (0)