c_cpp 在数组中计算反转

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 在数组中计算反转相关的知识,希望对你有一定的参考价值。

// solution 1: brute force
// for each element, count number of elements which are on right side of it and are smaller than it
int count_inversions(int A[], int N) {
    int count = 0;
    for(int i=0; i<N-1; i++) {
        for(int j=i+1; j<N; j++) {
            if(A[i] > A[j]) 
                count++;
        }
    }
    return count;
}


// solution 2: divide & conquer, similar to merge sort

int count_inversions(int A[], int i, int j) {
    if(i >= j) return 0;
    int m = (i+j)>>1;
    int c1 = count_inversions(A, i, m);
    int c2 = count_inversions(A, m+1, j);
    int c3 = merge(A, B, i, m, j);
    return c1 + c2 + c3;
}

/*
void merge_sort(int A[], int low, int high) {
    if(low >= high) return;
    int m = (low + high) >> 1;
    merge_sort(A, low, m);
    merge_sort(A, m+1, high);
    merge(A, B, low, m, high);
}*/

int merge(int A[], int low, int m, int high) {  // gist, add a mid parameter
    // merge left and right half of A into B, then assign all items in B into A
    // left half range: [low, m], right half range: [m+1, high]
    int i = low, j = m+1, k = low;
    int B[1000];
    int total_inversions = 0;
    
    while(i <= m && j <= high) {
        if(A[i] < A[j]) 
            B[k++] = A[i++];
        else {
            B[k++] = A[j++];
            total_inversions += m - i + 1;  // gist
        }
    }
    
    while(i <= m) B[k++] = A[i++];
    while(j <= high) B[k++] = A[j++];
    k--; // no need?
    
    for(int t=low; t<=high; t++) A[t] = B[t];
    return total_inversions;
}



以上是关于c_cpp 在数组中计算反转的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 在C中反转一个字符串

计算数组中的反转

计算变化数组中的预期反转次数[关闭]

c_cpp 计算已排序数组中的出现次数

c_cpp 计算已排序数组中的出现次数(或频率)

c_cpp 如何只根据数组名计算数组的长度?