简体   繁体   English

快速排序Python工作

[英]Getting a quick sort in Python to work

I have a text file "result.txt". 我有一个文本文件“result.txt”。 The following code opens result.txt and reads its contents into the list "data". 以下代码打开result.txt并将其内容读入列表“data”。 After that, the function "partition" is defined, then the recursive function "quicksort" is defined. 之后,定义函数“partition”,然后定义递归函数“quicksort”。 Finally, the function quicksort is applied to the list data. 最后,函数quicksort应用于列表数据。

    import io

    with io.open('result.txt', encoding='latin-1') as myfile:
        data = myfile.readlines()

    def partition(list, start, end):
        pivot = list[end]                          # Partition around the last value
        bottom = start-1                           # Start outside the area to be partitioned
        top = end                                  # Ditto

        done = 0
        while not done:                            # Until all elements are partitioned...

            while not done:                        # Until we find an out of place element...
                bottom = bottom+1                  # ... move the bottom up.

                if bottom == top:                  # If we hit the top...
                    done = 1                       # ... we are done.
                    break

                if list[bottom] > pivot:           # Is the bottom out of place?
                    list[top] = list[bottom]       # Then put it at the top...
                    break                          # ... and start searching from the top.

            while not done:                        # Until we find an out of place element...
                top = top-1                        # ... move the top down.

                if top == bottom:                  # If we hit the bottom...
                    done = 1                       # ... we are done.
                    break

                if list[top] < pivot:              # Is the top out of place?
                    list[bottom] = list[top]       # Then put it at the bottom...
                    break                          # ...and start searching from the bottom.

        list[top] = pivot                     

     # Put the pivot in its place.
    return top                                 # Return the split point


def quicksort(list, start, end):
    if start < end:                            # If there are two or more elements...
        split = partition(list, start, end)    # ... partition the sublist...
        quicksort(list, start, split-1)

quicksort(data, 0, (int(len(data))-1))

The quick sort fails to sort the data. 快速排序无法对数据进行排序。 Printing data[500500] returns "scheinhei" while printing data[500501] returns "blasende". 打印数据[500500]返回“scheinhei”,而打印数据[500501]返回“blasende”。 It is not alphabetically sorted as I want it to be. 它不按照我想要的字母顺序排序。 The data being sorted includes symbols, numbers and letters. 被排序的数据包括符号,数字和字母。 How can I get this quick sort to work? 我怎样才能快速完成这项工作?

You are almost there, but forgot to sort the second half of the array: 你几乎就在那里,但忘了排序数组的后半部分:

def quicksort(list, start, end):
  if start < end:                            # If there are two or more elements...
    split = partition(list, start, end)    # ... partition the sublist...
    quicksort(list, start, split-1)
    quicksort(list, split+1, end) #we need to sort the second half as well!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM