import pygame
+import time
pygame.init()
pygame.display.set_caption("sorting visuals")
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)
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)
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)
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()
# 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
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
frames.append(array[:])
if is_sorted: # break out if already sorted
break
- return frames # return sorted array
+ return frames
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
# Selection Sort (O(n^2))
-from utils import swap
+from sorting_algorithms.utils import swap
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