简体   繁体   English

C ++中的QuickSort无法完成排序

[英]QuickSort in C++ will not finish sorting

Could someone please help me and tell me why quick sort algorithm will not sort the final element? 有人可以帮我,告诉我为什么快速排序算法不能对最终元素进行排序吗? When I input: 6 89 2 78 12 19 99 43 7 63 I get an ALMOST sorted: 2 6 7 12 78 19 43 99 63 89 当我输入时:6 89 2 78 12 19 99 43 7 63我得到了排序的ALMOST:2 6 7 12 78 19 43 99 63 89

I have tried to figure this out my self, but when I work my way thought the code at some point I get lost doing a desk check. 我试图弄清楚自己的意思,但是当我以自己的方式工作时,以为代码在某个时候会迷失于进行桌面检查。

#include <iostream>
using namespace std;

// function prototypes
void quickSort(int arrayList[], int left, int right);
int choosePivot(int arrayList[], int listSize);
int partition(int array[], int left, int right);
void printArray(int theArray[], int Size);
// void swap(int value1, int value2);

int main()
{
    int myList[] = {6, 89, 2, 78, 12, 19, 99, 43, 7, 63};
    printArray(myList, 10);
    quickSort(myList, 0, 9);
    printArray(myList, 10);

    //int myList2[] = { 7, 4, 9, 10, -9 };
    //printArray(myList2, 5);
    //quickSort(myList2, 0, 5);
    //printArray(myList2, 5);


    cin;
    getchar;
    getchar;

    return 0;
}


void quickSort(int arrayList[], int left, int right)
{
    //if (left < right)
    if(right > left)
    {
        int p = partition(arrayList, left, right);

        printArray(arrayList, 10);
        quickSort(arrayList, left, p-1);
        quickSort(arrayList, p + 1, right);
    }
}

// left (index value) - left most part of the array we are working on
// right (index value) - right most part of the array we are working on
int partition(int array[], int left, int right)
{
    //int pivot = array[left]; // I will have to write a function to find the
    //  optimum pivot point, this is the naive solution
    int pivot = array[(left+right)/2];


    int i = left;
    int j = right;
    int temp;
    while (i < j)
    {
        //cout << "in the loop" ;
        while (array[i] < pivot)
            i++;
        while (array[j] > pivot)
            j--;
        if (i < j)
        {
            temp = array[i];
            array[i] = array[j];
            array[j] = temp;
            i++;
            j--;
        }
    }

    return i;
}

void printArray(int theArray[], int Size)
{
    for (int i = 0; i < Size; i++)
    {
        cout << theArray[i] << " ";
    }
    cout << endl ;
}

You have a bug in your partition() function (I take it that it's Hoare partition algorithm implementation). 您的partition()函数中存在一个错误(我认为这是Hoare分区算法的实现)。 You just need to remove this code: 您只需要删除以下代码:

        i++;
        j--;

after swapping values. 交换值后。

Here is the corrected partition() function code: 这是更正后的partition()函数代码:

int partition(int array[], int left, int right)
{
    int pivot = array[(left + right) / 2];
    int i = left;
    int j = right;

    while (i < j) {
        while (array[i] < pivot)
            i++;
        while (array[j] > pivot)
            j--;
        if (i < j) {
            int temp;

            /* Swap array[i] and array[j] */
            temp = array[i];
            array[i] = array[j];
            array[j] = temp;
        }
    }

    return i;
}

Even with this bug fixed, you should create a good unit test for your quicksort implementation, because there may be some other subtle bugs (it's very hard to write bug-less implementation of partition() from scratch). 即使已修复此错误,也应为您的quicksort实现创建一个好的单元测试,因为可能还会存在其他一些细微的错误(很难从头开始编写没有bug的partition()实现)。 Check for edge cases, corner cases and boundary cases in your unit test . 单元测试中检查边缘情况,拐角情况和边界情况

I'm not sure how much this would help but when you get your pivot of 我不确定这有多大帮助,但是当您掌握

(left + right) / 2

you are doing (0+9) / 2 which is 4. But the thing is, the middle should be 5 if the size of the array is 10. 您正在执行(0 + 9)/ 2,即4。但是事实是,如果数组的大小为10,则中间应该为5。

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

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