合并排序重复相同的数字

时间:2015-10-31 18:53:56

标签: c++ sorting

我试图重新掌握我的基本CS技能,并试图实现一个简单的合并排序算法。我无法弄清楚我做错了什么,但代码只是反复保存相同的数字。这是我得到的输出:

enter image description here

我使用此网站作为指南:http://geeksquiz.com/merge-sort/我确认其确实正常工作(稍加修改。)任何人都在想我的代码中没有任何明显的错误,我无法捕获?< / p>

void Merge(int arr[], int left, int right, int pivot)
{

    int leftCount = pivot - left + 1;
    int rightCount = right - pivot;

    int *leftSec = new int[leftCount];
    int *rightSec = new int[rightCount];

    for (int i = 0; i < leftCount; i++)
    {
        leftSec[i] = arr[left + i];
    }
    for (int j = 0; j < rightCount; j++)
    {
        rightSec[j] = arr[pivot + 1 + j];
    }

    int i = 0;
    int j = 0;
    int k = leftCount;
    while(i < leftCount && j < rightCount)
    {
        if (leftSec[i] <= leftSec[j])
        {
            arr[k] = leftSec[i];
            i++;
        }
        else
        {
            arr[k] = rightSec[j];
            j++;
        }
        k++;
    }
    while (i < leftCount)
    {
        arr[k] = leftSec[i];
        i++;
        k++;
    }
    while (j < rightCount)
    {
        arr[k] = rightSec[j];
        j++;
        k++;
    }
    delete[] leftSec;
    delete[] rightSec;
}

void MergeSort(int arr[], int left, int right)
{
    if (left < right)
    {
        int pivot = (left + right) / 2;
        MergeSort(arr, left, pivot);
        MergeSort(arr, pivot + 1, right);
        Merge(arr, left, right, pivot);
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    int t;
    int unsortedArray[] = { 94, 384, 934, 3, 56, 57, 94, 60, 90 };
    int arrayLength = sizeof(unsortedArray) / sizeof(int);
    printArray(unsortedArray, arrayLength);
    MergeSort(unsortedArray, 0, arrayLength);
    printArray(unsortedArray, arrayLength);
    cin >> t;
    return 0;
}

1 个答案:

答案 0 :(得分:1)

您的代码有很多问题。它在函数merge()中有内存泄漏:你使用new,你的删除在哪里?其次是

while(j < rightCount) //this is never executed, you can double check this by drawing the mergesort tree and a perform the steps.

抱歉这么久。无论如何这里是你的算法:)

void mergeTree(int* arr, int left, int mid, int right) {
    //new arrays left and right



    int* leftArr = new int[mid - left + 1];
    int* rightArr = new int[right - mid];

    //create a map between the new arrays 0...n where n = left counter/right counter and left...right
    int leftCounter = mid - left + 1;
    int rightCounter = right - mid;

    int i = 0;
    int j = left;
    //copy the arrays
    for(i = 0; i < leftCounter; i++) {
        leftArr[i] = arr[j];
        j++;
    }
    j = mid + 1;
    for(i = 0; i < rightCounter; i++) {
        rightArr[i] = arr[j];
        j++;
    }

    i = 0;
    j = 0;
    int k = left;

    while(i < leftCounter && j < rightCounter) {
        if(leftArr[i] <= rightArr[j]) {
            arr[k] = leftArr[i];
            i++;
        }
        else {
            arr[k] = rightArr[j];
            j++;
        }
        k++;
    }

    while(i < leftCounter) {
        arr[k] = leftArr[i];
        i++;
        k++;
    }

    delete[] leftArr;
    delete[] rightArr;

}

 void mergeSort(int* arr, int left, int right) {
    if(left < right) {
        int mid = (left + right) / 2;
        mergeSort(arr, left, mid);
        mergeSort(arr, mid + 1, right);
        mergeTree(arr, left, mid, right);
    }
}

mergesort(arr, 0, n-1)...