参考资料
复习基础排序算法(Java)
快速排序
import java.util.Random;
class Solution {
private static final Random RANDOM = new Random();
public int[] sortArray(int[] nums) {
quickSort(nums,0,nums.length-1);
return nums;
}
public void quickSort(int[] nums,int start,int end){
if(start >= end){
return;
}
int k = partition(nums,start,end);
quickSort(nums,start,k-1);
quickSort(nums,k+1,end);
}
public int partition(int[] nums,int start,int end){
int randomIndex = RANDOM.nextInt(end - start + 1) + start;
swap(nums, start, randomIndex);
int priot = nums[start];
int index = start;// [start,index-1]<priot,[index+1,end]>=priot
for(int i= start+1;i<=end;i++){
if(nums[i]<priot){
index++;
swap(nums,i,index);
}
}
swap(nums,start,index);
return index;
}
public void swap(int[] nums,int i, int j){
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
}
插入排序
归并排序