无法在C ++中实现合并排序

时间:2016-09-17 11:08:00

标签: c++ algorithm mergesort

我是算法的新手,我一直试图让合并排序工作,但它只是没有给出正确的输出。没有编译错误,但我想它只是在某处有缺陷,在输出中显示随机值作为排序数组。

void merge_sort(int[], int, int);
void merge(int[], int, int, int);
void printarray(int[], int);

int main() {
    int Arr[100], num_of_elements;
    cout << "Enter the number of elements (max 100): ";
    cin >> num_of_elements;
    cout << "Enter array elements: \n";
    for (int i = 0;i < num_of_elements;++i)
        cin >> Arr[i];
    merge_sort(Arr, 0, num_of_elements - 1);
    cout << "\nAfter Sorting (by Merge Sort):\n";
    printarray(Arr, num_of_elements);
    cout << endl;
    return 0;
}

void merge_sort(int arr[], int left, int right) {
    if (left < right) {
        int mid = (left + right) / 2;
        merge_sort(arr, left, mid);
        merge_sort(arr, mid + 1, right);
        merge(arr, left, mid, right);
    }  
}

void merge(int arr[], int left, int mid, int right) {
    int i, j, k;

    /* Calculate the lengths of the subarrays and copy the elements into them */
    int lenght_left = mid - left + 1;
    int length_right = right - mid;
    int *leftarray = new int[lenght_left];
    int *rightarray = new int[length_right];
    for (i = 0;i < lenght_left;++i)
        leftarray[i] = arr[left + i];
    for (j = 0;j < length_right;++j)
        rightarray[j] = arr[mid + 1 + j];

    /* Reordering the elements in the original array */
    for (k = left, i = 0, j = 0;k <= right;++k) {
        if (leftarray[i] <= rightarray[j])
            arr[k] = leftarray[i++];
        else
            arr[k] = rightarray[j++];
    }  

    /* Copy remaining elements into the array */
    while (i < lenght_left)
        arr[k] = leftarray[i++];
    while (j < length_right)
        arr[k] = rightarray[j++];
    delete[](leftarray);
    delete[](rightarray);
}

void printarray(int arr[], int num) {
    cout << "Displaying Elements in array: \n";
    for (int i = 0;i < num;i++)
        cout << arr[i] << "  ";
}

2 个答案:

答案 0 :(得分:0)

merge函数中:

/* Reordering the elements in the original array */
for (k = left, i = 0, j = 0; k <= right; ++k) {
//                           ^^^^^^^^^^^
// It should be i < lenght_left && j < length_right
    if (leftarray[i] <= rightarray[j])
        arr[k] = leftarray[i++];
    else
        arr[k] = rightarray[j++];
}

你用来控制循环的条件是不对的,条件应该是关于你的leftarrayrightarray,它应该是,当任何一个数组到达它们时结束,所以将你的条件改为i < lenght_left && j < length_right

在复制同一功能中的其余元素时:

/* Copy remaining elements into the array */
while (i < lenght_left)
    arr[k] = leftarray[i++];
//     ^^^
while (j < length_right)
    arr[k] = rightarray[j++];
//     ^^^

在这里,您忘记增加k,将其更改为k++

答案 1 :(得分:0)

你犯了几个错误:

  1. 您需要检查合并时是否超出数组的长度

    即代替:

    for (k = left, i = 0, j = 0;k <= right;++k)
    

    你应该:

    for (k = left, i = 0, j = 0;k <= right && i <lenght_left && j<length_right;++k)
    
  2. 添加剩余元素时,忘记增加数组计数器。 即:

    while (i < lenght_left)
        arr[k] = leftarray[i++];
    while (j < length_right)
        arr[k] = rightarray[j++];
    

    你应该:

    while (i < lenght_left)
        arr[k++] = leftarray[i++];
    while (j < length_right)
        arr[k++] = rightarray[j++];