]> Skullheadx's Git Forge - Sorting.git/commitdiff
add visuals for all
authorSkullheadx <704277@pdsb.net>
Wed, 25 Jan 2023 05:05:04 +0000 (00:05 -0500)
committerSkullheadx <704277@pdsb.net>
Wed, 25 Jan 2023 05:05:04 +0000 (00:05 -0500)
display.py
main.py
sorting_algorithms/bogo_sort.py
sorting_algorithms/bubble_sort.py
sorting_algorithms/insertion_sort.py
sorting_algorithms/selection_sort.py

index 66f1634d9403e39c98e4fd195a6716fa92c8ffa1..0a82f1e33a9b32191126eacd3e42455884e6dbd3 100644 (file)
@@ -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 f943f797d50b7263dfcc7e11887f750a017cc88d..19ac38464a01231e8c6aa40e475362923ba20680 100644 (file)
--- 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()
index 0f7b91bc02435dbddc0c9707f57a54c603f19500..f70a2108219c2f7cf5754e068c758fdc6dd791a5 100644 (file)
@@ -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
index aa81519bb9e2caf79f5206a11ac8696a89fdf87a..18f3c9424adfed4c74d81c74cc614f83a7ac525b 100644 (file)
@@ -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
index faa4ad44d337e42b1311b6bab53598f1f6dd92cc..6bda4dc35835332a0001b8f061db59d2b9821443 100644 (file)
@@ -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
index 99d7f54c0eb6a879782f08c87ecd08714ca86519..71ceab1ff09a99b96bc30636c6b30cc8da595a93 100644 (file)
@@ -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