运用二分的思想去将数列快速排序的方法
#include <stdio.h> int a[101],n; void quicksort (int left,int right) { int i,j,t,temp; if (left>right) return; temp=a[left];//temp中存的就是基准数 i=left; j=right; while (i!=j) { //注意顺序,一定要先右后左 while (a[j]>=temp&&i<j) j--; while (a[i]<=temp&&i<j) i++; //交换两个数在数组中的位置 if (i<j)//当哨兵ij在数组中没有相遇时 { t=a[i]; a[i]=a[j]; a[j]=t; } } //将基准数归位 a[left]=a[i]; a[i]=temp; //递归 quicksort(left,i-1); quicksort(i+1,right); return; } int main () { int i,j; scanf("%d",&n); for (i=1;i<=n;i++) { scanf("%d",&a[i]); } quicksort (1,n); for (i=1;i<=n;i++) printf("%d ",a[i]); system("pause"); return 0; }
05-01
2万+
08-08
81