Sort --- bucket sort

7. bucket sort

  Several different front bucket sort ordering, it is not based on a comparison of the sort, but based on the condition data is sorted, we set as a quantitative empty bucket array, sequence search and the project into a respective bucket go not empty bucket for each sort, not empty a bucket from the project and then back into the original sequence, the end of the sort.

  Counting sequencing bucket sort based on the time complexity is O (n), the spatial complexity is also O (n)

public class Sort{
    public static void bucketSort(int []arr){
        if(arr==null||arr.length<2)
            return ;
        int max=Integer.MIN_VALUE;
        for(int i=0;i<arr.length;i++){
            max=Math.max(max,arr[i]);
        }
        int []bucket=new int[max+1]; //桶的下标代表数组中数值的大小,所以桶的长度要为数组中的最大值
        for(int i=0;i<arr.length;i++){
            bucket[arr[i]]++; //记数排序,相同值出现的次数
        }
        int i=0;
        for(int j=0;j<bucket.length;j++){
            while(bucket[j]-->0)
                arr[i++]=j  //将排好的数存会原数组
        }
    }
}

Based on a question of extending the concept of the barrel, for a disorderly array, we want to come after ordering the biggest difference between the two numbers adjacent. Time complexity requirements O (n)

public class MaxGap{
    public static int maxGap(int []nums){
        if(nums==null||nums.length<2)
            return 0;
        int len=nums.length;//记录数组的长度
        int min=Integer.MAX_VALUE; //数组中最小的元素
        int max=Integer.MIN_VALUE;//数组中最大的元素
        for(int i=0;i<len;i++){
            min=Math.min(min,nums[i]);
            max=Math.max(max,nums[i]);
        }
        if(min==max)
            return 0;
        //桶的数量为数组的长度加一,
        boolean []hasNum=new boolean[len+1]; //表示桶里是否有数字
        int []maxs=new int[len+1]; 
        int []mins=new int[len+1]; //每个桶中只存放这个桶内的最大值和最小值
        int bid=0; //桶的下标
        for(int i=0;i<len;i++){
            bid=bucket(nums[i],len,min,max); //nums[i]应该放到哪个桶里
            mins[bid]=hasNum[bid]?Math.min(mins[bid],nums[i]):nums[i];
            maxs[bid]=hasNum[bid]?Math.max(maxs[bid],nums[i]):nums[i];
            hasNum[bid]=true;
        }
        //计算相邻非空桶中左桶的最大值和右桶的最小值的差值,找到最大的返回。
        int res=0;
        int lastMax=maxs[0]; //非空左桶的最大值
        int i=1;
        for(;i<=len;i++){
            if(hasNum[i]){
                res=Math.max(res,mins[i]-lastMax);
                lastMax=maxs[i];
            }
        }
        return res;
    }
    public static int bucket(long num,long len,long min,long max){//计算num应该存放在哪个桶
        return (int)((num-min)*len/(max-min));
    }
}

The bucket sort extending Title:

  The number most frequently arise K

347. Top K Frequent Elements (Medium)

Given [1,1,1,2,2,3] and k = 2, return [1,2].

  Setting a plurality of buckets, each bucket same frequency number memory occurs, and subscripts represent the number of frequency of appearing with buckets, i.e. buckets stored in the i-th number of the frequency of occurrence is i.

After the number are placed in barrels, buckets traversal from back to front, the number k is the first to get the most number of frequencies k appear.

public List<Integer>topKFrequent(int []nums,int k){
    Map<Integer,Integer>map=new HashMap<>();
    for(int i=0;i<nums.length;i++){
        map.put(nums[i],map.getOrDefault(nums[i],0)+1);
    }
    List []bucket=new ArrayList[nums.length+1];//构建桶,桶的下标代表数字出现的频率
    for(int key:map.keySet){
        int freqence=map.get(key);
        if(bucket[frequence]==null){
            bucket[frequence]=new ArrayList<>();
            }
        bucket[frequence].add(key);
    }
    List<Integer>res=new ArrayList<>();
    for(int i=bucket.length-1;i>=0&&res.size()<k;i--){
        if(bucket[i]==null)
            continue;
        if(bucket[i].size<=k-res.size())
            res.addAll(bucket[i]);
        else
            res.addAll(bucket[i].subList(0.k-res.size()));
    }
    return res;
}

Guess you like

Origin www.cnblogs.com/yjxyy/p/11104318.html