快速排序
class Solution { public int[] sortArray(int[] nums) { quicksort(nums, 0, nums.length - 1); return nums; } public void quicksort(int[] nums, int le, int ri) { if (le < ri) { int pos = randomizedPartition(nums, le, ri); quicksort(nums, le, pos - 1); quicksort(nums, pos + 1, ri); } } public int randomizedPartition(int[] nums, int le, int ri) { int i = new Random().nextInt(ri - le + 1) + le; // 随机选一个作为我们的主元 exch(nums, ri, i); return partition(nums, le, ri); } public int partition(int[] nums, int le, int ri) { int pivot = nums[ri]; int i = le - 1; for (int j = le; j <= ri - 1; ++j) { if (nums[j] <= pivot) { i = i + 1; exch(nums, i, j); } } exch(nums, i + 1, ri); return i + 1; } private void exch(int[] nums, int i, int j) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } }