[Java] 纯文本查看 复制代码 import java.util.Arrays;
import java.util.Random;
public class QuickSort {
public static void main(String[] args) {
int arr[] = {2,5,9,1,4,8,3,6,7,10};
quickSort(arr,0,arr.length-1);
System.out.println(Arrays.toString(arr));
}
private static void quickSort(int[] arr, int left, int right) {
if(left > right){
return;
}
int i = left;
int j = right;
int baseNum = arr[left];
while(i != j){
//从右边
while(arr[j] >= baseNum && j > i){
j--;
}
//从左边
while(arr[i] <= baseNum && i < j){
i++;
}
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
arr[left] = arr[i];
arr[i] = baseNum;
quickSort(arr,left,i-1);
quickSort(arr,i+1,right);
}
}
|