Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中带逆序的基数排序_Java_Sorting_Data Structures_Radix Sort - Fatal编程技术网

Java中带逆序的基数排序

Java中带逆序的基数排序,java,sorting,data-structures,radix-sort,Java,Sorting,Data Structures,Radix Sort,我理解基数排序有困难。我应该把单词的最后一个字母排序,就像从右向左排序一样,直到没有其他字母为止 文本文件如下所示 条 猫 苹果 缺陷 齿轮 跳跃 鹿角 踝关节 熊 我的输出是这样的 脚踝 鹿角 苹果 酒吧 熊 缺陷 跳跃 猫 cog 但我应该得到这样的输出 条 缺陷 猫 齿轮 熊 踝关节 苹果 跳跃 鹿角 感觉我已经接近拥有正确的代码了,但是我被卡住了,不知道还能做什么。如果我能得到帮助并为我指明正确的方向,我将不胜感激 这是我所做的代码 RadixSort.java public cla

我理解基数排序有困难。我应该把单词的最后一个字母排序,就像从右向左排序一样,直到没有其他字母为止

文本文件如下所示

条 猫 苹果 缺陷 齿轮 跳跃 鹿角 踝关节 熊

我的输出是这样的

脚踝 鹿角 苹果 酒吧 熊 缺陷 跳跃 猫 cog

但我应该得到这样的输出

条 缺陷 猫 齿轮 熊 踝关节 苹果 跳跃 鹿角

感觉我已经接近拥有正确的代码了,但是我被卡住了,不知道还能做什么。如果我能得到帮助并为我指明正确的方向,我将不胜感激

这是我所做的代码

RadixSort.java

  public class RadixSort {
      public static void main(String[]args) throws FileNotFoundException{
    Linkedlist[] allNameLinkedList = new Linkedlist[26]; // create an array 
        of LinkedList for 26 letters in alphabets
    int count = 0;
    // initialize all the elements in the array to new LinkedList
    for (int i = 0; i < allNameLinkedList.length; i++) {
        allNameLinkedList[i] = new Linkedlist();
    }
    Scanner scan = new Scanner(new File("words.txt"));

    while(scan.hasNextLine())
    {
        String currentname = scan.nextLine();
        for(int i = 0; i < 26; i++){
            if(currentname.charAt(2) == (char)(i+97))
            {
                allNameLinkedList[i].addNodeToTheEndOfTheList(currentname);
            }
        }
        count++;
    }

    // copy sorted nodes to new LinkedList called container
    Linkedlist container = new Linkedlist();
    for (int i = 0; i < 26; i++) {
        Node n = allNameLinkedList[i].front;

        while(n != null){
            container.addNodeToTheEndOfTheList(n.name);
            n = n.next;
        }
    }
    // empty all the elements of array
    for (int i = 0; i < allNameLinkedList.length; i++) {
        allNameLinkedList[i] = new Linkedlist();
    }

    Node m = container.front;
    while(m!=null)
    {
        String currentname = m.name;
        for(int i = 0; i < 26; i++){
            if(currentname.charAt(1) == (char)(i+97))
            {
                allNameLinkedList[i].addNodeToTheEndOfTheList(currentname);
            }
        }
        m = m.next;
        count++;
    }
    container = new Linkedlist();
    for (int i = 0; i < 26; i++) {
        m = allNameLinkedList[i].front;

        while(m!=null){
            container.addNodeToTheEndOfTheList(m.name);
            m = m.next;
        }
    }
    for (int i = 0; i < allNameLinkedList.length; i++) {
        allNameLinkedList[i] = new Linkedlist();
    }
    m = container.front;

    while(m!=null)
    {
        String currentname = m.name;

        for(int i = 0; i < 26; i++){
            if(currentname.charAt(0) == (char)(i+97))
            {
                allNameLinkedList[i].addNodeToTheEndOfTheList(currentname);
            }
        }
        m = m.next;
        count++;
    }
    container = new Linkedlist();
    for (int i = 0; i < 26; i++) {
        m = allNameLinkedList[i].front;

        while(m!=null){
            System.out.println(m.name);
            container.addNodeToTheEndOfTheList(m.name);
            m = m.next;
        }
    }
    scan.close();
    System.out.println("The total number of comparisions was :"+count);
    }
    }
公共类RadixSort{
公共静态void main(字符串[]args)引发FileNotFoundException{
Linkedlist[]allNameLinkedList=新建Linkedlist[26];//创建一个数组
LinkedList中的26个字母
整数计数=0;
//将数组中的所有元素初始化为新的LinkedList
for(int i=0;i
您的问题是只对第一个字符进行排序

while(m!=null)
    {
        String currentname = m.name;

        for(int i = 0; i < 26; i++){
            // charAt(0) is first character in String
            if(currentname.charAt(0) == (char)(i+97)) 
            {
                allNameLinkedList[i].addNodeToTheEndOfTheList(currentname);
            }
        }
        m = m.next;
        count++;
    }
while(m!=null)
{
字符串currentname=m.name;
对于(int i=0;i<26;i++){
//字符(0)是字符串中的第一个字符
if(currentname.charAt(0)=(char)(i+97))
{
allNameLinkedList[i]。将节点添加到内部列表(当前名称);
}
}
m=m.next;
计数++;
}
您必须遍历每个字符,而不仅仅是第一个字符。这解释了为什么排序是按字母顺序的。我无法理解它是如何按照完美的词典顺序排列的。代码片段将仅根据字符(0)将数据排序到它们的链表“bucket”中

您的输入和输出似乎没有加在一起,因为输入是未排序的,而您的输出实际上是字母基数排序的结果:MSD基数排序

最重要的数字是字母比较所需的数字,因为在字典顺序的情况下,较高的比较大小是第一个字符。其他排序可能是LSD(最低有效位),就像整数比较一样,但应该警告您,LSD基数排序在这里不够,因为您必须担心不同长度的字符串

使用MSD基数排序,没什么大不了的。你只要确保你没有超出一个单词的范围,并且在它被放置后不要移动它,除非它被另一个单词移动。使用LSD,您必须向后索引,首先检查您的
当前字长-当前索引>0
,然后继续排序,实际字符为
字符(字长-当前索引)
,但你的最终结果可能永远不会是真正按字母顺序排列的——我认为LSD基数排序在字母排序上没有什么意义,除非它是一个概念证明,你可以按这种方式排序,或者是一个以这种方式特别给你的赋值,这样你的最终结果就只能部分排序


其次,在每次迭代之后进行排序的逻辑似乎并不存在。您必须在每次迭代时从每个bucket中进行排序,以确保所有内容都是按顺序排序的。

为了便于将来参考,最好包含一个简短的、自包含的、正确的示例,以最少的文本量显示出错的地方。这将包括您的链接列表代码。要了解更多关于什么是可取的信息,您可以浏览太多的代码,我不太理解您的问题,您对单词进行了排序,您实际需要什么