使用python编程语言实现Tkinter界面
该界面要有以下功能
(1)从键盘上输入n个长度为m的整数,程序可以输出这些整数的四种排序方式:快速排序,直接选择排序,冒泡排序,归并排序,并在界面显示每种排序的升序和降序以及排序的时间复杂度和空间复杂度;
(2)程序能对所输入的数字进行判断,如果不是所要求的长度,弹出提醒消息框;
(3)输出结果以逗号隔开。
(4)只能使用数据结构的知识实现,不能使用数据库
使用python编程语言实现Tkinter界面
该界面要有以下功能
(1)从键盘上输入n个长度为m的整数,程序可以输出这些整数的四种排序方式:快速排序,直接选择排序,冒泡排序,归并排序,并在界面显示每种排序的升序和降序以及排序的时间复杂度和空间复杂度;
(2)程序能对所输入的数字进行判断,如果不是所要求的长度,弹出提醒消息框;
(3)输出结果以逗号隔开。
(4)只能使用数据结构的知识实现,不能使用数据库
import time
import tkinter as tk
from tkinter import messagebox
class SortingAlgorithms:
def __init__(self):
pass
def bubble_sort(self, arr):
start_time = time.time()
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
end_time = time.time()
time_complexity = end_time - start_time
space_complexity = 1
return arr, time_complexity, space_complexity
def selection_sort(self, arr):
start_time = time.time()
n = len(arr)
for i in range(n):
min_idx = i
for j in range(i+1, n):
if arr[min_idx] > arr[j]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
end_time = time.time()
time_complexity = end_time - start_time
space_complexity = 1
return arr, time_complexity, space_complexity
def quick_sort(self, arr):
start_time = time.time()
def partition(arr, low, high):
i = low - 1
pivot = arr[high]
for j in range(low, high):
if arr[j] <= pivot:
i = i + 1
arr[i], arr[j] = arr[j], arr[i]
arr[i+1], arr[high] = arr[high], arr[i+1]
return i+1
def quick_sort_helper(arr, low, high):
if low < high:
pi = partition(arr, low, high)
quick_sort_helper(arr, low, pi-1)
quick_sort_helper(arr, pi+1, high)
quick_sort_helper(arr, 0, len(arr)-1)
end_time = time.time()
time_complexity = end_time - start_time
space_complexity = len(arr)
return arr, time_complexity, space_complexity
def merge_sort(self, arr):
start_time = time.time()
def merge(arr, l, m, r):
n1 = m - l + 1
n2 = r - m
L = [0] * n1
R = [0] * n2
for i in range(n1):
L[i] = arr[l + i]
for j in range(n2):
R[j] = arr[m + 1 + j]
i = 0
j = 0
k = l
while i < n1 and j < n2:
if L[i] <= R[j]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1
while i < n1:
arr[k] = L[i]
i += 1
k += 1
while j < n2:
arr[k] = R[j]
j += 1
k += 1
def merge_sort_helper(arr, l, r):
if l < r:
m = (l+(r-1)) // 2
merge_sort_helper(arr, l, m)
merge_sort_helper(arr, m+1, r)