Radix Sort/Bucket Sort

algorithm thinking

1. Cardinality sorting is also called bucket sorting. The specific idea is to save the values ​​​​as subscripts of the array.
2. Take all the values ​​out and compare them. For example, if the value is m, it will be stored in the array subscripted as m.
3. Take out the compared array and it will be an array sorted by units, and then sort the sorted array by tens.
4. After comparing all the digits of tens, hundreds, and thousands, the sorting is completed.

Graphical sorting process

insert image description here

The idea of ​​implementing the two codes is the same, and the implementation method 1 can be directly rewritten as C or C++.

Code implementation 1

private int[] array = {
    
    23, 11, 7, 29, 33, 59, 8, 20, 9, 3, 2, 6, 10, 44, 83, 28, 5, 1, 0, 36};

/**
 * 基数排序,先按个位将所有数字按照个位的值放入0-9的二维数组中,依次取出之后再按十位
 * 如此循环直至个十百千等等所有位数遍历完为止
 */
private void radixSort() {
    
    

    // 定义二位数组用来存储每个基数以及基数下的数值
    int[][] temp;

    // 定义一维数组记录基数下保存了几位
    int[] position;

    int radix = 1;

    while (true) {
    
    
        position = new int[10];
        temp = new int[10][array.length];

        for (int i = 0; i < array.length; i++) {
    
    
            int value = (array[i] / radix) % 10;
            temp[value][position[value]] = array[i];
            position[value]++;
        }

        // 判断是否所有的数值都在0位上,都在0位上则表示排序完成
        if (position[0] == array.length) {
    
    
            break;
        }

        int index = 0;
        for (int i = 0; i < 10; i++) {
    
    
            for (int j = 0; j < position[i]; j++) {
    
    
                array[index] = temp[i][j];
                index++;
            }
        }

        radix = radix * 10;
    }
}

Code implementation 2

/**
 * 基数排序/桶排序
 */
public class BucktSort {
    
    

    /**
     * 待排序数组
     */
    private int[] originalArray;
    /**
     * 桶
     */
    private Map<String, List<Integer>> bucket;
    /**
     * 数组中,最长数字位数
     */
    private int maxLenght = 0;

    public BucktSort(int[] array) {
    
    
        originalArray = array;
        initBucket();
        getMaxLength();
    }

    private void initBucket() {
    
    
        if (null == bucket) {
    
    
            bucket = new HashMap<>(10);
        } else {
    
    
            bucket.clear();
        }
        for (int index = 0; index < 10; index++) {
    
    
            bucket.put(String.valueOf(index), new ArrayList<>());
        }
    }

    /**
     * 获取最长数字位数
     */
    private void getMaxLength() {
    
    
        for (int i = 0; i < originalArray.length; i++) {
    
    
            int temp = String.valueOf(originalArray[i]).length();
            if (temp > maxLenght) {
    
    
                maxLenght = temp;
            }
            continue;
        }
    }

    public int[] sort() {
    
    
        if (maxLenght == 0) {
    
    
            return originalArray;
        }

        int radix = 1;
        for (int index = 0; index < maxLenght; index++) {
    
    

            for (int i = 0; i < originalArray.length; i++) {
    
    
                String position = String.valueOf((originalArray[i] / radix) % 10);
                bucket.get(position).add(originalArray[i]);
            }

            int originalPosition = 0;
            for (int key = 0; key < 10; key++) {
    
    
                int j = 0;
                while (j < bucket.get(String.valueOf(key)).size()) {
    
    
                    originalArray[originalPosition] = bucket.get(String.valueOf(key)).get(j).intValue();
                    originalPosition++;
                    j++;
                }
            }

            initBucket();
            radix *= 10;
        }

        return originalArray;
    }

}

Guess you like

Origin blog.csdn.net/lrxb_123/article/details/115013190