QuickSort代码无法正常工作

时间:2014-11-13 05:11:58

标签: c++ quicksort

int main()
    {
        int numbers[size] = {14, 7, 23, 31, 1, 20, 6, 3, 40, 5}, start, finish;

        cout << "Numbers are: " << endl;
        for (int i = 0; i < size; i++)
        {
            cout << numbers[i] << "\t";
        }

        finish = 10;
        start = 0;

        quickSort(numbers, start, finish);

        cout << "\n\nSorted: " << endl;
        for (int j = 0; j < size; j++)
        {
            cout << numbers[j] << "\t";
        }

        return 0;
    }

int Partition(int numbers[], int start, int finish)
{

    int pole = start;
    int pivot = numbers[finish];

    for (int k = 0; k < finish - 1; k++)
    {
        if (numbers[k] <= pivot)
        {
            int temp = numbers[k];
            numbers[k] = numbers[pole];
            numbers[pole] = temp;

            pole++;
        }
    }

    int temp2 = numbers[pole];
    numbers[pole] = numbers[pivot];
    numbers[pivot] = temp2;

    return pole;
}

void quickSort(int numbers[], int start, int finish)
{
    int marker;
    if (start < finish)
    {
        marker = Partition(numbers, start, finish);
        quickSort(numbers, start, marker - 1);
        quickSort(numbers, marker + 1, finish);
    }
}

我觉得我错过了什么,但我无法弄清楚是什么。该程序没有正确排序。需要一些帮助!我尝试在过程中间小心地发现问题所在,从我的理解,它并没有完全排序。好吧,我不确定自己。我仍然是递归的新手,所以我可能做错了什么。

2 个答案:

答案 0 :(得分:3)

而不是

finish = 10;

你应该写

finish = size;

在您编写的分区函数中

int pivot = numbers[finish];

这超出了数组的末尾。你的意思是

int pivot = numbers[finish - 1];

在分区函数中,运行如下循环:

for (int k = 0; k < finish - 1; k++)

从整个数组的开头开始。它应该是:

for (int k = start; k < finish - 1; k++)

分区功能的最后一步是错误的:

int temp2 = numbers[pole];
numbers[pole] = numbers[pivot];
numbers[pivot] = temp2;

使用数据透视值而不是数据透视索引。它应该是:

int temp2 = numbers[pole];
numbers[pole] = numbers[finish - 1];
numbers[finish - 1] = temp2;

quickSort的实施也是错误的:

marker = Partition(numbers, start, finish);
quickSort(numbers, start, marker - 1);
quickSort(numbers, marker + 1, finish);

应该是:

marker = Partition(numbers, start, finish);
quickSort(numbers, start, marker);
quickSort(numbers, marker + 1, finish);

以上更改会修复您的代码。

从根本上说,我认为您需要更清楚startfinish的含义。它们遵循start是第一个项的索引的约定,finish比最后一个项的索引大一个。由于没有完全理解这个约定,上面的许多错误都归结为一个一个错误。

答案 1 :(得分:0)

int main(){
int numbers[] = {14, 7, 23, 31, 1, 20, 6, 3, 40, 5};
const int size = sizeof(numbers)/sizeof(int);

cout << "Numbers are: " << endl;
for (int i = 0; i < size; i++){
    cout << numbers[i] << "\t";
}

quickSort(numbers, 0, size - 1);//This is the first place you should take care of!
                                //What do you want the third parameter represent?
                                //1.the greatest index of the array;or 2. the length of the array
                                //Here goes the first choose, so size-1
cout << "Sorted: " << endl;
for (int j = 0; j < size; j++){
    cout << numbers[j] << "\t";
} 
return 0;
}

int Partition(int num[], int start, int finish){
int temp;
int pole = start;
int pivot = num[finish];

for (int k = start; k <= finish - 1; k++){//as I mentioned above, choose 1 , so there must be =
    if (num[k] <= pivot){
        temp = num[k];
        num[k] = num[pole];
        num[pole] = temp;

        pole++;
    }
}

temp = num[pole];
num[pole] = num[finish];//you did something wrong here!!
num[finish] = temp;//And here.Not pivot.

return pole;
}

void quickSort(int num[], int start, int finish){
int marker;
if (start < finish){
    marker = Partition(num, start, finish);
    quickSort(num, start, marker - 1);//And here, num[marker] is in its final place,so ending with marker-1 will be fine. 
                                      //Of course, you also can replace it with marker.
    quickSort(num, marker + 1, finish); 
}
}