C ++快速排序实现崩溃;还有风格建议

时间:2015-05-25 19:41:49

标签: c++ sorting quicksort

我是一个很新手,今天用C ++实现了我的第一个快速排序算法:) 它可以工作,但只有少数数字,当我尝试输入超过20个数字(或大数字)时,它会崩溃,我不知道为什么。写的那么糟糕吗?

也可以随意批评我的代码!有更好的方法吗?例如,我如何实现用户可以输入数字,直到他输入特定的单词?

谢谢! :)

#include <iostream>
using namespace std;

//SORTING FUNCTION
void sort(double* numbers, int end, int start = 0)
{
    //END THE RECURSION
    if ((end-start) <= 0)
    {
        return;
    }

    //SORTING PART
    else
    {
        int pivot = (start + end) / 2;
        int left = pivot - 1;
        int right = pivot + 1;

        //SORT ALL THE NUMBERS RIGHT OF THE PIVOT
        do
        {
            if (numbers[left] >= numbers[pivot])
            {
                double temp;
                temp = numbers[left];
                for (int i = left; i < pivot; i++)
                {
                    numbers[i] = numbers[i + 1];
                }
                numbers[pivot] = temp;
                pivot--;
                left--;
            }
            else if (numbers[left] < numbers[pivot])
            {
                left--;
            }
        } while (left >= start);

        //SORT ALL THE NUMBERS RIGHT OF THE PIVOT
        do
        {
            if (numbers[right] <= numbers[pivot])
            {
                double temp;
                temp = numbers[right];
                for (int i = right; i > pivot; i--)
                {
                    numbers[i] = numbers[i - 1];
                }
                numbers[pivot] = temp;
                pivot++;
                right++;
            }
            else if (numbers[right] > numbers[pivot])
            {
                right++;
            }
        } while (right <= end);

        //SPLIT THE ARRAY INTO TO SUB ARRAYS AND CALL THE SORT FUNCTION
        sort(numbers, pivot - 1); //LEFT SIDE OF THE PIVOT
        sort(numbers, end, pivot + 1); //RIGHT SIDE OF THE PIVOT
}

int main()
{
    //IMPUT NUMBERS
    cout << "IMPUT NUMBERS. END WITH CHARACTER." << endl << endl;
    double numbersTemp[100];
    int counter = -1; //-1 SO IT POINTS TO THE RIGHT INDEX
    for (int i = 0; i < 100; i++)
    {
        double temp;
        cin >> temp;
        if (cin)
        {
            numbersTemp[i] = temp;
            counter++;
        }
        else
        {
            break; //BREAK WHEN SOMETHING ELSE THAN A DOUBLE IS ENTERED
        }
    }

    //INITIALISE ARRAY IN RIGHT SIZE
    double* numbers = new double[counter + 1];

    //PASS THE VALUES TO THE NEW ARRAY
    for (int i = 0; i < counter + 1; i++)
    {
        numbers[i] = numbersTemp[i];
    }

    //SORT IT
    sort(numbers, counter);

    //GIVE OUT THE SORTET NUMBERS
    cout << endl << "SORTED NUMBERS: " << endl;
    for (int k = 0; k <= counter; k++)
    {
        cout << numbers[k] << " ";
    }
    cout << endl << endl;

    //CLEAR USED MEMORY SPACE
    delete[] numbers;

    //END PROGRAMM (DUH)
    return 0;
}

1 个答案:

答案 0 :(得分:0)

我重新编写了一些排序代码,以提高效率

//END THE RECURSION
if ((end - start) <= 0)
{
    return;
}

//SORTING PART
else
{
    int pivot = (start + end) / 2;
    int left = start;   // Set left to first element
    int right = end;    // Set right to last element

    while (left < right)
    {
        if (numbers[left] > numbers[pivot]) // If left is greater then pivot swap them
        {
            double temp = numbers[left];
            numbers[left] = numbers[pivot];
            numbers[pivot] = temp;

            pivot = left; // Pivot is now left
        }

        if (numbers[right] < numbers[pivot]) // If right is smaller then pivot
        {
            double temp = numbers[right];
            numbers[right] = numbers[pivot];
            numbers[pivot] = temp;

            pivot = right; // Pivot is now right
        }

        left++;
        right--;
    }

    //SPLIT THE ARRAY INTO TO SUB ARRAYS AND CALL THE SORT FUNCTION
    sort(numbers, pivot - 1); //LEFT SIDE OF THE PIVOT
    sort(numbers, end, pivot + 1); //RIGHT SIDE OF THE PIVOT
}

左右移动从数组的末尾而不是从中间移动。这样,阵列的左右部分将被排序,只有枢轴需要四处移动。如果你想看看它是如何工作的,它基于这个video