简体   繁体   English

Python中快速排序算法的解释

[英]Explanation about quicksort algorithm in Python

I am following the lines of code for quicksort algorithm lesson, with these lines of codes:我正在关注快速排序算法课程的代码行,其中包含以下代码行:

def quicksort(array):
    if len(array) < 2:
        return array
    else:
        pivot = array[0] #Recursive case
        less = [i for i in array[1:] if i <= pivot] #Sub-array of all elements < pivot
        greater = [i for i in array[1:] if i > pivot] #sub array of all elements > pivot
        return quicksort(less) + [pivot] + quicksort(greater)

print(quicksort([1,15,7,3,9]))

I run the code successfully if the pivot is set on array[0] , but fails when I change to other number, say, 2,3.如果枢轴设置在array[0] ,我会成功运行代码,但是当我更改为其他数字时失败,例如 2,3。 The error message is list index out of range .错误消息是list index out of range

Now I am confused how can it be out of range if there is 5 instances in the list?现在我很困惑,如果列表中有 5 个实例,它怎么会超出范围? And why is the code not working if I change the pivot to other array positions instead of [0]?如果我将枢轴更改为其他数组位置而不是 [0],为什么代码不起作用? I thought that the pivot can be set to any other element position to speed up the process?我认为可以将枢轴设置为任何其他元素位置以加快过程? Thanks!谢谢!

The code will be called recursively, using smaller subsets of the array.代码将使用数组的较小子集递归调用。 The smaller subsets can have 2 elements, in which case, accessing to array[2] will lead to index out of range .较小的子集可以有 2 个元素,在这种情况下,访问array[2]将导致index out of range

For example, in the above test, if you use array[2] as pivot, the code pivot by at array[2]=7 and recursively call quicksort([7,3]) and quicksort([15,9]) .例如,在上面的测试中,如果您使用array[2]作为主元,则代码中枢轴为array[2]=7并递归调用quicksort([7,3])quicksort([15,9]) If you use array[2] as pivot for both of these subarray, definitely it will be out of range.如果您使用array[2]作为这两个子array[2]枢轴,它肯定会超出范围。 (see the code and output below). (请参阅下面的代码和输出)。

Apart from the list index out of range error.除了list index out of range错误。 I would like to raise 2 comments on your implementation:我想就您的实施提出 2 条意见:

  • If using pivot other than array[0] , make sure to adjust the set array[1:] accordingly (it should be array[:p] + array[p+1:] )如果使用除array[0]以外的枢轴,请确保相应地调整 set array[1:] (它应该是array[:p] + array[p+1:]
  • One advantage of quicksort over other sorting algorithm (eg mergesort) is that it can be implemented in-place and require O(log N) extra memory.快速排序相对于其他排序算法(例如归并排序)的一个优点是它可以就地实现并且需要 O(log N) 额外内存。 The use of less and greater kind of defeat this advantage.使用lessgreater那种失败的这一优势。

Code example:代码示例:

def quicksort(array):
    print(array)
    if len(array) < 2:
        return array
    else:
        pivot = array[2] #Recursive case
        less = [i for i in array[1:] if i <= pivot] #Sub-array of all elements < pivot
        greater = [i for i in array[1:] if i > pivot] #sub array of all elements > pivot
        return quicksort(less) + [pivot] + quicksort(greater)

print(quicksort([1,15,7,3,9]))

Output:输出:

[1, 15, 7, 3, 9]
[7, 3]
IndexError: list index out of range

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

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