简体   繁体   English

如何在快速排序算法中对负数进行排序?

[英]How to sort negative numbers in the quick sort algorithm?

I am trying to use the quick sort algorithm to sort a random of list of any numbers, but do not know how to sort negative numbers as well, which part of the code should I work on? 我正在尝试使用快速排序算法对任意数字列表进行随机排序,但是不知道如何对负数进行排序,我应该在代码的哪一部分上工作?

Didn't know what to do, commenting out the change of code will help out a lot. 不知道该怎么办,注释掉代码的更改会很有帮助。

Expected Results: 预期成绩:

>>> quickSort([3,5,-3,-1,1,2,4])
[-3, -1, 1, 2, 3, 4, 5]

Actual Results: 实际结果:

>>> quickSort([3,5,-3,-1,1,2,4])
[1, 2, -3, -1, 3, 4, 5]
def quickSort(numList):

    n=len(numList)
    if n<=1:
        return numList
    x, left, right = numList[0], 0, n-1
    while left<right:
        if numList[left]<=x:    
            left+=1
        else:
            numList[left], numList[right]  =  numList[right], numList[left]
            right -=1
    numList[0], numList[left] = numList[left], numList[0]
    quickSort(numList[:left])
    quickSort(numList[left+1:])
    return numList

The unexpected result is not caused by negative number, but several bugs in your quick sort algorithm. 意外的结果不是由负数引起的,而是快速排序算法中的多个错误。 I have fixed them just based on your version, although it is not the best version to implement. 我已经根据您的版本对其进行了修复,尽管它不是最佳的实施版本。 you can compare revised code and read the comment to understand. 您可以比较修改后的代码并阅读评论以了解。

A fatal bug I want to point out is, numList[:left] will slice and generate a new array, it will not effect origin array when you sort on it. 我要指出的一个致命错误是, numList[:left]会切片并生成一个新数组,对它进行排序时不会影响原点数组。 So you should pass the array , and left , right index to quickSort function, not slice. 因此,您应该将arrayleftright索引传递给quickSort函数,而不是slice。

def quickSort(numList, left, right):
    # change check condition to left < right
    # n = len(numList)
    # if n <= 1:
    #     return numList
    if left < right:
        # copy left, right, it will used later
        low, high = left, right

        # it is better to abstract this block to a new function, like partition
        # pick a pivot number
        x = numList[left]
        while left < right:
            # you should use two-pointer to swap
            while left < right and numList[right] >= x:
                right -= 1
            numList[left] = numList[right]

            while left < right and numList[left] <= x:
                left += 1
            numList[right] = numList[left]
            # if numList[left] <= x:
            #     left += 1
            # else:
            #     numList[left], numList[right] = numList[right], numList[left]
            #     right -= 1

        # assign back the pivot number
        numList[left] = x
        # numList[0], numList[left] = numList[left], numList[0]

        # use origin arr and index, not slice 
        quickSort(numList, low, left-1)
        quickSort(numList, left+1, high)
        # quickSort(numList[:left])
        # quickSort(numList[left + 1:])
    return numList

test and output 测试输出

arr = [3, 5, -3, -1, 1, 2, 4]
print(quickSort(arr, 0, len(arr)-1))
# [-3, -1, 1, 2, 3, 4, 5]

Hope that will help you, and comment if you have further questions. 希望对您有所帮助,如有其他问题,请发表评论。 : ) :)

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

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