收录一些又长又臭,还可能要手写的模板算法题(说的就是你快排)
public class QuickSort {
public static void quickSort(int[] nums, int low, int high) {
if (low < high) {
int pivotIndex = partition(nums, low, high);
quickSort(nums, low, pivotIndex - 1);
quickSort(nums, pivotIndex + 1, high);
}
}
private static int partition(int[] nums, int low, int high) {
int pivot = nums[low];
int i = low;int j = high;
while(i!=j){
while(i<j&&nums[j]>=pivot) j--;
while(i<j&&nums[i]<=pivot) i++;
if(i<j){
int t = nums[i];nums[i] = nums[j];nums[j]=t;
}
}
nums[low] = nums[i];
nums[i] = pivot;
return i;
}
public static void main(String[] args) {
int[] nums = {9, 7, 5, 11, 12, 2, 14, 3, 10, 6};
quickSort(nums, 0, nums.length - 1);
for (int num : nums) {
System.out.print(num + " ");
}
}
}