【问题标题】:Can you store multiple integers at one array index?您可以在一个数组索引中存储多个整数吗?
【发布时间】:2012-11-30 01:29:20
【问题描述】:

我正在尝试进行基数排序,我见过的一些算法有一个 buckets[] 数组,它应该将多个整数保存到 bucket 数组的一个索引中,这是我所指的算法:

真的可以在一个索引中包含多个整数吗?怎么会这样?
还是有更简单的基数排序算法?

【问题讨论】:

  • 在 Java 中,bucket 可能是 List 数据结构
  • “真的可以在一个索引中包含多个整数吗?”。是的,一种方法是在每个索引处设置 Buckets

标签: java arrays sorting radix


【解决方案1】:

bucket 本身就是一个int[](或List 或任何可以存储多个项目的东西)。

你不能在 1 个索引中放更多的东西。

int[] array = new array[6];
int value = array[5];

如果有多个 int 将不再起作用。

最简单的可能是使用int[][] 数组。现在左框中的每个索引都指向整个数组。这些数组的长度也可以不同:

Java Jagged Array

【讨论】:

    【解决方案2】:

    是的,可以将多个int 添加到一个数组中,但您需要有一个数组,其中每个项目都是Object 而不是int

    例如...

    // the items to store in the array, which contain 3 ints
    public class Bucket {
        int number1 = -1;
        int number2 = -1;
        int number3 = -1;
    
        public void addInt(int number){
            if (number1 == -1){
                number1 = number;
            }
            else if (number2 == -1){
                number2 = number;
            }
            else if (number3 == -1){
                number3 = number;
            }
        }
    }
    
    // the array, as used in other classes
    Bucket[] bArray = new Bucket[6]; // 6 items in the array, where each item is a Bucket that contains 3 ints
    
    // assigning ints into the array
    bArray[2].addInt(56); // add the int '56' to the bucket at index '2' of the array
    
    // You could also use other Array-like structures
    ArrayList<Bucket> bList = new ArrayList<Bucket>();
    

    当然,如果您的存储桶中并非总是有 List 作为其变量,而不是单独的 ints。

    你也可以使用多维数组...

    // creating the buckets
    int[][] buckets = new int[6][3];
    
    // assigning ints into the array
    bArray[2][0] = 56; // add the int '56' to the bucket at index '2' of the array, position '0'
    

    但是,如果您开始使用不同大小的存储桶,这会变得有点混乱,并且您需要进行更多的错误检查以确保...

    1. 当您尝试访问存储桶中的项目时,它们不为空
    2. 当您向存储桶添加数字时,您需要检测第二维中下一个为空的位置,因此您不会覆盖已经存在的ints。

    如果是出于这些原因,我建议在多维数组上使用基于对象的数组。

    【讨论】:

      【解决方案3】:

      两种情况创建桶

      • 数字不是唯一的
      • 基数排序先对每个数字位置(十进制中的个位、十位、百位)进行排序,然后再继续到下一个位 - 因此,如果第一个数字上的排序匹配,则为 003 和 019。

      第一种情况实际上只是第二种情况的退化情况。

      请注意,根据您对数字进行排序的顺序,有几个基数排序变体。

      也回答问题的数据结构部分 - 不,您不能也不会在每个索引处存储多个值。相反,每个桶通常表示为数组的子序列。然后每个桶由其起点的偏移量表示(终点可以是隐式的)。

      【讨论】:

        【解决方案4】:

        除了可以将存储桶表示为列表或数组之外,还可以使用数组切片。在这种情况下,目标数组与输入数组的总大小相同。例如,存储桶 0 中有 2 个元素,存储桶 1 中有 2 个元素,存储桶 2 中有 3 个元素,存储桶 3 中有 1 个元素,存储桶 5 中有 2 个元素。

        Bucket 0 : d[0], d[1]
        Bucket 1 : d[2], d[3]
        Bucket 2 : d[4], d[5], d[6]
        Bucket 3 : d[7]
        Bucket 5 : d[8], d[9]
        

        这样做,排序必须跟踪每个桶的下一个索引。

        【讨论】:

          【解决方案5】:

          哇,数组和列表不是实现基数排序的方法。是的,列表可以工作,但它会减慢它的速度,当我这样做时,它比合并排序慢。最好的方法是实现一个频率数组作为每个字节计数排序的一部分。我发布了我的代码here,仅用于参考并行排序。希望对您有所帮助。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-02-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-12-02
            • 2016-03-24
            相关资源
            最近更新 更多