【问题标题】:Is there any problem with this implementation of the Merge Sort algorithm? [closed]合并排序算法的这种实现有什么问题吗? [关闭]
【发布时间】:2020-03-01 07:49:50
【问题描述】:

我知道合并排序算法有很多实现。我已经根据 CLRS 的 Introduction to Algorithms 一书中提供的算法实现了以下代码。

void merge_sort(int arr[], int starting_index, int ending_index) {  

    if(starting_index < ending_index) {         

        int middle_index = (starting_index + ending_index)/2;
        merge_sort(arr, starting_index, middle_index);  
        merge_sort(arr, middle_index+1, ending_index);
        merge_the_parts(arr, starting_index, middle_index, ending_index);   
    }
}

void merge_the_parts(int arr[], int starting_index, int middle_index, int ending_index) {

    int length_of_first_array = middle_index - starting_index + 1;
    int length_of_second_array = ending_index - middle_index;

    const int sentinel = INT_MAX;

    int left_arr[length_of_first_array + 1]; 
    int right_arr[length_of_second_array + 1]; 

    for(int i=0; i<length_of_first_array; i++) {
        left_arr[i] = arr[starting_index + i];      
    }

    // building second auxilliary
    for(int i=0; i<length_of_second_array; i++) {
        right_arr[i] = arr[(middle_index+1) + i];
    }

    left_arr[length_of_first_array] = sentinel; // use the sentinel as a condition
    right_arr[length_of_second_array] = sentinel;

    int i=0;
    int j=0;

    // the main merging loop
    for(int k=starting_index; k<ending_index+1; k++) {

        if(left_arr[i] <= right_arr[j]) {
            arr[k] = left_arr[i];
            i++;
        }

        else {
            arr[k] = right_arr[j];
            j++;
        }
    }   
}

该代码可以很好地用于对数组进行排序和反转计数。但它对这个问题给出了错误的答案 - https://www.spoj.com/problems/DCEPC206/。我已使用以下代码解决此问题。

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

long int merge(int *arr, int l, int mid, int r) {

    int n1 = mid - l + 1;
    int n2 = r - mid;

    int *arrL = new int[n1 + 1];
    int *arrR = new int[n2 + 1];

    for(int i=0; i<n1; i++) {
        arrL[i] = arr[l + i];
    }

    for(int j=0; j<n2; j++) {
        arrR[j] = arr[(mid + 1) + j];
    }

    arrL[n1] = INT_MAX;
    arrR[n2] = INT_MAX;

    int i = 0, j = 0;
    long int count = 0;

    for(int k=l; k<=r; k++) {

        if(arrL[i] < arrR[j]) {
            arr[k] = arrL[i];
            if(arrR[j] != INT_MAX) {
                count += (arrL[i] * (n2 - j));
            }
            i++;
        } else {
            arr[k] = arrR[j];
            j++;
        }
    }

    delete[] arrL;
    delete[] arrR;

    return count;
}

long int countSeries(int *arr, int l, int r) {

    long int count = 0;

    if(l < r) {

        int mid = (l + r) / 2;
        count += countSeries(arr, l, mid);
        count += countSeries(arr, mid + 1, r);
        count += merge(arr, l, mid, r);
        return count;
    }

    return count;
}

【问题讨论】:

  • 你自己回答了你的问题,有问题。请收下tour 并阅读How to Ask 以帮助您写出更多有用的问题。另外,提取并提供minimal reproducible example,其中包含足够的信息来重现故障。
  • 可以更改代码以检查索引是否到达子数组的末尾,而不是依赖于哨兵 (INT_MAX) 值。如果数据永远不能包含标记值,那不是问题。可以通过一次性分配工作数组,并根据递归级别更改合并方向(或切换到自下而上合并排序,并根据循环计数更改合并方向)来加快代码速度。跨度>

标签: c++ arrays algorithm mergesort


【解决方案1】:

您得到了错误的答案,因为考虑到问题中给出的范围,您只使用long 来保存count 的如此大的值。

使用long long,它可以保持较大的值并且不会溢出:

long long count = 0;, long long countSeries(int *arr, int l, int r) {, count += (long long)arrL[i] * (n2 - j);

Here's a running code

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-27
    • 1970-01-01
    • 2012-11-07
    • 2020-06-21
    • 1970-01-01
    • 1970-01-01
    • 2020-06-03
    相关资源
    最近更新 更多