简体   繁体   中英

Radix sort with reverse order in Java

Im having trouble understanding the radix sort. I am supposed to sort out the last letter of the word like sorting out from right to left until there are no more letters left.

The text file look like this

bar cat apple bug cog caper antler ankle bear

My output like this

ankle antler apple bar bear bug caper cat cog

But I am supposed to get the output like this

bar bug cat cog bear ankle apple caper antler

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

Here is the code what I did

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);
    }
    }

Your problem is that are sorting only the first character.

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++;
    }

You have to iterate across every character, not just the first. This explains why your sort is in alphabetical order. How it's in perfect lexicographical order is beyond me. The code snippet will only sort the data into their linked list "buckets" based on charAt(0).

Your input and output seems like it doesn't add up, because input is unsorted, and your output is actually what you're supposed to have for an alphabetic radix sort: MSD Radix sort.

Most significant digit is what you want for alphabetic comparisons because in the case of lexicographical order, higher comparative magnitudes are the first characters. Other sorts may be LSD (least significant digit) like in integer comparison, but you should be warned that LSD Radix sort falls short here because you have to worry about different length strings.

With MSD Radix sort, no big deal. You just make sure you don't exceed the range of a word and don't move it after it's been placed unless it's being moved around by another word. With LSD, you must index backwards, checking first if your current word length - current index > 0 before continuing to sort with your actual charAt being charAt(word length - current index) , but your end result will likely never be truly alphabetically ordered - I see little purpose in LSD radix sort on alphabetizing unless it is a proof-of-concept that you can order that way or an assignment specifically given to you in that fashion such that your end result is only partially ordered.

Secondly, your logic to sort after each iteration doesn't appear to be there. You must sort from within each bucket upon every iteration to make sure that everything is in sorted order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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