2013-04-04 15 views
0

我需要創建一個基數排序方法,它使用按位運算符(>>,<,<,&,|)從值中檢索各個位。使用按位運算的基數排序

文件看起來是這樣的:

0100 
1 
0011 
110 
0010 
101 
0001 
11 
0000 

我目前擁有的文件(尺寸爲未知)的讀取。起初我以整數讀取它們,但意識到我正在截斷前導零。所以我將它們存儲到一個String []中。

public static void readFile(String fileName) throws FileNotFoundException, IOException 
{ 
    File file = new File(fileName); 

    byte[] bytes = new byte[(int) file.length()]; 
    try (FileInputStream fis = new FileInputStream(file)) 
    { 
     fis.read(bytes); 
    } 

    String[] value = new String(bytes).split("\\s+"); 
    numbers = new String[value.length]; 
    System.arraycopy(value, 0, numbers, 0, value.length); 

} // end of import file 

這是我目前的方法來導入正在工作的文件。除了基數類型之外,我還有其他所有方法都可以工作,但我不確定從位操作開始。

我有排序如何工作的概念,但實現它似乎有點更具挑戰性。

問候,

麥克

回答

0

我將與遞歸分揀除以3點新的數組去:一個與下一位數= 0時,一個與下一個數字= 1和一個無下一位數字。因此,如下的數字將被排序,

Input 
0100 
1 
0011 
110 
0010 
101 
0001 
11 
0000 

Step 1 
Array "no next" (empty) 
Array "next = 0" 
0100 
0011 
0010 
0001 
0000 
Array "next = 1" 
1 
110 
101 
11 

Step 2 (only showing the array "next = 1") 
Array "no next" 
    1 
Array "next = 0" 
    101 
Array "next = 1" 
    110 
    11 

只要繼續下去,直到你得到0或1大小的數組,所以你知道,他們進行排序。然後,您將返回返回排序數組,如Quicksort