简体   繁体   English

快速排序 Python 递归 - 递归 function 是如何工作的

[英]Quick Sort Python Recursion - how does the recursive function work

So I understand how the partition works, however, I do not understand how the quicksort function works.所以我了解分区是如何工作的,但是,我不明白快速排序 function 是如何工作的。 This is code I found online however, most versions are pretty similar.这是我在网上找到的代码,但是大多数版本都非常相似。 How does the quicksort function piece together the entire sorted list?快速排序 function 如何将整个排序列表拼凑在一起? I don't understand how the return statement returns a whole sorted list when the partitions are making subsets of the list.我不明白当分区生成列表的子集时,return 语句如何返回整个排序列表。 So shouldn't the return value here be one or two numbers?那么这里的返回值不应该是一两个数字吗?

What I'm looking for is an explanation for how the _quicksort() function runs, step by step.我正在寻找的是关于_quicksort() function 如何逐步运行的解释。 Anyone's help is much appreciated!非常感谢任何人的帮助!

def partition(xs, start, end):
    follower = leader = start
    while leader < end:
        if xs[leader] <= xs[end]:
            xs[follower], xs[leader] = xs[leader], xs[follower]
            follower += 1
        leader += 1
    xs[follower], xs[end] = xs[end], xs[follower]
    return follower

def _quicksort(xs, start, end):
    if start >= end:
        return
    p = partition(xs, start, end)
    _quicksort(xs, start, p-1)
    _quicksort(xs, p+1, end)

def quicksort(xs):
    _quicksort(xs, 0, len(xs)-1)

This is an example of Lomuto partition scheme were partition() separates the input into values <= pivot, pivot, values > pivot.这是 Lomuto 分区方案的一个示例,partition() 将输入分隔为 values <= pivot, pivot, values > pivot。 The result of this is that the values <= pivot will be swapped so they are less than (follower-start) away from their final sorted position, values > pivot will be swapped so they are less than (end-follower) away from their final sorted position, and pivot (xs[end]) will be placed in it's sorted position.这样做的结果是值 <= pivot 将被交换,因此它们与最终排序的 position 的距离小于(跟随开始),值 > pivot 将被交换小于(交换)最终排序的 position 和 pivot (xs[end]) 将放置在排序后的 position 中。 Then it recursively calls itself for the left and right groups.然后它递归地调用左右组。 Each level of recursion for a group puts the elements in that group closer to their final sorted position, and once a base case is reached (0 or 1 elements), that element is in its final sorted position.组的每个递归级别使该组中的元素更接近其最终排序的 position,一旦达到基本情况(0 或 1 个元素),该元素将在其最终排序的 position 中。

You can think of this as the data becomes closer to being sorted with each level of recursion, and once the entire recursion stack tree has been traversed, the array ends up sorted.您可以将其视为随着每个递归级别的数据变得更接近排序,并且一旦遍历整个递归堆栈树,数组最终会排序。

Although quick sort is often called a divide and conquer method, the work done by swapping elements closer to their final sorted position is done before division, and once division reaches a base case of 1 element, that element is now in it's final sorted position, and nothing is done during the returns back up the call chain.尽管快速排序通常被称为分而治之的方法,但通过交换更接近最终排序 position 的元素所做的工作是在除法之前完成的,一旦除法达到 1 个元素的基本情况,该元素现在在它的最终排序 position 中,并且在返回调用链的过程中没有做任何事情。

Take a look at看一眼

https://en.wikipedia.org/wiki/Quicksort#Algorithm https://en.wikipedia.org/wiki/Quicksort#Algorithm

https://en.wikipedia.org/wiki/Quicksort#Lomuto_partition_scheme https://en.wikipedia.org/wiki/Quicksort#Lomuto_partition_scheme

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

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