话不多说,直接上代码:
public class QuickSort {
public static void main(String[] args) {
// int[] array = new int[]{34, 234, 32, 12, 45, 122, 33, 89, 23, 12};
int[] array = new int[]{3, 24, 32, 12, 4, 122, 33, 89, 23, 12};
sort(array,0,array.length-1);
for (int i =0;i<=array.length-1;i++){
System.out.print(array[i]+" ");
}
}
public static void sort(int[] array, int lo, int hi){
if(hi<=lo){
return;
}
int j = partition(array, lo, hi);
sort(array,lo,j-1);
sort(array,j+1,hi);
}
public static int partition(int[] array, int lo, int hi){
int value = array[lo];
int left = lo;
int right = hi+1;
int mid;
while (true){
left++;
while (array[left]<value){
if(left==hi){
break;
}
left++;
}
right--;
while (array[right]>value){
if (right==lo)
break;
right--;
}
if(left>=right)
break;
exch(array,left,right);
}
exch(array,lo,right);
return right;
}
public static void exch(int[] array, int index1, int index2){//用于交换数组中两个数
int mid;
mid = array[index1];
array[index1] = array[index2];
array[index2] = mid;
}
}