关于归并排序的题,不知道哪里出问题了,同样的思路java写出来是正确的,可是用C语言答案就错了
大家帮忙看看程序代码:
#include<stdio.h> #include<stdlib.h> void getResult(int ,int ); void getCount(int ,int ,int ); int *a; int *b; int n; long count = 0; int main() { int i; scanf("%d",&n); a = (int*)malloc(sizeof(int)*n); b = (int*)malloc(sizeof(int)*n); for(i=0; i<n; i++){ scanf("%d",&a[i]); } getResult(0,n-1); //printf("%d\n",count); for(i=0; i<n; i++){ printf("%d ",a[i]); } printf("\n"); return 0; } void getResult(int s,int e) { if(s < e){ int mid = (s+e)/2; getResult(s,mid); getResult(mid+1,e); getCount(s,mid,e); } } void getCount(int s,int mid,int e) { int i = s; int j = mid+1; int k = 0; while(i<mid+1 && j<e+1){ if(a[i] < b[j]){ b[k++] = a[i++]; }else{ //count += e-j+1; b[k++] = a[j++]; } } while(i < mid+1){ b[k++] = a[i++]; } while(j < e+1){ b[k++] = a[j++]; } int arg = 0; for(arg=0; arg<e-s+1; arg++){ a[s+arg] = b[arg]; } }