简体   繁体   English

QuickSort Python就地索引超出范围错误

[英]QuickSort Python In-place index out of bounds error

Just writing a quickSort algorithm and I can't seem to break free from either an index out of bounds error or an infinite loop. 只是编写一个quickSort算法,我似乎无法摆脱索引超出范围错误或无限循环的麻烦。 Can someone point me in the right direction? 有人能指出我正确的方向吗? Thanks in advance! 提前致谢!

----------- Quick Sort -------------- -----------快速排序--------------

def quickSort(lst):

    if len(lst) <= 1:
        return lst

    while len(lst) > 1:
        pivot = part(lst)
        quickSort(lst[ :pivot])
        quickSort(lst[pivot+1: ])
        return lst

def part(a):
    x = a[0]
    i = 1
    j = len(a)-1

    while (i <= j):
        while (i <= j) and (a[j] >= x):
            j -= 1
        while (i <= j) and (a[i] < x):
            i += 1
        if (i <= j) and a[i] > a[j]:
            temp = a[i]
            a[i] = a[j]
            a[j] = temp

    a[i], a[0] = a[0], a[i]     
    return i
while (i <= j) and (a[i] < x):
            i += 1

When i = j , and j being set to the len - 1 , the i+=1 will cause it to go out of bounds. i = jj设置为len - 1i+=1将使其超出范围。

Before jump and debugging your code, you have to understand the idea behind quicksort. 在跳转和调试代码之前,您必须了解quicksort背后的思想。 It is easy to understand what cause your "code error", IndexOutOfBound , but not so for "Logic error" 很容易理解是什么原因导致“代码错误” IndexOutOfBound ,但对于“逻辑错误”却并非如此

Quicksort 快速排序

The key of Quick Sort algorithm is the partitioning. 快速排序算法的关键是分区。 In partitioning, you usually take one pivot as your comparison point. 在分区中,通常将一个支点作为比较点。 In your case you choose index 0 as your pivot. 在您的情况下,您选择索引0作为枢轴。 Now imagine you have 3 logical partition in the array. 现在假设您在阵列中有3个逻辑分区。

  • Pivot: Any number ( index: 0 ) 枢轴:任何数字( index: 0
  • Partition 1: Any numbers smaller than the pivot. 分区1:任何小于枢轴的数字。 ( index: 1..i ) index: 1..i
  • Partition 2: Any numbers larger than the pivot. 分区2:大于枢轴的任何数字。 ( index: i..j ) index: i..j
  • Partition 3: The Unsorted numbers ( index: j..n-1 ) 分区3:未index: j..n-1数字( index: j..n-1

Your aim now is to take each of the numbers from Partition 3 into Partition 1 or 2. Then because Partition 3 will be emptied and we left with 2 partition. 您现在的目标是将分区3中的每个数字都放入分区1或2中。然后,由于分区3将被清空,因此我们剩下2个分区。 Swap the pivot with last element of partition 1. Then you have the following Partition 1 < Pivot < Partition 2 将数据透视图与分区1的最后一个元素交换。然后,您具有以下Partition 1 < Pivot < Partition 2

Next we deal with the Divide and Conquer technique in the Quicksort method.. In this method, you don't have to iteratively partition. 接下来,我们在Quicksort方法中处理“分而治之”技术。在这种方法中,您不必迭代分区。 Instead you just need to recursively call the Quicksort again for the each of the two new partitions which has been split by the pivot in the middle. 取而代之的是,您只需要再次递归调用两个新分区中的每个分区的Quicksort,该分区已被中间的枢轴拆分了。

The reason why this quicksort can sort by only partitioning into two parts is because each time they partition, the pivot is actually in the sorted index of the array. 这种快速排序只能通过分为两部分进行排序的原因是,每当它们进行分区时,枢轴实际上就在数组的排序索引中。

There are many implementation of Quicksort in the web. Web上有许多Quicksort的实现。 Don't get confused by looking them. 不要因为看它们而感到困惑。 Basically the idea is the same. 基本上,想法是一样的。 (2 Partition with the pivot sorted). (2个具有枢轴排序的分区)。 Some actually implement the partition from left to right, while others from right to left. 有些实际上是从左到右实现分区,而有些实际上是从右到左。 In your case, You seems to mix the two direction together. 在您的情况下,您似乎将两个方向混合在一起。 Such that i goes to the right, and j goes to the left. 这样i走到右边,而j走到左边。 So when doing so, you just have to be careful of this boundary, especially during the swapping. 因此,在执行此操作时,只需注意此边界,尤其是在交换期间。

In the past, I wrote Quicksort algorithm with one iterative loop in the partition methods (because it is much easier to understand when dealing with edge cases) and two recursive call in the quicksort algorithm. 过去,我写了Quicksort算法,在分区方法中有一个迭代循环(因为在处理边缘情况时更容易理解),在quicksort算法中有两个递归调用。

Hope that helps you. 希望对你有所帮助。

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

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