归并排序求逆序数

归并排序求逆序数
转自:http://www.blog.edu.cn/user2/sempr/archives/2006/1142716.shtml
#include<cstdio>
const int MAX = 10000; int a[MAX],b[MAX]; int change; void merge(int p, int q, int r) { int i, j = 0; int begA = p, endA = q, begB = q+1, endB = r; while(begA <= endA && begB <= endB) { if(a[begA] <= a[begB]) b[j++] = a[begA++]; else { b[j++] = a[begB++]; change += q - begA + 1; } } while(begA <= endA) b[j++] = a[begA++]; while(begB <= endB) b[j++] = a[begB++]; for(i = 0; i < j; i++) a[p+i] = b[i]; } void mergeSort(int first, int last) { if(first < last) { int mid = (first + last) / 2; mergeSort(first, mid); mergeSort(mid+1, last); merge(first, mid, last); } } int main() { return 0; }

你可能感兴趣的:(归并排序求逆序数)