排序算法笔记-快速排序

排序算法笔记-快速排序_第1张图片

文章目录

  • 笔记
    • 简介
    • 时间复杂度
    • 空间复杂度
    • 解题模版
    • 练习题

笔记

简介

快速排序:确定分界数,左边小于分界,右边大于分界数,通过递归来不断重置分界数划分区域,直至完成排序

时间复杂度

最优 n*logn
最差 n^2

空间复杂度

原地排序,所以空间复杂度是 O(1)

解题模版

细节不在阐述,自己理解一下

class Solution {
    public void sortColors(int[] nums) {
        if(nums == null || nums.length <=1)
            return ;
        quicksort(nums,0,nums.length-1);

    }
    private void quicksort(int[] nums,int left,int right){
        if(left>right){
            return;
        }
        int pivot = partition(nums,left,right);
        quicksort(nums,left,pivot-1);
        quicksort(nums,pivot+1,right);
    }
    private int partition(int[] nums,int left,int right){
        int pivot1 = nums[right];
        int i = left-1;
        for(int j = left;j<right;j++){
            if(nums[j]<=pivot1)
                i++;
            swap(nums,i,j);
        }
        swap(nums,i+1,right);
        return i+1;
    }
    private void swap(int[] nums,int i,int j){
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}

练习题

力扣912. 排序数组
套用模版,完美解决

class Solution {
    public int[] sortArray(int[] nums) {
        if(nums == null || nums.length <= 1){
            return nums;
        }
        quickSort(nums,0,nums.length-1);
        return nums;
    }
    private void quickSort(int[] nums,int start,int end){
        if(start >= end){
            return;
        }
        int pivot = partition(nums,start,end);
        quickSort(nums,start,pivot-1);
        quickSort(nums,pivot+1,end);
    }
    private int partition(int []nums,int start,int end){
        int pivot = nums[end];
        int i = start -1;
        for(int j =start;j<end;j++){
            if(nums[j]<= pivot){
                i++;
                swap(nums,i,j);
            } 
        }
        swap(nums,i+1,end);
        return i+1;
    }
    private void swap(int[] nums,int i,int j){
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}

力扣215 数组中的第K个最大元素
题中要求使用O(n)时间复杂度,因此可以借助快排的分区思想,因为求第K大,并不是全部排序,平均下来时间复杂度满足条件。

class Solution {
    public int findKthLargest(int[] nums, int k) {
        if(nums == null || nums.length == 0 || k<=0 || k>nums.length)
            return -1;
        int targetIndex = nums.length-k;
        int left =0;
        int right = nums.length-1;
        while(true){
            int pivotIndex = partition(nums,left,right);
            if(pivotIndex == targetIndex){
                return nums[pivotIndex];
            }else if(pivotIndex <targetIndex){
                left = pivotIndex +1;
            }else{
                right = pivotIndex-1;
            }
        }
    }
    private int partition(int [] nums,int left,int right){
        int pivot = nums[right];
        int i = left-1;
        for(int j =left;j<right;j++){
            if(nums[j]<=pivot){
                i++;
                swap(nums,i,j);
            }
        }
        swap(nums,i+1,right);
        return i+1;
    }
    private void swap(int[] nums,int i,int j){
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j]= temp; 
    }
}

力扣75. 颜色分类

看到题目要求,非常适合使用快排,直接默写

class Solution {
    public void sortColors(int[] nums) {
        if(nums == null || nums.length <=1)
            return ;
        quicksort(nums,0,nums.length-1);
    
    }
    private void quicksort(int[] nums,int left,int right){
        if(left>right){
            return;
        }
        int pivot = partition(nums,left,right);
        quicksort(nums,left,pivot-1);
        quicksort(nums,pivot+1,right);
    }
    private int partition(int[] nums,int left,int right){
        int pivot1 = nums[right];
        int i = left-1;
        for(int j = left;j<right;j++){
            if(nums[j]<=pivot1){
                i++;
                swap(nums,i,j);
            }
        }
        swap(nums,i+1,right);
        return i+1;
    }
    private void swap(int[] nums,int i,int j){
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}

你可能感兴趣的:(暑期算法冲刺,排序算法,算法,数据结构)