定一个基准位,递归左右两边排序。
public void fun(){ int arr[] = {2,3,4,5,6,7,822,3,4,5,8,6,5,4,2,1}; //System.out.println(qsort(arr,0,(arr.length)-1)); dofun(arr,0,(arr.length)-1); for(int i = 0 ; i < arr.length ; i ++){ System.out.println(arr[i]); } } private int qsort(int arr[],int left ,int right) { int begin_num = arr[left],i = left,j = right; while(i != j){ while(arr[j] >= begin_num && i!=j){ j--; } arr[i] = arr[j]; while(arr[i] < begin_num && i!=j){ i++; } arr[j] = arr[i]; } arr[i] = begin_num; return i; } private void dofun(int arr[],int left,int right) { if(left < right){ int mid = qsort(arr,left,right); dofun(arr, left, mid);dofun(arr, mid+1, right); } }