Merge sort: small and issues

Merge sort: small and issues

In an array, the left side of each element is smaller than the current element value of the element values added up, called small and arrays
example: [2, 3, 4, 1, 5]
2 on the left than the two smaller elements: none
3 left less than 3 elements: 2
4 4 smaller than the left element: 2,3
1 left elements is smaller than 1: no
5 on the left of the light elements than 5: 2, 3, 4, 1
small and small_sum = 2 + 2 + 3 + 2 + 3 + 4 + 1 = 17

solution

When using a merge sort properties binary array, recursion sort, when placed in an auxiliary array, the left and right two arrays are sorted, so when the right there is a certain element number is greater than the left, to the right of the right number certainly larger than the element, the element simply by multiplying the number of elements greater than, to the accumulated sum to

The array [2, 3, 4, 0, 5, 6, 1, 8]

Recursive call to merge sort, form is as follows:

​ [2, 3, 4, 0] [5, 6, 1, 8]

[2, 3] [4, 0] [5, 6] [1, 8]

[2] [3] [4] [0] [5] [6] [1] [8]

Recursive features, 2 and 3 of the first sort, when placed in an auxiliary array, the left to the right is smaller than 3, since there is no other number 3 behind the right, so the sum + = 2x1,2 and 3 in ascending order. 0,4 and 4 is then calculated is greater than 0, are not accumulated, sorted directly. Then calculating [2, 3], and [0, 4], when placed in the auxiliary array is greater than a second left to the right of a 0, is left because of small to large, a large 2 than 0, 2 behind numbers can not smaller than 0, it directly into the auxiliary array 0, pointer to 4, smaller than 24, but no further figures 4 behind, so the accumulated into 2 sum, sum + = 2x1, and then placed in 2 an auxiliary array, the left point 3, less than 4 3, plus accumulated sum + = 3x1, and in order, generates [0, 2, 3, 4]. Similarly to the right, and finally [0, 2, 3, 4] and sorting [1,5,6,8], the left and right 1 0 compared 0 smaller than 1, because the right is sorted in descending , so that the latter figure large than 0, is directly accumulated sum + = 0 * 4; Similarly other elements;

Code

#include <stdio.h>

int helperArr[10];

int Merge(int arr[], int l, int mid, int r)
{
    int i = l;
    int p1 = l;
    int p2 = mid + 1;
    int sum = 0;
    while (p1 <= mid && p2 <= r) {
        sum += arr[p1] < arr[p2] ? arr[p1] * (r - p2 + 1) : 0;
        helperArr[i++] = arr[p1] < arr[p2] ? arr[p1++] : arr[p2++];
    }
    while (p1 <= mid) {
        helperArr[i++] = arr[p1++];
    }
    while (p2 <= r) {
        helperArr[i++] = arr[p2++];
    }

    for (; l <= r; l++) {
        arr[l] = helperArr[l];
    }
    return sum;
}

int MergeSort(int arr[], int l, int r)
{
    if (l == r) {
        return 0;
    }
    int mid = l + ((r - l) >> 1); // =(l+r)/2 因为l+r有可能会溢出,所以改成减法的方式
    return MergeSort(arr, l, mid) + MergeSort(arr, mid + 1, r) + Merge(arr, l, mid, r);
}

int SmallSum(int arr[], int size)
{
    if (arr == NULL || size < 2) {
        return 0;
    }
    return MergeSort(arr, 0, size - 1);
}

void PrintArr(int arr[], int size)
{
    for (int i = 0; i < size; ++i) {
        printf("%d ", arr[i]);
    }
    printf("\r\n");
}

int main()
{
    int arr[10] = {6, 0, 5, 3, 15, 21, 13, 9, 12, 8};
    PrintArr(arr, 10);
    int ret = SmallSum(arr, 10);
    PrintArr(arr, 10);
    printf("小和是:%d", ret);
    return 0;
}

Guess you like

Origin www.cnblogs.com/causewang/p/12064308.html