快速排序

问题:

Input:
s = 7, nums = [5,3,1,7,5,20,2,4,3,2,2,1,2,4,3]
Output:
[1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 5, 5, 7, 20]
/*
例子:
3,8,7,1,2
2,8,7,1,3
2,3,7,1,8
2,1,7,3,8
2,1,3,7,8
    
1,2,3,7,8
*/

状态码朴素写法:

import java.util.Arrays;

public class test {
    public static void main(String[] args) {
        int[] nums= {5,3,1,7,5,20,2,4,3,2,2,1,2,4,3};
        quicklySort(nums,0,nums.length-1);
        //Arrays.sort(nums);
        System.out.println(Arrays.toString(nums));
    }
    public static int[] quicklySort(int[] nums,int prefix,int suffix) {
        //问题出在相同元素时不好判断//解决:相同的时候将其向前移或后移
        //问题出在换了一次之后,交换的顺序错误//解决:加入状态码
        int tempprefix=prefix;
        int tempsuffix=suffix;
        int status=0;
        if (prefix==suffix) return nums;
        
        while (prefix<suffix) {
            if (nums[prefix]>nums[suffix]) {
                swap(nums,prefix,suffix);
                status=status^1;//第一次交换状态为1
                if (status==1) {
                    prefix++;
                }else {
                    suffix--;
                }
            } else {
                if (status==1) {
                    prefix++;
                }else {
                    suffix--;
                }
            }
        }
        if (prefix==suffix) {
            quicklySort(nums, tempprefix,prefix-1);
            quicklySort(nums, suffix+1, tempsuffix);
        }
        return nums;
    }
    public static void swap(int[] nums,int prefix,int suffix) {
        int temp=nums[prefix];
        nums[prefix]=nums[suffix];
        nums[suffix]=temp;
    }
}

中值无状态码写法:

import java.util.Arrays;

public class test {
    public static void main(String[] args) {
        int[] nums= {5,3,1,7,5,20,2,4,3,2,2,1,2,4,3};
        nums=quicklySort(nums,0,nums.length-1);
        System.out.println(Arrays.toString(nums));
        
    }

    public static int[] quicklySort(int[] nums,int prefix,int suffix) {
        int centervalue=nums[(prefix+suffix)>>1];
        if (prefix>=suffix) return nums;
        
        int tempprefix=prefix-1;//进入循环,防止数组越界
        int tempsuffix=suffix+1;
        
        while (tempprefix<tempsuffix) {
            do {tempprefix++;} while (centervalue>nums[tempprefix]);
            do {tempsuffix--;} while (centervalue<nums[tempsuffix]);
            if (tempprefix<tempsuffix) {
                int temp=nums[tempprefix];
                nums[tempprefix]=nums[tempsuffix];
                nums[tempsuffix]=temp;
            }
        }
        //System.out.println(Arrays.toString(nums));
        quicklySort(nums, prefix,tempprefix-1);
        quicklySort(nums, tempsuffix+1, suffix);
        return nums;
    }
}

随机快排

import java.util.*;

public class Main{

    public static void main(String[] args) {
        int[] nums=new int[]{1,3,4,7,3,6,2,1,3,2,5,7,2,3,1,2,1};//[0, 1, 2, 2, 3, 3, 3, 4, 5, 6, 7, 7]
        quickSort(0,nums.length-1,nums);
        System.out.println(Arrays.toString(nums));
    }

    public static void quickSort(int l,int r,int[] nums){
        if(l>=r) return;
        swap(nums,l+(int)StrictMath.random()*(r-l+1),r);//StrictMath.random() 取值 [0.0,1.0)//随机为一个概率事件,使其复杂度为O(nlongn)
        int prefix=l-1;
        int suffix=r;
        for(int curr=l;curr<suffix;curr++){

            while(nums[curr]>nums[r] && curr<suffix){//若if在上,经过此过程后会造成小于当前的数出现
                swap(curr,--suffix,nums);
            }
            if(nums[curr]<nums[r] && curr<suffix){
                swap(curr,++prefix,nums);
            }
        }
        swap(r,suffix,nums);
        quickSort(l,prefix,nums);
        quickSort(suffix,r,nums);
    }
    public static void swap(int l,int r,int[] nums){
        int temp=nums[l];
        nums[l]=nums[r];
        nums[r]=temp;
    }
}

户枢不蠹,流水不腐

posted @ 2020-06-29 17:35  ZJfor  阅读(103)  评论(0编辑  收藏  举报