简体   繁体   English

我正在尝试使用 GUI 进行快速排序,但我的代码已损坏

[英]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.

相关问题 我正在尝试以 OOP 方式制作我的 PyQt GUI 代码 - I'm trying to make my PyQt GUI code in OOP way 我正在尝试通过删除索引来减少我的代码以使其更简单 - I am trying to reduce my code to make it simpler by removing the indexes 我正在尝试使用 tkinter 制作一个 GUI 应用程序 - I am trying to make a GUI application using tkinter 我的代码不起作用我正在尝试制作手动游戏但显示错误 - my code is not working i am trying to make a hand operated game but showing error 我正在尝试为我正在开发的应用程序制作 GUI,但是当我尝试打印全局变量时出现错误。 为什么? - I am trying to make a GUI for a app I am working on but when I try print a global variable I get an error. Why? 我正在尝试在 GUI Python 中显示结果 - I am trying to display a result in a GUI Python 为什么我在尝试使用 tkinter 制作多个 windows gui 时收到错误消息? - Why am I receiving a error when trying to use tkinter to make multiple windows gui? 我正在尝试制作一个简单的出租车费用估算器代码 - I am trying to make a simple taxi fare estimator code 我正在尝试为我的主菜单制作两个按钮 - I am trying to make two buttons for my main menu 我正在尝试在 while 循环中创建一个列表,但我的循环不会重新启动 - I am trying to make a list in a while loop but my loop will not restart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM