Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8f82afd

Browse files
Merge pull request avinashkranjan#1068 from avinashkranjan/avinashkranjan-linear
Visualizer for Linear Search Algorithm
2 parents 6bae4d7 + e3640de commit 8f82afd

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed

‎Linear-Search-Visualizer/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# LINEAR SEARCH VISUALIZER
2+
3+
## Description
4+
This application visualizes the linear search algorithm on a given array of numbers.
5+
It could be helpful in understanding how the algorithm actually works.
6+
7+
## Modules used
8+
- Tkinter
9+
- Random
10+
- Time
11+
12+
## Output
13+
<p align="center">
14+
<img src="https://imgur.com/behB8rV.png">
15+
<br>
16+
<b>Initial window</b>
17+
<br><br>
18+
<img src="https://imgur.com/oYC93Ie.png">
19+
<br>
20+
<b>Key Found</b>
21+
<br><br>
22+
<img src="https://imgur.com/CmkcfQv.png">
23+
<br>
24+
<b>Key not found</b>
25+
<br><br>
26+
</p>
27+
28+
## Author
29+
[Tanvi Bugdani](https://github.com/tanvi355)

‎Linear-Search-Visualizer/script.py

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
#linear search visualizer
2+
import tkinter as tk
3+
from tkinter import *
4+
from tkinter import messagebox
5+
import random
6+
import time
7+
8+
#main window
9+
root = Tk()
10+
root.title('Linear Search Visualizer')
11+
#background color
12+
root.config(bg = "grey")
13+
#disabling resizing of window
14+
root.resizable(0, 0)
15+
16+
#array of elements / rectangle heights
17+
array = []
18+
19+
# ~30 elements fit in the canvas using below function
20+
def drawRect(array, color):
21+
canvas.delete("all")
22+
c_height = 380
23+
c_width = 1000
24+
x_width = c_width / (len(array) + 1)
25+
x_start = 15
26+
spacing = 10
27+
normalizedArray = [ i / max(array) for i in array]
28+
for i, height in enumerate(normalizedArray):
29+
#top left
30+
x0 = i * x_width + x_start + spacing
31+
y0 = c_height - height * 340
32+
#bottom right
33+
x1 = (i + 1) * x_width + x_start
34+
y1 = c_height
35+
36+
canvas.create_rectangle(x0, y0, x1, y1, fill=color[i])
37+
canvas.create_text(x0+2, y0, anchor=SW, text=str(array[i]))
38+
39+
root.update_idletasks()
40+
41+
42+
#generate random elements for the array and draw their rectangles on the canvas
43+
def Generate():
44+
global array
45+
try:
46+
minVal = int(minEntry.get())
47+
maxVal = int(maxEntry.get())
48+
size = int(sizeEntry.get())
49+
except:
50+
messagebox.showwarning("Message", "Please enter all parameters")
51+
52+
array = []
53+
# generating random list
54+
color = []
55+
for _ in range (size):
56+
array.append(random.randrange(minVal, maxVal +1))
57+
color.append('#98AFC7')
58+
59+
drawRect(array, color)
60+
61+
62+
#search for a given key and change colours of the rectangles
63+
def search():
64+
global array
65+
#take input key to search from user
66+
try:
67+
key = int(keyEntry.get())
68+
except:
69+
messagebox.showwarning("Message", "Please enter a key to search")
70+
71+
#setting initial colour for the rectangles
72+
color = ['#98AFC7' for rect in range(len(array))]
73+
flag = False
74+
#elements being checked
75+
for i in range(len(array)):
76+
color[i] = 'white'
77+
drawRect(array, color)
78+
time.sleep(0.2)
79+
#if key is found
80+
if array[i] == key:
81+
color[i] = 'SeaGreen'
82+
drawRect(array, color)
83+
flag = True
84+
time.sleep(0.2)
85+
break
86+
#if key not found move on to check next element
87+
else:
88+
color[i] = 'Salmon'
89+
drawRect(array, color)
90+
time.sleep(0.2)
91+
92+
#display a final message
93+
if flag:
94+
messagebox.showwarning("Success", "Key found")
95+
else:
96+
messagebox.showinfo("Failure", "Key not found")
97+
98+
99+
#---adding frames---
100+
#top name frame
101+
top = Frame(root, width = 1300, height = 200, bg = '#98AFC7', bd = 8, relief = "groove")
102+
top.grid(row = 0, column = 0, padx = 10, pady = 5)
103+
104+
#frame for canvas
105+
canvas = Canvas(root, width=1000, height=380, bg = '#C3FDB8')
106+
canvas.grid(row=1, column=0, padx=10, pady=5)
107+
108+
#frame for user entries
109+
entries = Frame(root, width = 1300, height = 300, bg = '#98AFC7', bd = 8, relief = "groove")
110+
entries.grid(row = 2, column = 0, padx = 10, pady = 5)
111+
112+
113+
#---adding widgets---
114+
#top label
115+
greeting = Label(top, text = "Linear Search Visualizer", width = 62, font = ("Courier New", 20, "bold"), background = "#98AFC7")
116+
greeting.grid(row = 0, column = 1, pady = 5)
117+
118+
119+
#user entries and buttons
120+
#row 0
121+
Size = Label(entries, text = "Size of array : ", bg = '#C3FDB8', relief = "groove")
122+
Size.grid(row = 0, column = 0, padx = 15, pady = 5, sticky = W, ipadx = 20, ipady = 5)
123+
sizeEntry = Entry(entries, justify = "center")
124+
sizeEntry.grid(row = 0, column = 1, padx = 15, pady = 5, sticky = W, ipady = 5)
125+
126+
minn = Label(entries, text = "Minimum element : ", bg = '#C3FDB8', relief = "groove")
127+
minn.grid(row = 0, column = 2, padx = 15, pady = 5, sticky = W, ipadx = 20, ipady = 5)
128+
minEntry = Entry(entries, justify = "center")
129+
minEntry.grid(row = 0, column = 3, padx = 15, pady = 5, sticky = W, ipady = 5)
130+
131+
maxx = Label(entries, text = "Maximum element : ", bg = '#C3FDB8', relief = "groove")
132+
maxx.grid(row = 0, column = 4, padx = 15, pady = 5, sticky = W, ipadx = 20, ipady = 5)
133+
maxEntry = Entry(entries, justify = "center")
134+
maxEntry.grid(row = 0, column = 5, padx = 15, pady = 5, sticky = W, ipady = 5)
135+
136+
#row 1
137+
generate = Button(entries, text = "Generate", bg = '#C3FDB8', command = Generate)
138+
generate.grid(row = 1, column = 1, padx = 15, pady = 5, ipadx = 20, ipady = 5)
139+
140+
keyToSearch = Label(entries, text = "Key : ", bg = '#C3FDB8', relief = "groove")
141+
keyToSearch.grid(row = 1, column = 2, padx = 15, pady = 5, ipadx = 20, ipady = 5)
142+
keyEntry = Entry(entries, justify = "center")
143+
keyEntry.grid(row = 1, column = 3, padx = 15, pady = 5, ipady = 5)
144+
145+
Search = Button(entries, text = "Search", bg = '#C3FDB8', command = search)
146+
Search.grid(row = 1, column = 4, padx = 15, pady = 5, ipadx = 20, ipady = 5)
147+
148+
149+
root.mainloop()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /