简体   繁体   English

谁能告诉我快速排序代码中的错误是什么

[英]can anyone tell what's the bug in my quick sort code

can anyone tell what's the bug in my quick sort algorithm? 谁能说出我的快速排序算法中的错误? I am using two point 'left' and 'right' to compare with the pivot, and swap nums[left] and nums[right] if when nums[left] > nums[right]. 我使用两点“左”和“右”与数据透视表进行比较,并且当nums [left]> nums [right]时交换nums [left]和nums [right]。 when left index bigger than right index, break and swap nums[left] and nums[piovt],return left index. 当左索引大于右索引时,中断并交换nums [left]和nums [piovt],返回左索引。

nums = [3,2,3,1,2,4,5,5,6]
n = len(nums)

def partition(nums,left,right,pivot):
    while left<right:
        if left<right and nums[left]<=nums[pivot]:
            left += 1
        if left<right and nums[right]>=nums[pivot]:
            right -= 1
        elif nums[left]>nums[right]:
            nums[left],nums[right] = nums[right],nums[left]
            left += 1
            right -= 1
    nums[left],nums[pivot] = nums[pivot],nums[left]
    return left

def quicksort(nums,low,high,pivot):
    if low<high:
        pos = partition(nums,low,high,pivot)
        quicksort(nums,low,pos-2,pos-1)
        quicksort(nums,pos+1,high-1,high)
    return nums

quicksort(nums,0,n-2,n-1)

print(nums)

ans: [1, 2, 2, 3, 3, 5, 5, 6, 4] 回答:[1、2、2、3、3、5、5、6、4]

若要获取数组的中间索引,应将quicksort的轴设置为整数n / 2。

Here are several bugs in your code, and I have rewrite the code for you. 这是您代码中的几个错误,我已经为您重写了代码。 You can refer to the comment and run some test to figure out. 您可以参考注释并进行一些测试以找出答案。

import random

# two pointers solution, choose the last one as pivot
def partition(nums, left, right, pivot):
    while left < right:
        # here should change < to <=, because if pivot is larger than all in nums[left:right+1], should swap nums[left] with nums[pivot]
        # here I change if to while, because you should traverse the pointer to find the place which is invalid in partition
        while left <= right and nums[left] <= nums[pivot]:
            left += 1
        # here I change if to while, same reason above
        while left < right and nums[right] > nums[pivot]:
            right -= 1
        # you should not use elif here, because you have two ifs, the first if does not work here
        if left < right:
            nums[left], nums[right] = nums[right], nums[left]
    nums[left], nums[pivot] = nums[pivot], nums[left]
    return left


def quicksort(nums, low, high, pivot):
    if low < high:
        pos = partition(nums, low, high, pivot)
        quicksort(nums, low, pos - 2, pos - 1)
        # here is another problem: notice that nums[pivot] is the last element, not the nums[high]
        quicksort(nums, pos + 1, high, high + 1)
    return nums


if __name__ == '__main__':
    for _ in range(100):
        nums = [random.randrange(1, 100) for _ in range(10000)]
        n = len(nums)
        if sorted(nums) != quicksort(nums, 0, n - 2, n - 1):
            print('incorrect')
    print('success')

I have tried my best to help you, and hope you like it. 我已尽力帮助您,希望您喜欢它。

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

相关问题 谁能告诉我这里的代码有什么问题? - Can anyone tell me what's wrong with my code here? 谁能告诉我Python中的合并排序有什么问题吗? - Can anyone tell me what's wrong with my Merge Sort in Python? 谁能告诉我我的功能出了什么问题? - Can anyone tell me what's wrong with my function? 有人可以告诉我我的 animation 代码有什么问题吗? - Can someone tell me what's wrong with my animation code? 我的快速排序算法实现中的错误 - Bug in my quick sort algorithm implementation 谁能告诉我这个 CNN 代码中的错误是什么? - Can anyone tell me what is the error in this CNN code? 谁能告诉我如何在我的代码中增加蛇的大小? - Can anyone tell me how to increase the snake size in my code? 谁能告诉我我的 dequeue function 有什么问题,它不会在运行后从队列或 output 中删除任何内容 - Can anyone tell me what's wrong with my dequeue function, it wont remove anything from the queue or output after running it 有人可以告诉我合并排序的代码有什么问题吗? - Can some tell me whats wrong with my code for merge sort? 谁能帮我找出问题所在? 我的代码中出现索引错误 - Can anyone help me find out what's wrong? Index error in my code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM