From: Skullheadx Date: Tue, 11 Apr 2023 19:42:13 +0000 (-0400) Subject: quicksort fix X-Git-Url: http://git.skullheadx.com/tech/blog/openbsd_html_css/static/git-logo.png?a=commitdiff_plain;p=Sorting.git quicksort fix --- diff --git a/display.py b/display.py index 0a82f1e..021e248 100644 --- a/display.py +++ b/display.py @@ -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 e1b31a0..e82f05a 100644 --- 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() diff --git a/sorting_algorithms/quick_sort.py b/sorting_algorithms/quick_sort.py index b497b05..c9a3dca 100644 --- a/sorting_algorithms/quick_sort.py +++ b/sorting_algorithms/quick_sort.py @@ -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