如何删除合并排序数组的副本?

时间:2020-12-18 04:24:50

I have the following code, which merge sort an unsorted array in an ascending order.

我有下面的代码,它将一个无序数组按升序排序。

Now I would like to delete the repeated duplicate numbers

现在我想删除重复的重复数字。

for example if I have : 1 2 3 4 4 5 6 7 output should be: 1 2 3 4 5 6 7

例如,如果我有:1 2 3 4 4 5 6 7的输出应该是:1 2 3 4 5 6 7。

the code works and gives the right output for the example above. however if the array is : 1 2 3 4 6 6 6 7

代码工作并给出了上面示例的正确输出。但是如果数组是:1 2 3 4 6 6 7。

the i will go out of bound and there will be an error.

我将不再受约束,这将是一个错误。

how to fix it ?

如何修复它?

My code is below. Focus on the deletion part only, as the merge sort is working fine.

我的代码如下。只关注删除部分,因为合并排序工作良好。

#include <iostream>
using namespace std;

void mergeSort(int *a, int low, int high);
void merge(int *a, int low, int high, int mid);
int main()
{
    int myArray[] = {2,3,1,6,7,8,3,9};
    int sortedArray[7];
    int low= 0;
    int high = 7;

    mergeSort(myArray, low, high);


    for (int i = 0; i < 8; i++)
        cout << myArray[i] << " ";

    cout << endl;

    ////////////////delete duplicate///////////////////

    int j=0;
    for(int i =0; i <8; i++)
    {
    if(myArray[i] == myArray[i+1]) //if there is a duplicate at next spot
        {
            sortedArray[j] = myArray[i];
            i++;   //skip the i index from the duplicate array
            j++;   // move the sorted array index forward
        }
        else
        {
            sortedArray[j] = myArray[i];   
            j++;   //move the sorted array index forward
        }
    }

    for (int i = 0; i < 7; i++)
        cout << sortedArray[i] << " ";

}

void mergeSort(int *a, int low, int high)
{
    int mid;
    if(low < high)
    {
        mid = (low + high) / 2;
        mergeSort(a, low, mid);
        mergeSort(a, mid+1, high);
        merge(a,low,high,mid);
    }

    return;
}

void merge(int *a, int low, int high, int mid) 
{
    int i, j, k;
    i= low;
    j= mid +1;
    k= low;
    int c[8];

    while(i <= mid && j <= high)
    {
        if(a[i] < a[j])
        {
            c[k] = a[i];
            i++;
            k++;
        }
        else
        {
            c[k] = a[j];
            j++;
            k++;
        }
    }

    while(i <= mid)
    {
        c[k] = a[i];
        k++;
        i++;
    }

    while(j <= mid)
    {

        c[k] = a[j];

        k++;

        j++;

    }

     for (i = low; i < k; i++)

    {

        a[i] = c[i];

    }



    }

1 个解决方案

#1


1  

You forgot { and } to group two statements after else.

您忘记了{和}对另外两个语句。

there's bug, if the array is 1 4 4 4 5 your code will go wrong guy.

有bug,如果数组是1 4 4 4 5,你的代码就会出错。

a better solution:

一个更好的解决方案:

i = 0; j = 0;
while (i<length)
{
    sortedArr[j] = myArr[i];
    j++;
    k = i+1;
    while (k < length && myArr[i]=myArr[k]) k++;
    i = k;
}

#1


1  

You forgot { and } to group two statements after else.

您忘记了{和}对另外两个语句。

there's bug, if the array is 1 4 4 4 5 your code will go wrong guy.

有bug,如果数组是1 4 4 4 5,你的代码就会出错。

a better solution:

一个更好的解决方案:

i = 0; j = 0;
while (i<length)
{
    sortedArr[j] = myArr[i];
    j++;
    k = i+1;
    while (k < length && myArr[i]=myArr[k]) k++;
    i = k;
}