From: Skullheadx <704277@pdsb.net> Date: Wed, 25 Jan 2023 05:05:04 +0000 (-0500) Subject: add visuals for all X-Git-Url: http://git.skullheadx.com/phil/index.html?a=commitdiff_plain;h=04b51789e3359f284f750ffe5f440cfba4702149;p=Sorting.git add visuals for all --- diff --git a/display.py b/display.py index 66f1634..0a82f1e 100644 --- a/display.py +++ b/display.py @@ -1,4 +1,5 @@ import pygame +import time pygame.init() pygame.display.set_caption("sorting visuals") @@ -9,11 +10,16 @@ WHITE = (255, 255, 255) class Display: - WIDTH, HEIGHT = 1080, 720 + WIDTH, HEIGHT = 1920, 1080 DIMENSIONS = (WIDTH, HEIGHT) FPS = 0 - DELAY_TIME = 0 + DELAY_TIME = 20 + # 25 for insertion sort + # 75 for selection sort + # 10 for bogo sort + # 20 for bubble sort + def __init__(self, frames): self.screen = pygame.display.set_mode(self.DIMENSIONS) @@ -30,6 +36,9 @@ class Display: for event in pygame.event.get(): if event.type == pygame.QUIT: is_running = False + if event.type == pygame.KEYUP: + if event.key == pygame.K_ESCAPE: + is_running = False self.update() self.draw(self.screen) @@ -52,5 +61,5 @@ class Display: height = self.HEIGHT / max(self.frames[self.index]) for i, element in enumerate(self.frames[self.index]): - r = pygame.Rect(i * width, self.HEIGHT - element * height + 1, width + 1, element * height) + r = pygame.Rect(i * width, self.HEIGHT - element * height, width + 1, element * height + 1) pygame.draw.rect(surf, BLACK, r) diff --git a/main.py b/main.py index f943f79..19ac384 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,13 @@ import random from display import Display -from sorting_algorithms.bubble_sort import bubble_sort, bubble_sort_frames +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 + def main(): - array = [random.randint(10, 50) for _ in range(75)] + array = [random.random() for _ in range(50)] frames = bubble_sort_frames(array) window = Display(frames) window.show() diff --git a/sorting_algorithms/bogo_sort.py b/sorting_algorithms/bogo_sort.py index 0f7b91b..f70a210 100644 --- a/sorting_algorithms/bogo_sort.py +++ b/sorting_algorithms/bogo_sort.py @@ -1,10 +1,19 @@ # Bogo Sort (O(n*n!)) from random import sample -from utils import swap, is_increasing +from sorting_algorithms.utils import swap, is_increasing def bogo_sort(array): - while not is_increasing(array): - a, b = sample(range(len(array)), 2) - swap(array, a, b) - return array + while not is_increasing(array): # repeat until completely sorted in ascending order + a, b = sample(range(len(array)), 2) # get two random indices + swap(array, a, b) # swap the two indices + return array # return the sorted array + + +def bogo_sort_frames(array): + frames = [] + while not is_increasing(array): # repeat until completely sorted in ascending order + a, b = sample(range(len(array)), 2) # get two random indices + swap(array, a, b) # swap the two indices + frames.append(array[:]) + return frames diff --git a/sorting_algorithms/bubble_sort.py b/sorting_algorithms/bubble_sort.py index aa81519..18f3c94 100644 --- a/sorting_algorithms/bubble_sort.py +++ b/sorting_algorithms/bubble_sort.py @@ -12,6 +12,8 @@ def bubble_sort(array): if is_sorted: # break out if already sorted break return array # return sorted array + + def bubble_sort_frames(array): frames = [] for top in range(len(array) - 1, 0, -1): # loop through each element from end @@ -23,4 +25,4 @@ def bubble_sort_frames(array): frames.append(array[:]) if is_sorted: # break out if already sorted break - return frames # return sorted array + return frames diff --git a/sorting_algorithms/insertion_sort.py b/sorting_algorithms/insertion_sort.py index faa4ad4..6bda4dc 100644 --- a/sorting_algorithms/insertion_sort.py +++ b/sorting_algorithms/insertion_sort.py @@ -9,3 +9,15 @@ def insertion_sort(array): break # already inserted element, move on to the next element return array # return sorted array + +def insertion_sort_frames(array): + frames = [] + for index1 in range(1, len(array)): # look at second element + for index2 in range(0, index1): # loop through each of previous elements to find where to insert + if array[index2] > array[index1]: # find where in the sorted portion to put next element + array.insert(index2, array[index1]) # insert smaller element into sorted portion + del array[index1 + 1] # +1 because inserted an element + frames.append(array[:]) + break # already inserted element, move on to the next element + + return frames diff --git a/sorting_algorithms/selection_sort.py b/sorting_algorithms/selection_sort.py index 99d7f54..71ceab1 100644 --- a/sorting_algorithms/selection_sort.py +++ b/sorting_algorithms/selection_sort.py @@ -1,5 +1,5 @@ # Selection Sort (O(n^2)) -from utils import swap +from sorting_algorithms.utils import swap def selection_sort(array): @@ -10,3 +10,15 @@ def selection_sort(array): swap_index = index2 # set the swap index swap(array, index1, swap_index) # use swap the largest and the index1 (from end) return array # return sorted array + + +def selection_sort_frames(array): + frames = [] + for index1 in range(len(array) - 1, 0, -1): # loop through from end + swap_index = 0 # largest element to swap with + for index2 in range(index1 + 1): # loop through the first index1 elements + if array[index2] > array[swap_index]: # find the max element to swap with + swap_index = index2 # set the swap index + swap(array, index1, swap_index) # use swap the largest and the index1 (from end) + frames.append(array[:]) + return frames