]> Skullheadx's Git Forge - Sorting.git/commitdiff
quicksort fix main
authorSkullheadx <admonty1@gmail.com>
Tue, 11 Apr 2023 19:42:13 +0000 (15:42 -0400)
committerSkullheadx <admonty1@gmail.com>
Tue, 11 Apr 2023 19:42:13 +0000 (15:42 -0400)
display.py
main.py
sorting_algorithms/quick_sort.py

index 0a82f1e33a9b32191126eacd3e42455884e6dbd3..021e24848e8d0cf2f4260e9958a60e89a4d8e2aa 100644 (file)
@@ -14,7 +14,7 @@ class Display:
     DIMENSIONS = (WIDTH, HEIGHT)
     FPS = 0
 
-    DELAY_TIME = 20
+    DELAY_TIME = 20  # in milliseconds
     # 25 for insertion sort
     # 75 for selection sort
     # 10 for bogo sort
diff --git a/main.py b/main.py
index e1b31a0ceb09522da7a579bd16b0502580217c55..e82f05a89239267686f0af831f2df027f18de210 100644 (file)
--- a/main.py
+++ b/main.py
@@ -4,12 +4,12 @@ from sorting_algorithms.insertion_sort import insertion_sort_frames
 from sorting_algorithms.selection_sort import selection_sort_frames
 from sorting_algorithms.bogo_sort import bogo_sort_frames
 from sorting_algorithms.bubble_sort import bubble_sort_frames
-from sorting_algorithms.quick_sort import quick_sort_frames
+from sorting_algorithms.quick_sort import quick_sort
 
 
 def main():
-    array = [random.random() for _ in range(50)]
-    frames = quick_sort_frames(array)
+    array = [random.random() for _ in range(500)]
+    frames = quick_sort(array)
     window = Display(frames)
     window.show()
 
index b497b058b6cb41c67806d06263e4f02d4e5e6abc..c9a3dca6d075614be2047c3d18cf8634623e3844 100644 (file)
@@ -1,65 +1,39 @@
-from sorting_algorithms.utils import swap, is_increasing
 import random
 
-def quick_sort_frames(array, start=0, end=None, frames=None):
+
+def partition(array, begin, end, frames):
+    pivot_index = begin
+
+    for i in range(begin, end):
+        if array[i] < array[begin]:
+            pivot_index += 1
+            array[i], array[pivot_index] = array[pivot_index], array[i]
+            frames.append(array[:])
+
+    array[pivot_index], array[begin] = array[begin], array[pivot_index]
+    frames.append(array[:])
+
+    return pivot_index
+
+
+def quick_sort(array, begin=0, end=None, frames=None):
     if frames is None:
         frames = []
-        is_root = True
-    else:
-        is_root = False
+
     if end is None:
-        end = len(array) - 1
-    pivot = array[end]  # pivot is the last element
-    i = start-1
-    j = start
-    while j < end:  # loop through each element
-        if array[j] < pivot:  # if less than pivot, swap with i
-            i += 1
-            swap(array, i, j)
-            frames.append(array[:])
-        j += 1
-    swap(array, i + 1, j)  # swap pivot with i + 1
-    frames.append(array[:])
-    pivot = i + 1  # set pivot to i + 1
-    if start < pivot - 1:  # if there are elements to the left of pivot
-        quick_sort_frames(array, start, pivot - 1, frames)  # sort the left side
-    if pivot + 1 < end:  # if there are elements to the right of pivot
-        quick_sort_frames(array, pivot + 1, end, frames)  # sort the right side
-    if is_root:
-        return frames
-    else:
-        return array  # return sorted array
+        end = len(array)
+
+    if begin != end and begin + 1 != end:
+        pivot = partition(array, begin, end, frames)
+
+        quick_sort(array, begin, pivot, frames)
+
+        quick_sort(array, pivot + 1, end, frames)
 
+    return frames
 
-# arr = [random.randint(0, 100) for _ in range(10)]
-# print(arr)
-# print(quick_sort(arr))
-# print(is_increasing(arr))
 
-# import random
-#
-# def partition(array, begin, end):
-#     pivot_index = begin
-#
-#     for i in range(begin, end):
-#         if array[i] < array[begin]:
-#             pivot_index += 1
-#             array[i], array[pivot_index] = array[pivot_index], array[i]
-#
-#     array[pivot_index], array[begin] = array[begin], array[pivot_index]
-#
-#     return pivot_index
-#
-# def quick_sort(array, begin, end):
-#     if begin != end and begin + 1 != end:
-#         pivot = partition(array, begin, end)
-#
-#         quick_sort(array, begin, pivot)
-#
-#         quick_sort(array, pivot + 1, end)
-#
-#
-# array = [random.randint(0, 100) for _ in range(10)]
-# print(array)
-# quick_sort(array, 0, len(array))
-# print(array)
\ No newline at end of file
+array = [random.randint(0, 100) for _ in range(10)]
+print(array)
+quick_sort(array, 0, len(array), [])
+print(array)
\ No newline at end of file