C ++:mergesort和插入排序混合

时间:2014-11-29 01:00:46

标签: c++ algorithm sorting mergesort insertion-sort

我一直在尝试在c ++中创建mergesort和插入排序混合。但是,每当我使用确切的顺序9,1,1,4,4,0,3,8,5,7,9,5,9,1,3,3,1,5,7,0,7,2(顺便说一下这22个数字)将这些数字输入到算法中时,我得到:1,1,0,1,1,0,2,3,3,3,4,4,5,5,5,7,7,7,8,9,9,9,显然没有排序正确。

如果您想知道我的个人算法是否错误,答案是否定的。我使用相同的数字以相同的顺序单独测试了我的mergesort和插入排序算法,并且每个算法都生成了正确排序的数字数组。

因此,为了将两种算法粘合在一起,所需的附加代码中的算法可能出错了。 (我也稍微更改了插入排序代码以使其更有效,但这也不是问题,如果你想知道的话。在我改变它之前和之后对算法的两次测试产生了相同的结果:错误的结果。)

如果你想看到我的代码,请点击这里:

#include <iostream>

void sort (int *array, int low, int mid, int high) {
    //Insertion sort
    for (int i = mid; i < high; i++) {
        for (int j = i - 1; j >= 0; j--) {
            if (array[i] < array [j]) {
                int holder = array[j];
                array[j] = array[i];
                array[i] = holder;
                i--;
            }
        }
    }
}
void merge (int *array, int *sub, int low, int mid, int high) {
    //Merge part of mergesort
    int a = low;
    int b = low;
    int c = mid;

    while ((a < mid) && (c < high)) {
        if (array[a] <= array[c]) {
            sub[b] = array[a];
            a++;
        } else {
            sub[b] = array[c];
            c++;
        }
        b++;
    }
    while (a == mid && c < high) {
        sub[b] = array[c];
        c++;
        b++;
    }
    while (c == high && a < mid) {
        sub[b] = array[a];
        a++;
        b++;
    }
    for (int d = low; d < high; d++) {
        array[d] = sub[d];
    }
}
void split (int *array, int *sub, int low, int high) {
    //Split part of mergesort
    if (low < high - 1) {
        int mid = (low + high) / 2;
        split(array, sub, low, mid);
        split(array, sub, mid, high);
        if ((high - low) > 10){
            merge(array, sub, low, mid, high);
        } else {
            sort(array, low, mid, high);
        }

    }
}

int main()
{
    std::cout << "This is a program that sorts integers.\n";
    std::cout << "How many numbers would you like to sort?\n";
    int num;
    std::cin >> num;
    if (num < 0) {
        std::cout << "Invalid amount of numbers.\n";
        return 0;
    } else if (num == 0) {
        std::cout << "No numbers to sort.\n";
        return 0;
    } else if (num == 1) {
        std::cout << "Please type in the number.\n";
        std::cin >> num;
        std::cout << "Your sorted number is: " << num << ".\n";
        return 0;
    }
    int * array = new int [num];
    int * sub = new int [num];
    std::cout << "Please type in the numbers.\n";
    for (int i = 0; i < num; i++) {
        std::cin >> array[i];
    }
    split(array, sub, 0, num);
    std::cout << "Your sorted numbers are: ";
    for (int i = 0; i < num; i++) {
        std::cout << array[i];
        if (i != num - 1) {
            std::cout << ", ";
        } else {
            std::cout << ".\n";
        }
    }
    delete[] array;
    delete[] sub;

    return 0;

}

对此的任何帮助将不胜感激。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您的sort()例程中存在两个问题 - 您只需从midhigh(而不是lowhigh进行排序) ,留下一些未分类的东西。修复后,当j移动到low以下而不是0时,您需要停止mid,因为我们并不总是从数组的开头开始工作。

最后,该功能不再需要void sort (int *array, int low, int high) { //Insertion sort for (int i = low; i < high; i++) { for (int j = i - 1; j >= low; j--) { if (array[i] < array [j]) { int holder = array[j]; array[j] = array[i]; array[i] = holder; i--; } } } }

split()

稍后,在sort(array, low, high); 中,调整电话:

{{1}}

工作示例:https://ideone.com/B1UtSK