Java排序算法---快速排序
java快排简单实现
public class QuickSort {
public static void quickSort(int[] arr,int low,int high) {
int i,j,temp,t;
if(low>high){
return;
}
i=low;
j=high;
temp=arr[low];
while(i<j){
while(temp<=arr[j]&&j>i){
j--;
}
while(temp>=arr[i]&&j>i){
i++;
}
if(i<j){
t=arr[j];
arr[j]=arr[i];
arr[i]=t;
}
}
arr[low]=arr[i];
arr[i]=temp;
quickSort(arr,low,j-1);
quickSort(arr,j+1,high);
}
public static void main(String[] args){
int[] arr = {10,7,2,4,7,62,3,4,2,1,8,9,19};
quickSort(arr, 0, arr.length-1);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
}
输出结果为1 2 2 3 4 4 7 7 8 9 10 19 62