2015-12-21 30 views
0

我有一個我的數據結構類的最終項目,我無法弄清楚如何去做。我需要實現基數排序,我理解大部分的概念。但到目前爲止,我在網上找到的所有實現都嚴格使用整數,並且需要將其與我創建的其他類型(稱爲Note)一起使用,它是一個帶ID參數的字符串。 這是我到目前爲止,但不幸的是它沒有通過任何JUnit測試。使用節點而不是整數在Java中使用基數進行排序

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 
     } 

    } 

回答

0

代替

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

我應該用

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

和改變從void變量類型的RadixNote[].

相關問題