Leetcode学习笔记:#912. Sort an Array
Given an array of integers nums, sort the array in ascending order.
实现:
private void quickSort(int[] array, int left, int right){
if (left >= right){
return;
}
Random rand = new Random();
int pivotIndex = left + rand.nextInt(right - left + 1);
swap(array, pivotIndex, right);
int i = left, j = right - 1;
while(i <= j){
if(array[i] <= array[right]){
i++;
} else {
swap(array, i, j);
j--;
}
}
swap(array, right, i);
quickSort(array, left, i - 1);
quickSort(array, i + 1, right);
}
private void swap(int[]array, int i, int j){
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
思路:
快速排序,要默写还是非常难的