排序可视化算法
2024-06-26 21:41:52
发布于:广东
__init文件:
import tkinter as tk
import random
import time
import threading
class Visualizer:
def __init__(self, root, canvas_width=1000, canvas_height=600, num_bars=10):
self.root = root
self.canvas_width = canvas_width
self.canvas_height = canvas_height
self.num_bars = num_bars
self.bar_width = canvas_width / num_bars
self.data = [i * (canvas_height - 100) // num_bars for i in range(1, num_bars + 1)]
self.drawing = [None] * num_bars#记录画图
self.all_need = 0#所有需要的
self.ok = 0#已经完成的,这两个用来计算排序完成度
self.label = tk.Label(self.root, text='排序次数:0 完成度:0%')
self.label.pack()
self.very_quick_sort_once()
self.canvas = tk.Canvas(root, width=canvas_width, height=canvas_height)
self.canvas.pack()
#self.draw_data(color = 'white')
def draw_data(self, color, idx = None):#绘图
if self.all_need == 0:
self.label.configure(text='排序次数:0 完成度:100%')
else:
self.label.configure(text='排序次数:' + str(self.ok) + ' 完成度:' + str(round(self.ok / self.all_need * 100, 2)) + '%')
if idx is not None:
for i in idx:
self.canvas.delete(self.drawing[i])#直接修改单个,提高速度
self.drawing[i] = self.canvas.create_rectangle(int(i * self.bar_width),
self.canvas_height - self.data[i],
int((i + 1) * self.bar_width), self.canvas_height,
fill = color)
else:
for i, value in enumerate(self.data):
self.drawing[i] = self.canvas.create_rectangle(int(i * self.bar_width),
self.canvas_height - self.data[i],
int((i + 1) * self.bar_width), self.canvas_height,
fill=color)
self.root.update_idletasks()
def very_quick_sort_once(self):#猴子排序,打乱
for i in range(len(self.data)):
tmp = random.randint(0, len(self.data) - 1)
self.data[i], self.data[tmp] = self.data[tmp], self.data[i]
def __check(self):#检测是否有序
for i, value in enumerate(self.data):
if i != value: return 0
return 1;
def thread_it(func, *args):#阻塞,防止未响应影响观看
t = threading.Thread(target = func, args = args)
t.setDaemon(1)
t.start()
if __name__ == '__main__':
root = tk.Tk()
root.title("排序算法可视化")
n = int(input("请输入数据总数\n"))
app = Visualizer(root, num_bars = n)
root.mainloop()
冒泡:
from __init import *
root = tk.Tk()
root.title("排序算法可视化--冒泡")
n = int(input('请输入数据总数\n'))
app = Visualizer(root, num_bars = n)
tmp = [i for i in app.data]
cnt = 0
def merge(left, mid, right):
i = left
j = mid + 1
global cnt
arr = []
while i <= mid and j <= right:
if tmp[i] <= tmp[j]:
arr.append(tmp[i])
i += 1
else:
arr.append(tmp[j])
j += 1
cnt += mid - i + 1
while i <= mid:
arr.append(tmp[i])
i += 1
while j <= right:
arr.append(tmp[j])
j += 1
for i, val in enumerate(arr):
tmp[i + left] = val
def MergeSort(left, right):#求逆序对
if left >= right: return
mid = (left + right) >> 1
MergeSort(left, mid)
MergeSort(mid + 1, right)
merge(left, mid, right)
MergeSort(0, n - 1)
app.all_need = cnt
app.draw_data(color = 'white')
def BubbleSort():
for i in range(len(app.data)):
flag = 1
if(i != 0): app.draw_data(idx = [len(app.data) - i], color = 'green')
app.draw_data(idx = [len(app.data) - i - 1], color = 'white')
for j in range(len(app.data) - i - 1):
if(j != 0): app.draw_data(idx = [j - 1], color = 'white')
if(app.data[j] > app.data[j + 1]):
app.ok += 1
flag = 0
app.data[j], app.data[j + 1] = app.data[j + 1], app.data[j]
app.draw_data(idx = [j, j + 1], color = 'red')
time.sleep(0.01)
if flag == 1: break
app.draw_data(color = 'green')
tk.Button(root, text = '开始', command = lambda:thread_it(BubbleSort)).pack()
root.mainloop()
选择:
from __init import *
root = tk.Tk()
root.title("排序算法可视化--冒泡")
n = int(input('请输入数据总数\n'))
app = Visualizer(root, num_bars = n)
tmp = [i for i in app.data]
app.all_need = n * (n + 1) // 2
app.draw_data(color = 'white')
def SelectSort():
for i in range(n):
app.draw_data(idx = [i], color = 'green')
idx = i
mx = app.data[i]
for j in range(i, n):
app.ok += 1
app.draw_data(idx=[i], color='green')
if(j > i): app.draw_data(idx = [j], color = 'red')
if(j > i + 1): app.draw_data(idx = [j - 1], color = 'white')
app.draw_data(idx=[idx], color='blue')
if(app.data[j] < mx):
app.draw_data(idx = [idx], color = 'white')
mx = app.data[j]
idx = j
app.draw_data(idx=[i], color='blue')
app.data[i], app.data[idx] = app.data[idx], app.data[i]
app.draw_data(idx=[i, idx, n - 1], color='white')
tk.Button(root, text = '开始', command = lambda:thread_it(SelectSort)).pack()
root.mainloop()
全部评论 3
为什么不是广东省教育局局长?
2024-06-27 来自 上海
0(doge)
2024-06-27 来自 上海
0不对,应该是教育厅厅长(
2024-06-27 来自 上海
0
?不是哥们选择完成度怎么能无限叠
2024-06-26 来自 广东
06
2024-06-26 来自 广东
0
有帮助,赞一个