1. 快速排序

找基准元素,递归排序

  1. public int[] sortArray(int[] nums) {
  2. int n = nums.length;
  3. quickSrot(nums,0,n-1);
  4. return nums;
  5. }
  6. //快速排序
  7. public void quickSrot(int[] nums,int l,int r){
  8. if(l<r){
  9. int t = this.partition(nums,l,r);
  10. quickSrot(nums,l,t-1);
  11. quickSrot(nums,t+1,r);
  12. }
  13. }
  14. //一次调整
  15. public int partition(int[] nums,int l,int r){
  16. //选取第一个元素为基准元素
  17. int pivot = nums[l];
  18. while(l<r){
  19. while(l<r && nums[r]>=pivot){
  20. r-=1;
  21. }
  22. nums[l] = nums[r];
  23. while(l<r && nums[l]<=pivot){
  24. l += 1;
  25. }
  26. nums[r] = nums[l];
  27. }
  28. nums[l] = pivot;
  29. return l;
  30. }

2. 计数排序

构造count数组

  1. public int[] sortArray(int[] nums) {
  2. int max = Integer.MIN_VALUE;
  3. int min = Integer.MAX_VALUE;
  4. for(int x:nums){
  5. max = Math.max(max,x);
  6. min = Math.min(min,x);
  7. }
  8. //计数数组的长度
  9. int d = max - min+1;
  10. int[] count = new int[d];
  11. //统计对应元素个数
  12. for(int x:nums){
  13. count[x-min] += 1;
  14. }
  15. //数组后边的元素个数等于前面的元素之和,表示排序后的位置
  16. for(int i=1;i<d;i++){
  17. count[i] += count[i-1];
  18. }
  19. //倒序遍历nums,进行排序
  20. int[] ans = new int[nums.length];
  21. for(int i=nums.length-1;i>=0;i--){
  22. //给ans赋值
  23. ans[count[nums[i]-min]-1] = nums[i];
  24. //修改count
  25. count[nums[i]-min]--;
  26. }
  27. return ans;
  28. }

3. 堆排序

父结点索引:(i-1)/2
左孩子索引:2*i+1
右孩子索引:2*i+2

  1. //堆排序
  2. public int findKthLargest(int[] nums, int k) {
  3. //堆大小
  4. int heapSize = nums.length;
  5. //建堆
  6. buildHeap(nums,heapSize);
  7. //删除k-1次堆顶元素
  8. for(int i=nums.length-1;i>=nums.length-k+1;i--){
  9. swap(nums,0,i);
  10. heapSize--;
  11. adjustHeap(nums,0,heapSize);
  12. }
  13. return nums[0];
  14. }
  15. //建堆,从最后一个父结点往上依次进行堆调整
  16. public void buildHeap(int[] nums,int heapSize){
  17. //父结点索引为(i-1)/2
  18. for(int i=(heapSize-2)/2;i>=0;i--){
  19. adjustHeap(nums,i,heapSize);
  20. }
  21. }
  22. //堆调整
  23. public void adjustHeap(int[] nums,int i,int heapSize){
  24. //孩子结点索引为2*i+1,2*i+2
  25. int l = i*2+1, r = i*2+2, maxN = i;
  26. if(l<heapSize && nums[l]>nums[maxN]){
  27. maxN = l;
  28. }
  29. if(r<heapSize && nums[r]>nums[maxN]){
  30. maxN = r;
  31. }
  32. if(maxN != i){
  33. swap(nums,i,maxN);
  34. //递归调整
  35. adjustHeap(nums,maxN,heapSize);
  36. }
  37. }
  38. public void swap(int[] nums,int i,int j){
  39. int temp = nums[i];
  40. nums[i] = nums[j];
  41. nums[j] = temp;
  42. }

4. 归并排序

使用一个全局变量temp[]作为一个临时数组

  1. int[] temp;
  2. public int[] sortArray(int[] nums) {
  3. int n = nums.length;
  4. temp = new int[n];
  5. mergeSort(nums,0,n-1);
  6. return nums;
  7. }
  8. //分治
  9. public void mergeSort(int[] nums, int l, int r){
  10. if(l<r){
  11. int mid = (l+r)>>1;
  12. mergeSort(nums,l,mid);
  13. mergeSort(nums,mid+1,r);
  14. merge(nums,l,mid,r);
  15. }
  16. }
  17. //合并
  18. public void merge(int[] nums, int l, int mid, int r){
  19. //数组首下标
  20. int i=l,j=mid+1,cnt = 0;
  21. while(i<=mid && j<=r){
  22. if(nums[i]<=nums[j]){
  23. temp[cnt++] = nums[i++];
  24. }else{
  25. temp[cnt++] = nums[j++];
  26. }
  27. }
  28. while(i<=mid){
  29. temp[cnt++] = nums[i++];
  30. }
  31. while(j<=r){
  32. temp[cnt++] = nums[j++];
  33. }
  34. //复制到原数组
  35. for(int k=0;k<cnt;k++){
  36. nums[l+k] = temp[k];
  37. }
  38. }