问题描述
/**快速排序**/privatestaticintn=0;publicstaticvoidquickSort(int[]arr,intstart,intend){intkey=arr[start];inti=start,j=end;if(i==j){return;}while(i!=j){while(j>i){if(arr[j]<key){swap(arr,i,j);break;}j--;}while(i<j){if(arr[i]>key){swap(arr,i,j);break;}i++;}}intk=i;printProcess(n++,arr);quickSort(arr,k+1,end);quickSort(arr,start,k);}/**交换数组中的两个元素**/publicstaticvoidswap(int[]arr,inti,intj){inttemp=arr[i];arr[i]=arr[j];arr[j]=temp;}/**打印排序过程**/publicstaticvoidprintProcess(inti,int[]arr){System.out.print("第"+(i+1)+"趟排序:");for(intn=0;n<arr.length;n++){System.out.print(arr[n]+"");}System.out.println();}
时间: 2024-11-05 06:05:26