Java实现快速排序
2018-07-28 本文已影响1人
奔跑的蛙牛
import java.util.Arrays;
public class QuickSort {
public void sort(int[] arr ,int left,int right){
if(left>right) return;
int base = arr[left];
int low = left;
int hign = right;
while (left<right){
while (right>left && arr[right]>base){
right--; //2
}
while (left<right && arr[left]<=base){
left++; // 1
}
if(left<right){
int temp = arr[left]; //temp = 10
arr[left] = arr[right]; // arr3 = 10
arr[right] = temp; // arr
}
}
int p = base;
arr[low]=arr[left];
arr[left]=p;
sort(arr,low,left-1);
sort(arr,left+1,hign);
}
public static void main(String[] args){
int[] arr = {5,10,4,8,9};
QuickSort qs = new QuickSort();
qs.sort(arr,0,4);
System.out.print(Arrays.toString(arr));
}
}