本文共 577 字,大约阅读时间需要 1 分钟。
归并排序是每一次的递归调用会确定正确排序中的一个值。
然后这时只确定了个元素的位置。 再通过fun(left,i-1);fun(i+1,right);
确定这个元素两边的正确排序的位置
#includeusing namespace std;int a[5005];int fun(int left,int right){ if(left >= right) return 0; int i = left; int j = right; int x = a[i]; while(i < j){ while(i < j && a[j] >= x) j--; if(i < j) a[i++] = a[j]; while(i < j && a[i] < x) i++; if(i < j) a[j--] = a[i]; } a[i] = x; fun(left,i-1); fun(i+1,right); return 0;}int main(){ int n; scanf("%d",&n); for(int i = 0;i < n;i++){ scanf("%d",a+i); } fun(0,n-1); for(int i = 0;i < n;i++){ printf("%d\n",a[i]); } return 0;}
转载地址:http://nfqp.baihongyu.com/