当前位置:网站首页>Bit group sort

Bit group sort

2022-06-22 08:25:00 ambition_ forever

1、 The premise of using digit group sorting is that the elements in the array cannot have duplicate numbers , And the array can be unordered , However, there is a maximum range limit for the occurrence of elements in an array .

2、 Epoch array , Store the elements of the original array in the specified bit of the pre created bit group .

3、 Re traverse each bit in the bit array if it is true Transformation of , Is attached to the final result .

//  Use BitSet Sort 
    private static String sortNums(Integer[] integers) {


        long start = System.currentTimeMillis();

        System.out.println(" Start sorting - Use BitSet Sort ");
        int len = integers.length;

        StringBuilder sb = new StringBuilder();
        BitSet bitSet = new BitSet(len);

        bitSet.set(0, len, false);

        for (Integer integer : integers) {
            bitSet.set(integer, true);
        }

        sb.append("[");
        for (int i = 0; i <= len; i++) {

            if (bitSet.get(i)) {
                sb.append(i);
            }
            if (bitSet.get(i + 1)) {
                sb.append(", ");
            }
        }
        sb.append("]");

        System.out.println(" Sort complete , Time consuming :" + (System.currentTimeMillis() - start) + " millisecond ");
        return sb.toString();
    }

public static void main(String[] args) {

        Integer[] integers = {3, 2, 1, 4, 6, 5, 0};

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

        String sortNums = sortNums(integers);

        System.out.println(sortNums);
    }

        

原网站

版权声明
本文为[ambition_ forever]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202220528291925.html