在Python中使用 Tkinter 可视化冒泡排序
在本文中,我们将使用Python GUI 库 Tkinter 来可视化冒泡排序算法。
- Tkinter 是一个非常易于使用且对初学者友好的 GUI 库,可用于可视化排序算法。
- 在这里,冒泡排序算法是可视化的,它通过重复交换相邻元素/值(如果它们的顺序错误)来工作,直到对整个数组进行排序。
- 这个想法是:如果 arr[i] > arr[i+1] 然后交换它们。
- 在第一次迭代中,必须扫描 N-1 个项目,并且最大的元素移动到其正确的位置。在第二次迭代中,第二大的项目将移动到正确的位置,第三次迭代后(在项目 N-3 处停止)第三大的项目将就位。因此,要按正确顺序放置所有元素,上述操作将执行 N-1 次。该算法的时间复杂度 = O(N 2 )。
程序:
- 指定范围内的随机值列表生成为条形图。
- 不同的颜色(红色和绿色)用于显示排序过程。
- 创建了一个合适的“速度”范围栏,以方便用户进行可视化。
- “生成”和“开始”按钮分别用于创建数据条和启动排序过程。
冒泡排序的扩展代码:
这是冒泡排序算法的扩展代码,在主 Tkinter 可视化器代码中导入,用于实现冒泡排序算法并返回排序结果。
Python3
# Extension Bubble Sort Code
# importing time module
import time
# function to implement bubble sort by passing
# the following parameters:
# data is passed for the set of unsorted data values
# drawdata is used to generate the data bars
# timer iis for the speed range
def bubble(data, drawData, timer):
n = len(data)
for i in range(n):
for j in range(0, n-i-1):
if data[j] > data[j+1]:
data[j], data[j+1] = data[j+1], data[j]
# if swapped then color becomes Green else stays Red
drawData(data, ['Green' if x == j +
1 else 'Red' for x in range(len(data))])
time.sleep(timer)
# sorted elements generated with Green color
drawData(data, ['Green' for x in range(len(data))])
Python3
# code for Bubble Sort Visualizer using Python and Tkinter
from tkinter import *
from tkinter import ttk
import random
from bub_srt import bubble
# initialising root class for Tkinter
root = Tk()
root.title("Bubble Sort Visualizer")
# maximum window size
root.maxsize(900, 600)
root.config(bg="Black")
select_alg = StringVar()
data = []
# function to generate the data values by accepting a given range
def generate():
global data
# minval : minimum value of the range
minval = int(minEntry.get())
# maxval : maximum value of the range
maxval = int(maxEntry.get())
# sizeval : number of data values/bars to be generated
sizeval = int(sizeEntry.get())
# creating a blank data list which will be further
# filled with random data values
# within the entered range
data = []
for _ in range(sizeval):
data.append(random.randrange(minval, maxval+1))
drawData(data, ['Red' for x in range(len(data))])
# function to create the data bars by creating a canvas in Tkinter
def drawData(data, colorlist):
canvas.delete("all")
can_height = 380
can_width = 550
x_width = can_width/(len(data) + 1)
offset = 30
spacing = 10
# normalizing data for rescaling real-valued numeric data within the
# given range
normalized_data = [i / max(data) for i in data]
for i, height in enumerate(normalized_data):
# top left corner
x0 = i*x_width + offset + spacing
y0 = can_height - height*340
# bottom right corner
x1 = ((i+1)*x_width) + offset
y1 = can_height
# data bars are generated as Red colored vertical rectangles
canvas.create_rectangle(x0, y0, x1, y1, fill=colorlist[i])
canvas.create_text(x0+2, y0, anchor=SE, text=str(data[i]))
root.update_idletasks()
# function to initiate the sorting process by
# calling the extension code
def start_algorithm():
global data
bubble(data, drawData, speedbar.get())
# creating main user interface frame and
# basic layout by creating a frame
Mainframe = Frame(root, width=600, height=200, bg="Grey")
Mainframe.grid(row=0, column=0, padx=10, pady=5)
canvas = Canvas(root, width=600, height=380, bg="Grey")
canvas.grid(row=1, column=0, padx=10, pady=5)
# creating user interface area in grid manner
# first row components
Label(Mainframe, text="ALGORITHM", bg='Grey').grid(
row=0, column=0, padx=5, pady=5, sticky=W)
# algorithm menu for showing the name of the sorting algorithm
algmenu = ttk.Combobox(
Mainframe, textvariable=select_alg, values=["Bubble Sort"])
algmenu.grid(row=0, column=1, padx=5, pady=5)
algmenu.current(0)
# creating Start Button to start the sorting visualization process
Button(Mainframe, text="START", bg="Blue", command=start_algorithm).grid(
row=1, column=3, padx=5, pady=5)
# creating Speed Bar using scale in Tkinter
speedbar = Scale(Mainframe, from_=0.10, to=2.0, length=100, digits=2,
resolution=0.2, orient=HORIZONTAL, label="Select Speed")
speedbar.grid(row=0, column=2, padx=5, pady=5)
# second row components
# sizeEntry : scale to select the size/number of data bars
sizeEntry = Scale(Mainframe, from_=3, to=60, resolution=1,
orient=HORIZONTAL, label="Size")
sizeEntry.grid(row=1, column=0, padx=5, pady=5)
# minEntry : scale to select the minimum value of data bars
minEntry = Scale(Mainframe, from_=0, to=10, resolution=1,
orient=HORIZONTAL, label="Minimum Value")
minEntry.grid(row=1, column=1, padx=5, pady=5)
# maxEntry : scale to select the maximum value of data bars
maxEntry = Scale(Mainframe, from_=10, to=100, resolution=1,
orient=HORIZONTAL, label="Maximum Value")
maxEntry.grid(row=1, column=2, padx=5, pady=5)
# creating generate button
Button(Mainframe, text="Generate", bg="Red", command=generate).grid(
row=0, column=3, padx=5, pady=5)
# to stop automatic window termination
root.mainloop()
Tkinter 代码:
在此代码中,我们将数据值生成为不同长度和特定颜色的条形。基本布局在 Tkinter 'Frame' 中设计,生成条形和算法可视化的部分在 Tkinter 'Canvas' 中设计。
该代码基本上具有以下组件:
- 大型机:一个 Tkinter 框架,以有组织的方式排列所有必要的组件(标签、按钮、速度栏等)
- Canvas : 一个 Tkinter 画布,用作绘制生成的数据条和可视化排序过程的空间
- generate() :通过接受范围然后将其作为参数传递给 drawData()函数来生成数据值的方法
- drawData() : 在画布上为特定颜色的标准化数据值(在给定范围内)生成条形的方法
- start_algorithm() :当按下“START”按钮时调用此函数。它通过调用冒泡排序扩展代码中的冒泡()函数来启动排序过程。
蟒蛇3
# code for Bubble Sort Visualizer using Python and Tkinter
from tkinter import *
from tkinter import ttk
import random
from bub_srt import bubble
# initialising root class for Tkinter
root = Tk()
root.title("Bubble Sort Visualizer")
# maximum window size
root.maxsize(900, 600)
root.config(bg="Black")
select_alg = StringVar()
data = []
# function to generate the data values by accepting a given range
def generate():
global data
# minval : minimum value of the range
minval = int(minEntry.get())
# maxval : maximum value of the range
maxval = int(maxEntry.get())
# sizeval : number of data values/bars to be generated
sizeval = int(sizeEntry.get())
# creating a blank data list which will be further
# filled with random data values
# within the entered range
data = []
for _ in range(sizeval):
data.append(random.randrange(minval, maxval+1))
drawData(data, ['Red' for x in range(len(data))])
# function to create the data bars by creating a canvas in Tkinter
def drawData(data, colorlist):
canvas.delete("all")
can_height = 380
can_width = 550
x_width = can_width/(len(data) + 1)
offset = 30
spacing = 10
# normalizing data for rescaling real-valued numeric data within the
# given range
normalized_data = [i / max(data) for i in data]
for i, height in enumerate(normalized_data):
# top left corner
x0 = i*x_width + offset + spacing
y0 = can_height - height*340
# bottom right corner
x1 = ((i+1)*x_width) + offset
y1 = can_height
# data bars are generated as Red colored vertical rectangles
canvas.create_rectangle(x0, y0, x1, y1, fill=colorlist[i])
canvas.create_text(x0+2, y0, anchor=SE, text=str(data[i]))
root.update_idletasks()
# function to initiate the sorting process by
# calling the extension code
def start_algorithm():
global data
bubble(data, drawData, speedbar.get())
# creating main user interface frame and
# basic layout by creating a frame
Mainframe = Frame(root, width=600, height=200, bg="Grey")
Mainframe.grid(row=0, column=0, padx=10, pady=5)
canvas = Canvas(root, width=600, height=380, bg="Grey")
canvas.grid(row=1, column=0, padx=10, pady=5)
# creating user interface area in grid manner
# first row components
Label(Mainframe, text="ALGORITHM", bg='Grey').grid(
row=0, column=0, padx=5, pady=5, sticky=W)
# algorithm menu for showing the name of the sorting algorithm
algmenu = ttk.Combobox(
Mainframe, textvariable=select_alg, values=["Bubble Sort"])
algmenu.grid(row=0, column=1, padx=5, pady=5)
algmenu.current(0)
# creating Start Button to start the sorting visualization process
Button(Mainframe, text="START", bg="Blue", command=start_algorithm).grid(
row=1, column=3, padx=5, pady=5)
# creating Speed Bar using scale in Tkinter
speedbar = Scale(Mainframe, from_=0.10, to=2.0, length=100, digits=2,
resolution=0.2, orient=HORIZONTAL, label="Select Speed")
speedbar.grid(row=0, column=2, padx=5, pady=5)
# second row components
# sizeEntry : scale to select the size/number of data bars
sizeEntry = Scale(Mainframe, from_=3, to=60, resolution=1,
orient=HORIZONTAL, label="Size")
sizeEntry.grid(row=1, column=0, padx=5, pady=5)
# minEntry : scale to select the minimum value of data bars
minEntry = Scale(Mainframe, from_=0, to=10, resolution=1,
orient=HORIZONTAL, label="Minimum Value")
minEntry.grid(row=1, column=1, padx=5, pady=5)
# maxEntry : scale to select the maximum value of data bars
maxEntry = Scale(Mainframe, from_=10, to=100, resolution=1,
orient=HORIZONTAL, label="Maximum Value")
maxEntry.grid(row=1, column=2, padx=5, pady=5)
# creating generate button
Button(Mainframe, text="Generate", bg="Red", command=generate).grid(
row=0, column=3, padx=5, pady=5)
# to stop automatic window termination
root.mainloop()
输出 :