使用Python进行快速排序

使用Python进行快速排序,python,sorting,Python,Sorting,我有一个文本文件“result.txt”。下面的代码打开result.txt并将其内容读取到列表“data”中。然后,定义函数“partition”,然后定义递归函数“quicksort”。最后,函数quicksort应用于列表数据 import io with io.open('result.txt', encoding='latin-1') as myfile: data = myfile.readlines() def partition(lis

我有一个文本文件“result.txt”。下面的代码打开result.txt并将其内容读取到列表“data”中。然后,定义函数“partition”,然后定义递归函数“quicksort”。最后,函数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))
导入io
将io.open('result.txt',encoding='latin-1')作为myfile:
data=myfile.readlines()
def分区(列表、开始、结束):
pivot=list[end]#围绕最后一个值进行分区
底部=开始-1#在要分区的区域外开始
顶部=末端#同上
完成=0
未完成时:#直到所有元素都已分区。。。
虽然没有完成:#直到我们找到一个不合适的元素。。。
底部=底部+1#。。。将底部向上移动。
如果底部=顶部:#如果我们到达顶部。。。
完成=1#。。。我们完了。
打破
如果list[bottom]>pivot:#底部是否不合适?
列表[顶部]=列表[底部]#然后将其放在顶部。。。
打断。。。从顶部开始搜索。
虽然没有完成:#直到我们找到一个不合适的元素。。。
top=top-1#。。。将顶部向下移动。
如果顶部=底部:#如果我们触底。。。
完成=1#。。。我们完了。
打破
如果列表[顶部]

快速排序无法对数据进行排序。打印数据[500500]返回“scheinhei”,而打印数据[500501]返回“blasende”。它不是按我想要的字母顺序排列的。正在排序的数据包括符号、数字和字母。我如何才能让这种快速排序工作?

您就快到了,但忘记了对数组的后半部分进行排序:

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!
def快速排序(列表、开始、结束):
如果开始<结束:#如果有两个或更多元素。。。
分割=分区(列表、开始、结束)#。。。分区子列表。。。
快速排序(列表、开始、拆分-1)
快速排序(列表,拆分+1,结束)#我们还需要对下半部分进行排序!

您做了哪些调试工作?例如,使用2个字符的字符串进行测试?单独测试
部分
fn?不要命名变量
列表
!与任何问题都完全无关,但当你对内置名称进行阴影处理时,你只是在为自己设置混乱的错误。我没有时间深入研究算法,但我保证你不会尝试执行
list[top]=list[bottom]
。你想交换这两个价值观吗?@ATLUS这太荒谬了。他不是问如何对列表排序,而是问如何实现快速排序。如果你想滚动,你不需要重新发明轮子,但是你需要重新发明轮子来学习向心运动……你重新发明轮子是因为你正在学习轮子的重新发明101。