//Standard mergesort algorithm, sort left, sort right, merge. void _mergeSort(vector<int> *arr, int start, int end) { if(start < end) { int mid = (end + start) / 2; _mergeSort(arr, start, mid); _mergeSort(arr, mid + 1, end); merge(arr, start, mid, end); } }
long long _mergeSort(long long arr[], long long temp[], long long left, long long right) { long long mid, inv_count = 0; if (right > left) { mid = (right + left)/2; inv_count = _mergeSort(arr, temp, left, mid); inv_count += _mergeSort(arr, temp, mid+1, right); inv_count += merge(arr, temp, left, mid+1, right); } return inv_count; }
long long int _mergeSort(int arr[], int temp[], int left, int right) { long long int mid, inv_count = 0; if (right > left) { mid = (right + left)/2; inv_count = _mergeSort(arr, temp, left, mid); inv_count += _mergeSort(arr, temp, mid+1, right); inv_count += merge(arr, temp, left, mid+1, right); } return inv_count; }
/* An auxiliary recursive function that sorts the input array and returns the number of inversions in the array. */ int _mergeSort(int arr[], int temp[], int left, int right) { int mid, inv_count = 0; if (right > left) { /* Divide the array into two parts and call _mergeSortAndCountInv() for each of the parts */ mid = (right + left)/2; /* Inversion count will be sum of inversions in left-part, right-part and number of inversions in merging */ inv_count = _mergeSort(arr, temp, left, mid); inv_count += _mergeSort(arr, temp, mid + 1, right); inv_count += merge(arr, temp, left, mid + 1, right); /*Merge the two parts*/ } return inv_count; }
/* This function sorts the input array and returns the number of inversions in the array */ int mergeSort(int arr[], int array_size) { //将字符串复制 int *temp = (int *)malloc(sizeof(int)*array_size); //归并排序 return _mergeSort(arr, temp, 0, array_size - 1); }
/* This function sorts the input array and returns the number of inversions in the array */ int mergeSort(int arr[], int array_size) { int *temp = (int *)malloc(sizeof(int)*array_size); return _mergeSort(arr, temp, 0, array_size - 1); }
long long mergeSort(long long arr[], long long array_size) { long long *temp = (long long *)malloc(sizeof(long long)*array_size); return _mergeSort(arr, temp, 0, array_size - 1); }
void mergeSort(vector<int> *arr) { _mergeSort(arr,0,(*arr).size()-1); }