Sorting and counting radix sort of bucket sort

Sorting and counting radix sort of bucket sort

Bucket sort

A common sorting algorithm, works by an array assigned to a limited number of a bucket, and then each individual bucket sort (recursive will continue to use the bucket sort of way to sort or use other sorting algorithms).

Counting Sort

Counting sequencing is a special case of the bucket sort
Counting Sort

  • As can be seen from the figure, the numbers to be sorted in the range [1,9];
  • Create empty barrel 9, to be placed in the corresponding figures were sorted tub;
  • The tub is in turn in the order of the elements out of the tub;

Code:

public class CountSort {
    public static void main(String[] args) {
        int[] arr = {2, 4, 2, 3, 7, 1, 1, 1, 0, 5, 6, 9, 8, 7, 4, 0, 9};
        int[] result = sort(arr);
        System.out.println(Arrays.toString(result));
    }
    static int[] sort(int[] arr){
		//新建一个数组用来存放最终的结果
        int[] result = new int[arr.length];
        //10个桶
        int[] count = new int[10];
		//计数,计算每个桶中有多少个数字
        for (int i = 0; i < arr.length; i++) {
            count[arr[i]]++;
        }
        
        System.out.println(Arrays.toString(count));
        //累加数组,得到该桶的最后一个数字在最终数组中排在第几位的数字
        for (int i = 1; i < count.length; i++) {
            count[i] = count[i] + count[i-1];
        }
        System.out.println(Arrays.toString(count));
		//倒序迭代,将待排数字拿出放入累加数组中,得到结果数组中的位置,放入(建议拿出笔来算算)
        for (int i = arr.length-1; i >= 0; i--) {
            result[--count[arr[i]]] = arr[i];
        }
        return result;
    }
}

Radix Sort

Here Insert Picture Description

  • Algorithm idea: multi-keyword sort;
  • Similarly, the row of numbers will be based on this number of bits, ten one hundred, one thousand, respectively, into the bucket.;

Code:

public class RadixSort {

    public static void main(String[] args) {

        int[] arr = {421, 240, 225, 532, 305, 430, 124};

        int[] result = sort(arr);

        System.out.println(Arrays.toString(result));
    }

    public static int[] sort(int[] arr){
        int[] result = new int[arr.length];
        int[] count = new int[10];
		
        for (int i = 0; i < 3; i++) {
            int division = (int) Math.pow(10, i);
            System.out.println(division);
            //得到余数
            for (int j = 0; j < arr.length; j++) {
                int num = arr[j]/division % 10;

                System.out.print(num + " ");

                count[num]++;
            }
            System.out.println();
            System.out.println(Arrays.toString(count));

            for (int m = 1; m < count.length; m++) {
                count[m] = count[m] + count[m-1];
            }

            System.out.println(Arrays.toString(count));

            for (int n = arr.length-1; n >= 0; n--) {
                int num = arr[n]/division % 10;
                result[--count[num]] = arr[n];
            }
            System.arraycopy(result, 0, arr, 0, arr.length);
            Arrays.fill(count, 0);
        }
        return result;
    }
}

Mamba Never Out

R.I.P Mamba
Here Insert Picture Description

Published 25 original articles · won praise 8 · views 40000 +

Guess you like

Origin blog.csdn.net/FungLi_notLove/article/details/104089874