[英]Radix sort with reverse order in Java
我无法了解基数排序。 我应该对单词的最后一个字母进行排序,就像从右到左进行排序,直到没有更多的字母为止。
文本文件如下所示
棒猫苹果虫子齿轮雀跃鹿角脚踝熊
我的输出是这样的
脚踝鹿角苹果栏熊bug雀跃猫齿轮
但是我应该得到这样的输出
bar bug猫齿轮熊脚踝苹果雀跃鹿角
感觉我已经接近拥有正确的代码,但是我被困住了,不知道该怎么办。 如果能得到帮助并指出正确的方向,将不胜感激
这是我做的代码
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);
}
}
您的问题是仅排序第一个字符。
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++;
}
您必须遍历每个字符,而不仅仅是第一个。 这解释了为什么您的排序按字母顺序排列。 这在我的词典编纂顺序中是完美的。 该代码段将仅基于charAt(0)将数据分类到其链接列表“存储桶”中。
您的输入和输出似乎没有加起来,因为输入未排序,并且您的输出实际上是您应该对字母基数排序所拥有的:MSD Radix排序。
最重要的数字是您想要进行字母比较的原因,因为在词典顺序中,较高的比较幅度是第一个字符。 其他排序方式可能是LSD(最低有效数字),例如在整数比较中,但应提醒您,此处的LSD Radix排序方式不足,因为您必须担心长度不同的字符串。
使用MSD Radix排序,没什么大不了的。 您只需要确保不超出一个单词的范围,并且放置它之后就不要移动它,除非它被另一个单词移动。 使用LSD时,您必须向后索引,首先检查current word length - current index > 0
然后再继续以您的实际charAt为charAt(word length - current index)
进行排序,但是最终结果可能永远不会真正按字母顺序排序-在按字母顺序排列的LSD基数排序中,我看不出什么用处,除非这是概念上的证明,您可以按照这种方式进行排序,也可以以这种方式专门分配给您的作业,这样您的最终结果只能部分排序。
其次,您的逻辑在每次迭代后似乎都不存在。 您必须在每次迭代时从每个存储桶中进行排序,以确保所有内容都按排序顺序进行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.