使用节点而不是整数在Java中实现Radix排序

时间:2015-12-21 12:26:53

标签: java sorting radix-sort

我的Data Structures类有一个最终项目,我无法弄清楚该怎么做。我需要实现Radix排序,我在很大程度上理解这个概念。但到目前为止,我在网上找到的所有实现都严格地使用整数,我需要将它与我创建的另一个类型Note一起使用,这是一个带有ID参数的字符串。 这是我到目前为止所做的,但不幸的是它没有通过任何JUnit测试。

package edu.drew.note;
public class RadixSort implements SortInterface {

     public static void Radix(Note[] note){

            // Largest place for a 32-bit int is the 1 billion's place
            for(int place=1; place <= 1000000000; place *= 10){
                // Use counting sort at each digit's place
                note = countingSort(note, place);
            }

            //return note;
        }

        private static Note[] countingSort(Note[] note, long place){ //Where the sorting actually happens
            Note[] output = new Note[note.length]; //Creating a new note that would be our output.

            int[] count = new int[10];  //Creating a counter

            for(int i=0; i < note.length; i++){ //For loop that calculates 
                int digit = getDigit(note[i].getID(), place);
                count[digit] += 1;
            }

            for(int i=1; i < count.length; i++){
                count[i] += count[i-1];
            }

            for(int i = note.length-1; i >= 0; i--){
                int digit = getDigit((note[i].getID()), place);

                output[count[digit]-1] = note[i];
                count[digit]--;
            }

            return output;

        }

        private static int getDigit(long value, long digitPlace){  //Takes value of Note[i] and i. Returns digit.
            return (int) ((value/digitPlace ) % 10);
        }


        public Note[] sort(Note[] s) {  //
             Radix(s);
             return s;
        }


        //Main Method
        public static void main(String[] args) {
            // make an array of notes
            Note q = new Note(" ", " ");
            Note n = new Note("CSCI 230 Project Plan", 
                    "Each person will number their top 5 choices.\n" +
                    "By next week, Dr. Hill will assign which piece\n" +
                    "everyone will work on.\n");
            n.tag("CSCI 230");
            n.tag("final project");

            Note[] Note = {q,n};
            //print out not id's
            System.out.println(Note + " Worked");
            //call radix
            Radix(Note);
            System.out.println(Note);
            //print out note_id's
        }

    }

1 个答案:

答案 0 :(得分:0)

而不是

public Note[] sort(Note[] s) {  //
         Radix(s);
         return s;
    }

我应该用

public Note[] sort(Note[] s) {  //
        s = Radix(s);
         return s;
    }

并将Radix的变量类型从void更改为Note[].