#include <iostream>
#include <algorithm>
using namespace std;
void show(int * a, int b){
for(int i = 0; i < b; i++){
cout << a[i] << " ";
}
cout << endl;
}
int merge(int * a, int b, int m, int e){
cout << "b " << b << " " << m << " " << e << endl;
int i = b, j = m, k = 0;
int h = 0;
int * c = (int *)malloc(sizeof(int) * (e - b));
while(i < m && j < e){
if(a[i] > a[j]){
h = h + m - i;
c[k++] = a[j++];
}
else if(a[i] < a[j]){
c[k++] = a[i++];
}
}
while(i < m){
c[k++] = a[i++];
}
while(j < e){
c[k++] = a[j++];
}
for(i = b, k = 0; i < e; i++, k++){
a[i] = c[k];
}
show(a, 8);
return h;
}
int f(int * a, int s, int e){
cout << s << " " << e << endl;
if(e - s == 1){
return 0;
}
int m = (e + s) / 2;
int t1 = f(a, s, m);
int t2 = f(a, m, e);
int t3 = merge(a, s, m, e);
return t1 + t2 + t3;
}
int b[5] = {-1};
int main(){
int a[] = {1, 2, 3, 4, 5, 6, 7, 0};
cout << f(a, 0, 8);
}
逆序對
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.