快速排序;
public class QuickSort {
public static void exchange(int [] arr, int i, int j)
{
int temp;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void sort(int [] arr , int low , int high)
{
int i,j,temp;
i = low;
j = high;
if(i > j){
return;
}
temp = arr[low];
boolean flag = true;
while( i < j ){
if(flag){
if(arr[j] < temp){
exchange(arr, i, j);
flag = false;
}else{
j --;
}
}else{
if(arr[i] > temp){
exchange(arr, i, j);
flag = true;
}else{
i ++;
}
}
}
sort(arr,low,i-1);
sort(arr,j+1,high);
}
public static void main(String[] args) {
int [] arr = {1,2,6,3,7,8,1,9,1};
sort(arr, 0, arr.length-1);
for (int i : arr) {
System.out.print(i+" ");
}
}
}