|
1 | | -from algorithms import bubble_sort, selection_sort |
| 1 | +from algorithms import bubble_sort, selection_sort, merge, merge_sort |
2 | 2 | import tkinter as tk |
3 | 3 | import random |
4 | 4 |
|
@@ -43,12 +43,21 @@ def start_selection_sort(): |
43 | 43 | color_array[i] = 'blue' |
44 | 44 | color_array[j] = 'blue' |
45 | 45 |
|
| 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 | + |
46 | 55 | #Randomized Array |
47 | 56 | array = [random.randint(10, 100) for _ in range(50)] |
48 | 57 | saved_array = array.copy() |
49 | 58 |
|
50 | 59 | def randomize_array(): |
51 | | - global array, color_array |
| 60 | + global array, color_array, saved_array |
52 | 61 | array = [random.randint(10, 100) for _ in range(50)] # Generate new random values |
53 | 62 | color_array = ['blue' for _ in range(len(array))] # Reset colors |
54 | 63 | draw_array(array, color_array) # Replot the array |
@@ -77,6 +86,10 @@ def unsort_array(): |
77 | 86 | start_selection_button = tk.Button(button_frame, text="Start Selection Sort", command=start_selection_sort) |
78 | 87 | start_selection_button.pack(side=tk.LEFT, padx=5) |
79 | 88 |
|
| 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 | + |
80 | 93 | #'Unsort' Button |
81 | 94 | unsort_button = tk.Button(button_frame, text="Unsort", command=unsort_array) |
82 | 95 | unsort_button.pack(side=tk.LEFT, padx=5) |
|
0 commit comments