如何正确实现合并排序

时间:2015-04-27 14:14:55

标签: c++ sorting mergesort

我有下面的合并排序代码,它没有提供正确的输出。 输入:1 7 3 9 4 5 2 6 8 输出:1 5 2 6 7 3 8 1 9。

#include<iostream>
#include<vector>
using namespace std;

void merge(vector<int>& array,vector<int>& helper,int low,int mid,int high)
{
    int size = array.size();
    for(int i =0;i<size;i++)
        helper.push_back(array[i]);
    //helper = array; // copy vector elements i .. copying references doesnt work hence
    int current = low;
    int helperLeft = low;
    int helperRight = mid+1;
    while(helperLeft<=mid && helperRight <=high)
    {
        if(helper[helperLeft]<helper[helperRight])
        {   array[current] = helper[helperLeft];
            helperLeft++;
        }
        else
        {
            array[current] = helper[helperRight];
            helperRight++;
        }
        current++;
    }
    int remaining = mid - helperLeft;
    for (int i = 0; i <= remaining; i++) 
        array[current + i] = helper[helperLeft + i];
    // copy the remaining elements from the left only and not right because right elememts are already there in dest (array) sorted.
}

void mergeSort(vector<int>&array,vector<int>& helper,int low,int high)
{
    if(low < high)
    {
        int mid = (low+high)/2;
        mergeSort(array,helper,low,mid);// sort left half
        mergeSort(array,helper,mid+1,high);// sort right half
        merge(array,helper,low,mid,high);
    }
}

void mergeSort(vector<int>& array)
{
    vector<int> helper;
    mergeSort(array,helper,0,array.size());
}

int main()
{
    vector<int> v;
    int i =1;
    v.push_back(i);
    v.push_back(i+6);
    v.push_back(i+2);
    v.push_back(i+8);
    v.push_back(i+3);
    v.push_back(i+4);
    v.push_back(i+1);
    v.push_back(i+5);
    v.push_back(i+7);
    vector<int>::iterator it=v.begin();
    while(it!=v.end())
    {
        cout<<" "<<*it;
        it++;
    }
    cout<<endl;
    mergeSort(v);
    it = v.begin();
    while(it!=v.end())
    {
        cout<<" "<<*it;
        it++;
    }
    cout<<endl;
}

我在这里使用C ++向量而不是数组,因为传递大小会有问题吗? 有人可以帮我纠正这里的错误吗?

1 个答案:

答案 0 :(得分:0)

我得到了答案。正确的解决方案发布在下面。

#include<iostream>
#include<vector>
using namespace std;

void merge(vector<int> &array,int low,int mid,int high)
{
    vector<int> helper(array);
    //helper = array; // copy vector elements i .. copying references doesnt work hence
    int current = low;
    int helperLeft = low;
    int helperRight = mid+1;
    while(helperLeft<=mid && helperRight <=high)
    {
        if(helper[helperLeft]<helper[helperRight])
        {   
            array[current] = helper[helperLeft];
            helperLeft++;
        }
        else
        {
            array[current] = helper[helperRight];
            helperRight++;
        }
        current++;
    }
    while(helperLeft<=mid)    
        array[current++] = helper[helperLeft++];
    // copy the remaining elements from the left only and not right because right elememts are already there in dest (array) sorted.
}

void mergeSort(vector<int>&array,int low,int high)
{
    if(low < high)
    {
        int mid = (low+high)/2;
        mergeSort(array,low,mid);// sort left half
        mergeSort(array,mid+1,high);// sort right half
        merge(array,low,mid,high);
    }
}

void mergeSort(vector<int>& array)
{
    mergeSort(array,0,array.size()-1);
}

int main()
{
    vector<int> v;
    int i =1;
    v.push_back(i);
    v.push_back(i+6);
    v.push_back(i+2);
    v.push_back(i+8);
    v.push_back(i+3);
    v.push_back(i+4);
    v.push_back(i+1);
    v.push_back(i+5);
    v.push_back(i+7);
    vector<int>::iterator it=v.begin();
    while(it!=v.end())
    {
        cout<<" "<<*it;
        it++;
    }
    cout<<endl;
    mergeSort(v);
    it = v.begin();
    while(it!=v.end())
    {
        cout<<" "<<*it;
        it++;
    }
    cout<<endl;
}