快速排序
代码
#includevoid quicksort(int a[],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)//交换两个数在数组中的位置 { t = a[i]; a[i] = a[j]; a[j] = t; } } //将基准数归位 a[left] = a[i]; a[i] = temp; //递归: quicksort(a,left,i-1);//继续处理左边 quicksort(a,i+1,right);//继续处理右边 return; } int main() { int i,j; int a[]={1,346,234,3246,7,24,63,52,45,8,1,42,55,63,2}; int n = sizeof(a) / sizeof(int);//获取数组中元素个数 quicksort(a,1,n); for(i=0;i
Comments | NOTHING