Skip to content

Commit

Permalink
Merge pull request #5 from CoolSpy3/master
Browse files Browse the repository at this point in the history
Bug fixes and Miracle Sort
  • Loading branch information
ProfessorAtomicManiac authored Sep 18, 2022
2 parents c33a317 + abbc54d commit 9330905
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
21 changes: 16 additions & 5 deletions Algorithms/Sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -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)]
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)]
32 changes: 16 additions & 16 deletions UI/UI.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import threading
import time
import tkinter
from tkinter import filedialog

import Algorithms.Sorting as Sorting
import pygame

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
Expand Down Expand Up @@ -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 = []
Expand All @@ -258,26 +269,15 @@ 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

buttons = []
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
Expand Down

0 comments on commit 9330905

Please sign in to comment.