Skip to content

Commit 81f012c

Browse files
Merge pull request #1 from reshmaharidhas/v1.1.0
Included 'Dutch national flag' algorithm
2 parents a10634b + 6dc813c commit 81f012c

File tree

1 file changed

+95
-1
lines changed

1 file changed

+95
-1
lines changed

sorting_visualizer.py

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,98 @@ def draw_lines(arr):
6767
line_value_objects.append(line_value)
6868
last_x += 23
6969

70+
def draw_lines_dutch_national_flag():
71+
global line_objects, canvas1, bar_color, canvas_bg_color, line_value_objects
72+
# Delete all previous lines from the canvas.
73+
canvas1.delete('all')
74+
line_objects = []
75+
line_value_objects = []
76+
status_label.config(text="Idle")
77+
dutch_flag_colors = ["red","white","blue"]
78+
canvas3.config(background="#000000")
79+
canvas1.config(background="#000000")
80+
arr = []
81+
for ptr in range(0,56):
82+
arr.append(random.randrange(0,3))
83+
# Shuffle the given array 'arr'.
84+
random.shuffle(arr)
85+
last_x = 14
86+
# Create lines of width 15 on canvas 'canvas1' along with height of that line as text displaying below the line.
87+
for num in range(len(arr)):
88+
# Create line of width 15 with color 'bar_color'.
89+
line1 = canvas1.create_line(last_x, 0, last_x, 300, fill=dutch_flag_colors[arr[num]], width=15)
90+
# Insert the created line's object to a list 'line_objects'.
91+
line_objects.append(line1)
92+
# Create a text on the canvas 'canvas1' which is actually the value of current array element from array 'arr'.
93+
# It denotes the height of the line 'line1'.
94+
line_value = canvas1.create_text(last_x, 300 + 7, text=str(arr[num]), fill=dutch_flag_colors[arr[num]])
95+
# Insert the text's object in a list 'line_value_objects'.
96+
line_value_objects.append(line_value)
97+
last_x += 23
98+
return arr
99+
def exchange_dutch_national_flag(a,b):
100+
global canvas1,line_objects,animation_speed,line_value_objects
101+
play_sound()
102+
height1 = canvas1.coords(line_objects[a])[3]
103+
height2 = canvas1.coords(line_objects[b])[3]
104+
x1 = canvas1.coords(line_objects[a])[0]
105+
x2 = canvas1.coords(line_objects[b])[0]
106+
diff = abs(x2-x1)
107+
# Move the line bar at index 'a' towards right side by 'diff'.
108+
canvas1.move(line_objects[a],diff,0)
109+
# Move the text of the line bar at index 'a' towards right side by 'diff'.
110+
canvas1.move(line_value_objects[a],diff,0)
111+
# Move the line bar at index 'b' towards left side by 'diff'.
112+
canvas1.move(line_objects[b],-diff,0)
113+
# Move the text of the line bar at index 'b' towards left side by 'diff'.
114+
canvas1.move(line_value_objects[b],-diff,0)
115+
# Swapping lines in list 'line_objects', and the text of line bars in list 'line_value_objects'.
116+
line_objects[a],line_objects[b] = line_objects[b],line_objects[a]
117+
line_value_objects[a],line_value_objects[b] = line_value_objects[b],line_value_objects[a]
118+
root.update()
119+
time.sleep(animation_speed)
120+
def dutch_national_flag_algorithm():
121+
arr2 = draw_lines_dutch_national_flag()
122+
time.sleep(1)
123+
# Disable all sorting buttons.
124+
disable_all_buttons()
125+
# Reset the timer text to 0.00
126+
time_label.config(text="0.00 seconds")
127+
# Show status
128+
status_label.config(text="Running Dutch National flag algorithm..")
129+
# Store the current time in variable 'starting_time' as beginning time of bubble sort.
130+
starting_time = calculate_run_start_time()
131+
lo = 0
132+
hi = len(arr2)-1
133+
mid = 0
134+
# Iterate till all the elements
135+
# are sorted
136+
while mid <= hi:
137+
# If the element is 0
138+
if arr2[mid] == 0:
139+
exchange_dutch_national_flag(lo,mid)
140+
arr2[lo], arr2[mid] = arr2[mid], arr2[lo]
141+
lo = lo + 1
142+
mid = mid + 1
143+
# If the element is 1
144+
elif arr2[mid] == 1:
145+
mid = mid + 1
146+
# If the element is 2
147+
else:
148+
exchange_dutch_national_flag(mid,hi)
149+
arr2[mid], arr2[hi] = arr2[hi], arr2[mid]
150+
hi = hi - 1
151+
# Calculate current time because DNF sort completed now.
152+
ending_time = calculate_run_end_time()
153+
# Calculate the total time of execution of DNF sort.
154+
total_execution_time = find_total_time_to_sort(starting_time, ending_time)
155+
# Display the total time of execution.
156+
time_label.config(text=str(total_execution_time) + " seconds")
157+
# Change status_label text
158+
status_label.config(text="Dutch National flag algorithm completed sorting")
159+
# Activate all the sorting buttons.
160+
activate_all_buttons()
161+
70162
# Function to perform bubble sort on given list 'arr'.
71163
def bubble_sort(arr):
72164
# Disable all sorting buttons.
@@ -420,7 +512,7 @@ def add_max_value_of_array():
420512

421513
# Function to show app about section.
422514
def show_about():
423-
messagebox.showinfo("About Sorting Visualizer","Version:1.0\nDate of release:20th June 2024\nOS: Windows 10 or later\nDeveloper: Reshma Haridhas")
515+
messagebox.showinfo("About Sorting Visualizer","Version:1.1\nDate of release:3rd July 2024\nOS: Windows 10 or later\nDeveloper: Reshma Haridhas")
424516

425517
# GUI
426518
root = tk.Tk()
@@ -512,6 +604,8 @@ def show_about():
512604
btn_shaker_sort.grid(row=0,column=6,padx=5)
513605
btn_oddeven_sort = tk.Button(btn_frame,text="Odd even sort",command=lambda :odd_even_sort(arr),bg="brown",activebackground="brown",fg="white",font=("Arial",15))
514606
btn_oddeven_sort.grid(row=0,column=7,padx=4)
607+
btn_dutch_national_flag_sort = tk.Button(btn_frame,text="DNF sort",command=dutch_national_flag_algorithm,font=("Arial",15),fg="blue",bg="#ffffff")
608+
btn_dutch_national_flag_sort.grid(row=0,column=8,padx=4)
515609
# array of numbers
516610
arr = [64,300,100,200,150,230,50,20,77,81,90,34,44,55,13,10,130,585,444,100,270,224,68,73,85,97,250,330,210,110,125,30,40,60,600,500,530,520,93,420,400,360,340,450,550,476,170,181,190,144,140,570,504,34,122,222]
517611
# list to hold bar (lines) in canvas 'canvas1'.

0 commit comments

Comments
 (0)