[英]I am trying to make a Quicksort with a GUI my code broke
please keep functions the same as this is for a school project where we are supposed to show our understanding of the code.请保持功能与我们应该展示我们对代码的理解的学校项目相同。 I'm not sure what has happened and need a hand fixing it, when clicking "Quick Sort" on the GUI and error pops up saying that "low" has not been defined.
我不确定发生了什么并且需要手动修复它,当单击 GUI 上的“快速排序”并弹出错误说“低”尚未定义时。 Thanks in Advance.
提前致谢。
# This function takes last element as pivot, places
# the pivot element at its correct position in sorted
# array, and places all smaller (smaller than pivot)
# to left of pivot and all greater elements to right
# of pivot
#Add Function
def add():
global label_result
newNumber = int(passwordEntry.get())
arr.append(newNumber)
label_result = tk.Label(master, text=str(arr))
label_result.grid(row=3, column=0, columnspan=2)
def partition(arr, low, high):
i = (low-1) # index of smaller element
pivot = arr[high] # pivot
for j in range(low, high):
# If current element is smaller than or
# equal to pivot
if arr[j] <= pivot:
# increment index of smaller element
i = i+1
arr[i], arr[j] = arr[j], arr[i]
arr[i+1], arr[high] = arr[high], arr[i+1]
return (i+1)
# The main function that implements QuickSort
# arr[] --> Array to be sorted,
# low --> Starting index,
# high --> Ending index
# Function to do Quick sort
def quickSort(arr, low, high):
if len(arr) == 1:
return arr
if low < high:
# pi is partitioning index, arr[p] is now
# at right place
pi = partition(arr, low, high)
# Separately sort elements before
# partition and after partition
quickSort(arr, low, pi-1)
quickSort(arr, pi+1, high)
# Driver code to test above
arr = []
n = len(arr)
quickSort(arr, 0, n-1)
print("Sorted array is:")
for i in range(n):
print("%d" % arr[i]),
master = tk.Tk()
master.title("Dans Quick Sort")
passwordEntry = tk.Entry(master)
passwordEntry.grid(row=0, column=1)
tk.Label(master, text='Enter Number: ', font='bold',).grid(row=0, column=0)
tk.Button(master, text='Add to Array', command=add).grid(row=1, column=0)
#lambda allows you to call a function with parameter.
tk.Button(master, text='Quick Sort', command=lambda: partition(arr, low, high)).grid(row=1, column=1)
label_msg = tk.Label(master, text='')
label_msg.grid(row=2, column=0, columnspan=2)```
You executed partition(...)
in the lambda
which will not do the sorting.您在
lambda
中执行了partition(...)
,它不会进行排序。
Create a function to execute quickSort()
function and show the result in a label:创建一个 function 来执行
quickSort()
function 并在 label 中显示结果:
def add():
newNumber = int(passwordEntry.get())
arr.append(newNumber)
label_msg.config(text=str(arr)) # used label_msg instead of creating new label
...
def doQuickSort(arr):
n = len(arr)
quickSort(arr, 0, n-1)
result.config(text=str(arr)) # update the result label
...
arr = []
tk.Button(master, text='Quick Sort', command=lambda: doQuickSort(arr)).grid(row=1, column=1)
label_msg = tk.Label(master, text='')
label_msg.grid(row=2, column=0, columnspan=2)
# label for the sorting result
result = tk.Label(master)
result.grid(row=3, column=0, columnspan=2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.