简体   繁体   English

Radix使用Java中的队列对字符串数组进行排序

[英]Radix Sort an array of strings using a queue in Java

I don't completely understand radix sort so that is making this more difficult for me to write this program. 我不完全了解基数排序,因此这使我编写此程序更加困难。 I need to sort an array of strings that is read from a .txt file. 我需要对从.txt文件读取的字符串数组进行排序。 I was able to read the file and enter the strings into an array. 我能够读取文件并将字符串输入到数组中。 A string can contain letters or special characters. 字符串可以包含字母或特殊字符。 I need help with writing the code for sorting the array. 我在编写用于对数组进行排序的代码方面需要帮助。 It feels like I'm close to the having the correct code, but I'm stuck and don't know what else to do. 感觉我已经接近拥有正确的代码,但是我被困住了,不知道该怎么办。 It would be greatly appreciated if I could get help and point me in the right direction. 如果能得到帮助并指出正确的方向,将不胜感激。

Each string has the same amount of characters. 每个字符串具有相同数量的字符。 I am also using a stackPackage that contains a queue class. 我也在使用包含队列类的stackPackage。 I need to modify the code the my professor gave me to sort strings. 我需要修改教授给我的用于对字符串进行排序的代码。

Here is the code I started with: 这是我开始的代码:

import queuepackage.*;

public class Radix { 公共类基数{

public static void main(String[] args) {
    int[] array = {143,934,782,687,555,222,111,213,842,2000};
    printArray(array);
    radixSort(array, 1000);
    printArray(array);
}   

public static void printArray(int[] array) {
    for (int i = 0; i < array.length; i++) {
        System.out.print(array[i] + ", ");
    }   
    System.out.println();
}   

public static void radixSort(int[] array, int maxPowerOf10) {

    Queue[] queueArray = new Queue[10];

    for (int queueNum = 0; queueNum < 10; queueNum++) {
        queueArray[queueNum] = new Queue();
    }   

    for (int powerOf10 = 1; powerOf10 <= maxPowerOf10; powerOf10 = powerOf10 * 10) {
        for (int item = 0; item < array.length; item++) {
            int digit = getDigit(array[item], powerOf10);
            queueArray[digit].enqueue(new Integer(array[item]));
        }   
        int item = 0;
        for (int queueNum = 0; queueNum < 10; queueNum++) {
            while (!queueArray[queueNum].isEmpty()) {
                    array[item] = ((Integer) queueArray[queueNum].dequeue()).intValue();
                    item++;
            }   
        }   
    }   
}   

public static int getDigit(int number, int powerOf10) {
    return (number/powerOf10)%10;
}   

} }

This is what I have. 这就是我所拥有的。

length = length of the array 长度=数组的长度

wordLen = length of a string in the array wordLen =数组中字符串的长度

Here is my current code of the radixSort method: 这是我当前的radixSort方法代码:

public static void radixSort(String[] array, int length, int wordLen) {
    Queue[] queueArray = new Queue[256];
    for (int queueNum = 0; queueNum < 256; queueNum++) {
        queueArray[queueNum] = new Queue();
    }
    for (int len = 0; len < wordLen; len++) {
        for (int item = 0; item < length; item++) {
            int letter = array[item].charAt(len);
            queueArray[letter].enqueue(new String(array[item]));
        }
        int item = 0;
        for (int queueNum = 0; queueNum < 256; queueNum++) {
            while (!queueArray[queueNum].isEmpty()) {
                array[item] = ((String) queueArray[queueNum].dequeue()).toString();
                item++;
            }   
        }   
    }   
}

It's almost working, you just need to iterate the word backwards, look at the second for loop 几乎可以正常工作了,您只需要向后迭代单词,查看第二个for循环

public static void radixSort(String[] array, int length, int wordLen) {
    Queue[] queueArray = new Queue[256];
    for (int queueNum = 0; queueNum < 256; queueNum++) {
        queueArray[queueNum] = new Queue();
    }
    for (int len = wordLen-1; len >= 0; len--) {
        for (int item = 0; item < length; item++) {
            int letter = array[item].charAt(len);
            queueArray[letter].enqueue(new String(array[item]));
        }
        int item = 0;
        for (int queueNum = 0; queueNum < 256; queueNum++) {
            while (!queueArray[queueNum].isEmpty()) {
                array[item] = ((String) queueArray[queueNum].dequeue()).toString(); // Here you need to swap the 
                item++;
            }   
        }   
    }   
}

If you iterate it in the regular way you are losing the previous information, so the last characters are the most important 如果按常规方式进行迭代,则会丢失以前的信息,因此最后一个字符是最重要的

Good luck! 祝好运!

The only mistake in your code that I can see is that in RadixSort you wanna start with sorting by the least significant digit and move up to more significant digits as you go. 我可以看到的代码中的唯一错误是,在RadixSort中,您要从按最低有效位开始排序,然后逐步升至更高的有效位。 In this case however you are starting at the left and going right which is the wrong way around for natural order String sorting. 但是,在这种情况下,您是从左侧开始并向右移动,这对于自然顺序字符串排序是错误的方法。
This however can be easily fixed by changing for (int len = 0; len < wordLen; len++) into for (int len = wordLen - 1; len >= 0; len--) . 但是,可以通过将for (int len = 0; len < wordLen; len++)更改for (int len = 0; len < wordLen; len++) for (int len = wordLen - 1; len >= 0; len--)

for (int len = 0; len < wordLen; len++)更改for (int len = 0; len < wordLen; len++) for (int len = wordLen - 1; len >= 0; len--)可以使我的程序执行应有的功能

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

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