diff --git a/Algorithms/Sorting.py b/Algorithms/Sorting.py index c9814c4..91f2784 100644 --- a/Algorithms/Sorting.py +++ b/Algorithms/Sorting.py @@ -45,7 +45,8 @@ def insertion_sort(arr, event, aux = None): ''' def quick_sort(arr, event, aux = None): quick_sort_method(arr, event, 0, arr.length()) - finish(arr) + if not event.is_set(): + finish(arr) def quick_sort_method(arr, event, begin, end): if (begin < end): @@ -74,7 +75,8 @@ def quick_sort_method(arr, event, begin, end): ''' def merge_sort(arr, event, aux = None): merge_sort_method(arr, event, 0, arr.length()) - finish(arr) + if not event.is_set(): + finish(arr) def merge_sort_method(arr, event, begin, end): if (begin+1 < end): @@ -367,10 +369,10 @@ def counting_sort(arr, event, aux): ''' def is_sorted(arr, event): for i in range(0, arr.length() - 1): - if (arr.get(i) > arr.get(i + 1)): - return -1 if (event.is_set()): return 0 + if (arr.get(i) > arr.get(i + 1)): + return -1 return 1 def shuffle(arr, event): @@ -393,6 +395,15 @@ def bogo_sort(arr, event, aux = None): if (num == 1): finish(arr) +def miracle_sort(arr, event, aux = None): + while True: + status = is_sorted(arr, event) + if status == 1: + finish(arr) + return + if status == 0: + return + # Sorting Algorithm Input class SA: ''' name = how it will appear on the dropdown menu @@ -403,4 +414,4 @@ def __init__(self, name, sort): self.sort = sort sorting_algos = [SA("Selection Sort", selection_sort), SA("Insertion Sort", insertion_sort), SA("Quick Sort", quick_sort), SA("Merge Sort", merge_sort), -SA("Heap Sort", heap_sort), SA("Radix Sort", radix_sort), SA("Counting Sort", counting_sort), SA("Bogo Sort", bogo_sort)] \ No newline at end of file +SA("Heap Sort", heap_sort), SA("Radix Sort", radix_sort), SA("Counting Sort", counting_sort), SA("Bogo Sort", bogo_sort), SA("Miracle Sort", miracle_sort)] diff --git a/UI/UI.py b/UI/UI.py index 4a8cac8..855f680 100644 --- a/UI/UI.py +++ b/UI/UI.py @@ -1,5 +1,7 @@ import threading import time +import tkinter +from tkinter import filedialog import Algorithms.Sorting as Sorting import pygame @@ -7,9 +9,6 @@ import UI.Wrapper as Wrapper from UI.Arrays import Array -import tkinter -from tkinter import filedialog - ''' This should only have code concerning the UI duh Specifically the display of screens, and functions for input (button presses) Everything here should just to be to display stuff and inputs @@ -237,6 +236,18 @@ def update(self): def on_reset(self): self.window.event.set() + self.set_sorting_thread(None) + + def set_sorting_thread(self, thread): + if self.sorting_thread is not None: + self.window.event.set() + self.sorting_thread.join() + self.window.event.clear() + self.sorting_thread = thread + + def sort_done(self): + self.sorting_thread = None + self.window.event.set() def display_sorting(self, screen_group, options_screen_group, slide_in = True): self.scroll_bar = [] @@ -258,17 +269,6 @@ def display_sorting(self, screen_group, options_screen_group, slide_in = True): screen_group.add(Wrapper.Text(Wrapper.DefaultText.text("Sorting", Wrapper.FontSizes.TITLE_SIZE), (self.window.window.get_size()[0]/2, self.TITLE_Y), self.window, slide_in)) screen_group.add(Wrapper.Background((self.MARGIN_X + self.SORTING_WIDTH/2, self.SORTING_Y), (self.SORTING_WIDTH, self.SORTING_HEIGHT), Wrapper.Colors.SMALL_BACKGROUND_COLOR, self.window, None, False)) - - def set_sorting_thread(thread): - if self.sorting_thread is not None: - self.window.event.set() - self.sorting_thread.join() - self.window.event.clear() - self.sorting_thread = thread - - def sort_done(): - self.sorting_thread = None - self.window.event.set() self.sorting_thread = None @@ -276,8 +276,8 @@ def sort_done(): resets = [] def bind_function(sort_input): def func(): - sort_algo = Wrapper.sequential_functions(Wrapper.add_args_to_func(sort_input.sort, self.array, self.window.event, self.aux_array), sort_done) - set_sorting_thread(threading.Thread(target=sort_algo)) + sort_algo = Wrapper.sequential_functions(Wrapper.add_args_to_func(sort_input.sort, self.array, self.window.event, self.aux_array), self.sort_done) + self.set_sorting_thread(threading.Thread(target=sort_algo)) return self.sorting_thread.start() func.__name__ = sort_input.name return func