Im无法理解基数排序。我应该整理这个词的最后一个字母,就像从右向左整理,直到没有更多的字母被留下。在Java中使用相反顺序的基数排序
文本文件看起来像这样
酒吧 猫 苹果 错误 COG 跳跃 鹿茸 脚踝 熊
我的输出这样
脚踝 鹿茸 苹果 酒吧 熊 错误 跳跃 猫 COG
但我应该得到这样
酒吧 错误 猫 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);
}
}
为了将来的参考,包括一个简短的,独立的,正确的例子,用最少量的文本显示哪里出错是明智的。这将包括您的链接列表代码。 欲了解更多关于什么是可取的信息,你可以看看http://sscce.org/ – dddJewelsbbb
太多的代码,我不明白你的问题,你排序的话,你真正需要什么? “你应该得到的输出”与基数排序有什么关系? – Shadov