简体   繁体   English

带有计数排序的基数排序错误地对转换为二进制的字母进行排序

[英]Radix sort with counting sort incorrectly sorts letters converted to binary

I'm having trouble understanding why my code for radix sort with counting sort doesn't correctly sort the input when I convert it to binary.我无法理解为什么当我将输入转换为二进制时,我的带有计数排序的基数排序代码不能正确地对输入进行排序。 I'm basically using the same code for letters represented as decimal and they work just fine but here it isn't even close.我基本上对表示为十进制的字母使用相同的代码,它们工作得很好,但在这里它甚至还差得远。

Below is the code that takes part in binary radix sort:下面是参与二进制基数排序的代码:

    static String[] countSort(String[] input, int position)
    {
        int[] count = new int[2];
        int n = input.length;

        char temp;
        for (String value : input) {
            temp = value.charAt(value.length()-1 - position);

            count[temp-'0']++;
        }

        for (int i = 1; i < 2; i++) {
            count[i] = count[i] + count[i - 1];
        }

        String[] output = new String[n];
        for (int i = n - 1; i >= 0; i--) {
            temp = input[i].charAt(input[i].length()-1 - position);

            output[count[temp-'0']-1] = input[i];
            count[temp-'0']--;
        }

        return output;
    }

    public static String[] radixSortBinary(String str, int stringLength) {

        //convert letters to binary
        char[] charArr = str.toCharArray();
        String[] array = new String[charArr.length];
        for (int i=0; i<charArr.length; i++)
            array[i] = Integer.toBinaryString(charArr[i]);

        System.out.println("Binary input:" + Arrays.toString(array));

        //iterate over each character position (starting from the least significant)
        for (int i = stringLength-1; i >= 0; --i) {
            array = countSort(array, i);
        }


        System.out.println("Binary output:" + Arrays.toString(array));

        //convert back to letters
        StringBuilder sb = new StringBuilder();
        for (int i=0; i<array.length; i++) {
            Arrays.stream(array[i].split("(?<=\\G.{7})")).forEach(s -> sb.append((char) Integer.parseInt(s, 2)));
            array[i] = sb.toString();
            sb.setLength(0);
        }

        return array;
    }

    public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    String input2 = scan.next();

    String[] result = radixSortBinary(input2, 7);
    System.out.println("Output:" + Arrays.toString(result));
}

console:安慰:

input: 
ababababababa
Binary input:[1100001, 1100010, 1100001, 1100010, 1100001, 1100010, 1100001, 1100010, 1100001, 1100010, 1100001, 1100010, 1100001]
Binary output:[1100001, 1100010, 1100001, 1100010, 1100001, 1100010, 1100001, 1100010, 1100001, 1100010, 1100001, 1100010, 1100001]
Output:[a, b, a, b, a, b, a, b, a, b, a, b, a]

another case:另一种情况:

input: 
abcdefgdftglkgfdj
Binary input:[1100001, 1100010, 1100011, 1100100, 1100101, 1100110, 1100111, 1100100, 1100110, 1110100, 1100111, 1101100, 1101011, 1100111, 1100110, 1100100, 1101010]
Binary output:[1100100, 1100100, 1100100, 1110100, 1101100, 1100010, 1101010, 1100110, 1100110, 1100110, 1100001, 1100101, 1100011, 1101011, 1100111, 1100111, 1100111]
Output:[d, d, d, t, l, b, j, f, f, f, a, e, c, k, g, g, g]

Any help would be greatly appreciated!任何帮助将不胜感激!

The bug is here:错误在这里:

//iterate over each character position (starting from the least significant)
for (int i = stringLength-1; i >= 0; --i) {
    array = countSort(array, i);
}

That code does not do what the comment says it does.该代码不执行注释所说的操作。 Actually this iterates over each character position starting from the most significant character.实际上,这会从最重要的字符开始遍历每个字符 position。 There is such as thing as MSB-radix sort, but that's different.有诸如 MSB-radix 之类的东西,但那是不同的。

Reversing that loop made it work correctly for me.反转那个循环使它对我来说正常工作。

By the way you should probably ensure that all binary strings have the same length, padded with leading zeroes if necessary.顺便说一句,您可能应该确保所有二进制字符串的长度相同,必要时用前导零填充。 Working directly on the bits of the integer representations of characters would not require that as an explicit step.直接处理 integer 字符表示的位不需要将其作为显式步骤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM