简体   繁体   English

Java中使用Node而不是整数实现Radix排序

[英]Implementation of Radix sort in Java using Nodes instead of integers

I have a final project for my Data Structures class that I can't figure out how to do. 我有一个数据结构课程的最终项目,我不知道该怎么做。 I need to implement Radix sort and I understand the concept for the most part. 我需要实现Radix排序,并且大部分时间我都了解该概念。 But all the implementations I found online so far are using it strictly with integers and I need to use it with the other Type that I have created called Note which is a string with ID parameter. 但是到目前为止,我在网上找到的所有实现都严格将其与整数配合使用,并且我需要将其与我创建的另一个Type(称为Note)结合使用,Note是一个ID参数的字符串。 Here is what I have so far but unfortunately it does not pass any JUnit test. 这是我到目前为止的内容,但是很遗憾,它没有通过任何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
        }

    }

Instead of 代替

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

I should have used 我应该用过

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

and change the variable type of Radix from void to Note[]. 并将Radix的变量类型从void更改为Note[].

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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