├── README.md └── tutorial.py /README.md: -------------------------------------------------------------------------------- 1 | # Sorting-Algorithm-Visualizer 2 | Python sorting algorithm visualizer. 3 | 4 | 5 | # 💻 Launch Your Software Development Career Today! 6 | 7 | 🎓 **No degree? No problem!** My program equips you with everything you need to break into tech and land an entry-level software development role. 8 | 9 | 🚀 **Why Join?** 10 | - 💼 **$70k+ starting salary potential** 11 | - 🕐 **Self-paced:** Complete on your own time 12 | - 🤑 **Affordable:** Low risk compared to expensive bootcamps or degrees 13 | - 🎯 **45,000+ job openings** in the market 14 | 15 | 👉 **[Start your journey today!](https://techwithtim.net/dev)** 16 | No experience needed—just your determination. Future-proof your career and unlock six-figure potential like many of our students have! 17 | -------------------------------------------------------------------------------- /tutorial.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | import random 3 | import math 4 | pygame.init() 5 | 6 | class DrawInformation: 7 | BLACK = 0, 0, 0 8 | WHITE = 255, 255, 255 9 | GREEN = 0, 255, 0 10 | RED = 255, 0, 0 11 | BACKGROUND_COLOR = WHITE 12 | 13 | GRADIENTS = [ 14 | (128, 128, 128), 15 | (160, 160, 160), 16 | (192, 192, 192) 17 | ] 18 | 19 | FONT = pygame.font.SysFont('comicsans', 30) 20 | LARGE_FONT = pygame.font.SysFont('comicsans', 40) 21 | 22 | SIDE_PAD = 100 23 | TOP_PAD = 150 24 | 25 | def __init__(self, width, height, lst): 26 | self.width = width 27 | self.height = height 28 | 29 | self.window = pygame.display.set_mode((width, height)) 30 | pygame.display.set_caption("Sorting Algorithm Visualization") 31 | self.set_list(lst) 32 | 33 | def set_list(self, lst): 34 | self.lst = lst 35 | self.min_val = min(lst) 36 | self.max_val = max(lst) 37 | 38 | self.block_width = round((self.width - self.SIDE_PAD) / len(lst)) 39 | self.block_height = math.floor((self.height - self.TOP_PAD) / (self.max_val - self.min_val)) 40 | self.start_x = self.SIDE_PAD // 2 41 | 42 | 43 | def draw(draw_info, algo_name, ascending): 44 | draw_info.window.fill(draw_info.BACKGROUND_COLOR) 45 | 46 | title = draw_info.LARGE_FONT.render(f"{algo_name} - {'Ascending' if ascending else 'Descending'}", 1, draw_info.GREEN) 47 | draw_info.window.blit(title, (draw_info.width/2 - title.get_width()/2 , 5)) 48 | 49 | controls = draw_info.FONT.render("R - Reset | SPACE - Start Sorting | A - Ascending | D - Descending", 1, draw_info.BLACK) 50 | draw_info.window.blit(controls, (draw_info.width/2 - controls.get_width()/2 , 45)) 51 | 52 | sorting = draw_info.FONT.render("I - Insertion Sort | B - Bubble Sort", 1, draw_info.BLACK) 53 | draw_info.window.blit(sorting, (draw_info.width/2 - sorting.get_width()/2 , 75)) 54 | 55 | draw_list(draw_info) 56 | pygame.display.update() 57 | 58 | 59 | def draw_list(draw_info, color_positions={}, clear_bg=False): 60 | lst = draw_info.lst 61 | 62 | if clear_bg: 63 | clear_rect = (draw_info.SIDE_PAD//2, draw_info.TOP_PAD, 64 | draw_info.width - draw_info.SIDE_PAD, draw_info.height - draw_info.TOP_PAD) 65 | pygame.draw.rect(draw_info.window, draw_info.BACKGROUND_COLOR, clear_rect) 66 | 67 | for i, val in enumerate(lst): 68 | x = draw_info.start_x + i * draw_info.block_width 69 | y = draw_info.height - (val - draw_info.min_val) * draw_info.block_height 70 | 71 | color = draw_info.GRADIENTS[i % 3] 72 | 73 | if i in color_positions: 74 | color = color_positions[i] 75 | 76 | pygame.draw.rect(draw_info.window, color, (x, y, draw_info.block_width, draw_info.height)) 77 | 78 | if clear_bg: 79 | pygame.display.update() 80 | 81 | 82 | def generate_starting_list(n, min_val, max_val): 83 | lst = [] 84 | 85 | for _ in range(n): 86 | val = random.randint(min_val, max_val) 87 | lst.append(val) 88 | 89 | return lst 90 | 91 | 92 | def bubble_sort(draw_info, ascending=True): 93 | lst = draw_info.lst 94 | 95 | for i in range(len(lst) - 1): 96 | for j in range(len(lst) - 1 - i): 97 | num1 = lst[j] 98 | num2 = lst[j + 1] 99 | 100 | if (num1 > num2 and ascending) or (num1 < num2 and not ascending): 101 | lst[j], lst[j + 1] = lst[j + 1], lst[j] 102 | draw_list(draw_info, {j: draw_info.GREEN, j + 1: draw_info.RED}, True) 103 | yield True 104 | 105 | return lst 106 | 107 | def insertion_sort(draw_info, ascending=True): 108 | lst = draw_info.lst 109 | 110 | for i in range(1, len(lst)): 111 | current = lst[i] 112 | 113 | while True: 114 | ascending_sort = i > 0 and lst[i - 1] > current and ascending 115 | descending_sort = i > 0 and lst[i - 1] < current and not ascending 116 | 117 | if not ascending_sort and not descending_sort: 118 | break 119 | 120 | lst[i] = lst[i - 1] 121 | i = i - 1 122 | lst[i] = current 123 | draw_list(draw_info, {i - 1: draw_info.GREEN, i: draw_info.RED}, True) 124 | yield True 125 | 126 | return lst 127 | 128 | 129 | def main(): 130 | run = True 131 | clock = pygame.time.Clock() 132 | 133 | n = 50 134 | min_val = 0 135 | max_val = 100 136 | 137 | lst = generate_starting_list(n, min_val, max_val) 138 | draw_info = DrawInformation(800, 600, lst) 139 | sorting = False 140 | ascending = True 141 | 142 | sorting_algorithm = bubble_sort 143 | sorting_algo_name = "Bubble Sort" 144 | sorting_algorithm_generator = None 145 | 146 | while run: 147 | clock.tick(60) 148 | 149 | if sorting: 150 | try: 151 | next(sorting_algorithm_generator) 152 | except StopIteration: 153 | sorting = False 154 | else: 155 | draw(draw_info, sorting_algo_name, ascending) 156 | 157 | for event in pygame.event.get(): 158 | if event.type == pygame.QUIT: 159 | run = False 160 | 161 | if event.type != pygame.KEYDOWN: 162 | continue 163 | 164 | if event.key == pygame.K_r: 165 | lst = generate_starting_list(n, min_val, max_val) 166 | draw_info.set_list(lst) 167 | sorting = False 168 | elif event.key == pygame.K_SPACE and sorting == False: 169 | sorting = True 170 | sorting_algorithm_generator = sorting_algorithm(draw_info, ascending) 171 | elif event.key == pygame.K_a and not sorting: 172 | ascending = True 173 | elif event.key == pygame.K_d and not sorting: 174 | ascending = False 175 | elif event.key == pygame.K_i and not sorting: 176 | sorting_algorithm = insertion_sort 177 | sorting_algo_name = "Insertion Sort" 178 | elif event.key == pygame.K_b and not sorting: 179 | sorting_algorithm = bubble_sort 180 | sorting_algo_name = "Bubble Sort" 181 | 182 | 183 | pygame.quit() 184 | 185 | 186 | if __name__ == "__main__": 187 | main() --------------------------------------------------------------------------------