Skip to content

Commit 8bdb353

Browse files
committed
Added merge sort and fixed small issue with Unsort and Randmize clashing
1 parent fab326f commit 8bdb353

3 files changed

Lines changed: 64 additions & 2 deletions

File tree

1.32 KB
Binary file not shown.

algorithms.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#Bubble Sort Function
12
def bubble_sort(array):
23
n = len(array)
34
for i in range(n):
@@ -6,6 +7,7 @@ def bubble_sort(array):
67
array[j], array[j+1] = array[j+1], array[j]
78
yield array, j, j+1
89

10+
#Selection Sort Function
911
def selection_sort(array):
1012
n = len(array)
1113
for i in range(n):
@@ -19,3 +21,50 @@ def selection_sort(array):
1921
array[i], array[min_idx] = array[min_idx], array[i]
2022
# Pause after the swap
2123
yield array, i, min_idx
24+
25+
26+
#Merge Sort Functions
27+
def merge(arr, left, mid, right):
28+
merged = []
29+
i, j = left, mid + 1
30+
31+
#Merge two already-sorted halves
32+
while i <= mid and j <= right:
33+
yield arr, i, j
34+
35+
if arr[i] < arr[j]:
36+
merged.append(arr[i])
37+
i += 1
38+
else:
39+
merged.append(arr[j])
40+
j += 1
41+
42+
#Add the remainders
43+
while i <= mid:
44+
yield arr, i, i
45+
merged.append(arr[i])
46+
i += 1
47+
48+
while j <= right:
49+
yield arr, j, j
50+
merged.append(arr[j])
51+
j += 1
52+
53+
#Copy the now merged list back to an array
54+
for k, val in enumerate(merged):
55+
arr[left + k] = val
56+
yield arr, left + k, left + k
57+
58+
def merge_sort(arr, left, right):
59+
if right - left <= 0:
60+
return
61+
mid = (left + right) // 2
62+
63+
#Sorts the left halve
64+
yield from merge_sort(arr, left, mid)
65+
66+
#Sorts the right halve
67+
yield from merge_sort(arr, mid + 1, right)
68+
69+
#Merge the halves
70+
yield from merge(arr, left, mid, right)

main.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from algorithms import bubble_sort, selection_sort
1+
from algorithms import bubble_sort, selection_sort, merge, merge_sort
22
import tkinter as tk
33
import random
44

@@ -43,12 +43,21 @@ def start_selection_sort():
4343
color_array[i] = 'blue'
4444
color_array[j] = 'blue'
4545

46+
#Function to Start Sorting using Merge Sort
47+
def start_merge_sort():
48+
global array
49+
for arr, i, j in merge_sort(array, 0, len(array) - 1):
50+
color_array = ["blue"] * len(array)
51+
color_array[i] = "red"
52+
color_array[j] = "red"
53+
draw_array(arr, color_array)
54+
4655
#Randomized Array
4756
array = [random.randint(10, 100) for _ in range(50)]
4857
saved_array = array.copy()
4958

5059
def randomize_array():
51-
global array, color_array
60+
global array, color_array, saved_array
5261
array = [random.randint(10, 100) for _ in range(50)] # Generate new random values
5362
color_array = ['blue' for _ in range(len(array))] # Reset colors
5463
draw_array(array, color_array) # Replot the array
@@ -77,6 +86,10 @@ def unsort_array():
7786
start_selection_button = tk.Button(button_frame, text="Start Selection Sort", command=start_selection_sort)
7887
start_selection_button.pack(side=tk.LEFT, padx=5)
7988

89+
#'Start Selection Sort' Button
90+
start_merge_button = tk.Button(button_frame, text="Start Merge Sort", command=start_merge_sort)
91+
start_merge_button.pack(side=tk.LEFT, padx=5)
92+
8093
#'Unsort' Button
8194
unsort_button = tk.Button(button_frame, text="Unsort", command=unsort_array)
8295
unsort_button.pack(side=tk.LEFT, padx=5)

0 commit comments

Comments
 (0)