创建合并排序算法时遇到问题(C ++)

时间:2016-03-25 22:01:50

标签: c++ algorithm sorting

我在合并排序的最终实现方面遇到了问题。以下是我的代码:

#include <iostream>
using namespace std;

void decomposeArray(int* array, int length) {
    int arrayOfArrays[length];
    cout << endl;
    for(int i = 0; i < length; i++) {
       cout << "Array: " << array[i] << endl;
    }

    if (length > 1) {
        int midpoint = length/2;
        int elemInFirst = midpoint;
        int elemInSecond = elemInFirst;

        if ((length % 2) == 1) {
            elemInSecond += 1;
        }

        int firstArray[elemInFirst];
        int secondArray[elemInSecond];

        for(int i = 0; i < elemInFirst; i ++) {
            firstArray[i] = array[i];

        }

        for(int j = elemInFirst; j < length; j++) {
            secondArray[j-elemInFirst] = array[j];
        }


        for(int i = 0; i < elemInFirst; i++) {
           cout << "First half: " << firstArray[i] << endl;
        }

        for(int i = elemInFirst; i < length; i++) {
           cout << "Second half: " << secondArray[i-elemInFirst] << endl;
        }

        decomposeArray(firstArray, elemInFirst);
        decomposeArray(secondArray, elemInSecond);


        int* superiorArray;
        int* inferiorArray;
        int dominantIndex;
        int inferiorIndex;

        if (elemInFirst > elemInSecond) {
            dominantIndex = elemInFirst;
            superiorArray = firstArray;

            inferiorIndex = elemInSecond;
            inferiorArray = secondArray;

        }
        else {
            dominantIndex = elemInSecond;
            superiorArray = secondArray;

            inferiorIndex = elemInFirst;
            inferiorArray = firstArray;
        }



        for(int i = 0; i < dominantIndex; i++) {
            for (int j = 0; j < inferiorIndex; j++) {
                if (superiorArray[i] > inferiorArray[j]) {
                    int tempVal = array[i];
                    array[i] = inferiorArray[j];
                    array[(dominantIndex+j)] = tempVal;
                }
                else if (j == (inferiorIndex-1)){
                    int tempVal = array[i];
                    cout << "Array at i is " << array[i] << endl;
                    cout << "i is " << i << endl;
                    cout << "This temp value is " << array[i] << endl;
                    array[i] = superiorArray[i];
                    cout << "superiorArray[i]: " << superiorArray[i] << endl;
                    array[((dominantIndex)+j)] = tempVal;
                }

                else {
                    continue;
                }

            }
            cout << "Current array " << endl;
            for(int i = 0; i < length; i++) {
                cout << array[i] << ",";
            }
            cout << endl;
        }



        /*while ((firstIterator != elemInFirst) && (secondIterator !=elemInSecond) && (origArrayIter != length)) {
            if (firstArray[firstIterator] > secondArray[secondIterator]) {
                array[origArrayIter] = secondArray[secondIterator];
                array[origArrayIter + 1] = firstArray[firstIterator];

                firstIterator++;
                secondIterator++;
                origArrayIter+= 2;
            }
            else {
                array[origArrayIter] = firstArray[firstIterator];
                array[origArrayIter+1] = secondArray[secondIterator];
                firstIterator++;
                secondIterator++;
                origArrayIter+= 2;
            }
        }*/
        cout << endl;
        for(int i = 0; i < length; i++) {
            cout << "Array coming out: " << array[i] << endl;
        }

        /*int tempArray[length];
        for(int i = 0; i < length; i++) {
            tempArray[i] = array[i];
        }
        return tempArray;*/
    }

}

具体来说,我遇到问题的片段是将数组合并在一起:

    for(int i = 0; i < dominantIndex; i++) {
            for (int j = 0; j < inferiorIndex; j++) {
                if (superiorArray[i] > inferiorArray[j]) {
                    int tempVal = array[i];
                    array[i] = inferiorArray[j];
                    array[(dominantIndex+j)] = tempVal;
                }
                else if (j == (inferiorIndex-1)){
                    int tempVal = array[i];
                    cout << "Array at i is " << array[i] << endl;
                    cout << "i is " << i << endl;
                    cout << "This temp value is " << array[i] << endl;
                    array[i] = superiorArray[i];
                    cout << "superiorArray[i]: " << superiorArray[i] << endl;
                    array[((dominantIndex)+j)] = tempVal;
                }

                else {
                    continue;
                }

            }
            cout << "Current array " << endl;
            for(int i = 0; i < length; i++) {
                cout << array[i] << ",";
            }
            cout << endl;
        }

当我使用值数组{5,3,8,2}时,我返回一个数组{2,5,3,3}。最终数组之前的步骤产生一个数组{2,3,8,5}。出于某种原因,我只是不知道5和8为什么不交换。任何帮助或指导将不胜感激,谢谢。

0 个答案:

没有答案