【问题标题】:Radix sort using bitwise operations使用按位运算进行基数排序
【发布时间】:2013-04-04 00:27:09
【问题描述】:

我需要创建一个执行基数排序的方法,该方法使用按位运算符(>>、

文件看起来像这样:

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

这是我目前导入正在运行的文件的方法。我的所有其他方法都在工作,除了基数排序,我不确定从哪里开始按位运算。

我知道排序是如何工作的,但实现它似乎更具挑战性。

问候,

迈克

【问题讨论】:

    标签: java bit-manipulation binaryfiles radix-sort


    【解决方案1】:

    我会使用递归排序将数组划分为 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-26
      • 1970-01-01
      • 2023-03-07
      相关资源
      最近更新 更多