【问题标题】:Inserting into array in order按顺序插入数组
【发布时间】:2013-12-18 23:01:55
【问题描述】:

简单来说,我正在进行的项目的一部分是让我获取一个按降序排列的数组并添加一个元素,以便该数组保持有序。起初我以为只要将元素添加到数组中,然后在实现 Comparable 后进行排序就很简单,但后来发现禁止使用任何类型的排序算法;收藏品也是如此。关于如何从这里开始的想法,有什么提示吗?

前:

int[] x = [ 7, 6, 6, 5, 3, 2, 1 ] 

add 4

[ 7, 6, 6, 5, 4, 3, 2, 1 ]

澄清说我并不是完全没有想法,只是有效率的想法。到目前为止我能推理的:

int[] bigger = new int[x.length];
int add = 4;
for (int i = 0; i < bigger.length; i++){
     if ( add > x[i] && add < x[i+1]){
         bigger[i] = x[i];
         bigger[i+1] = add;
     else{
         bigger[i] = x[i];
     }
}

我知道它会给x抛出一个IndexOutOfBounds错误,但我觉得一定有比这更简单的方法,对吧?

【问题讨论】:

  • 是的。这是一个提示,int[] newArray = new int[oldArray.length+1];,我建议你从mergeSort 中查看merge
  • 最好使用数组列表
  • 你能用Arrays.sort(yourArray)吗?
  • @Andrew 不幸的是没有。
  • 这非常简单。扫描预排序的数组,直到找到一个小于您要插入的元素的元素。在该位置插入新元素,将找到的元素和所有内容滑动到它的“右侧”更右侧。如果找不到比要插入的元素少的元素,请在末尾添加新元素。如果使用上面的即用即复制方式,找到插入点后退出搜索循环,进入循环完成复制。

标签: java arrays insertion


【解决方案1】:

一旦找到正确的位置,即 if(value 移动一个

   list[numElements] = value;
    for(int i = 0; i < numElements; i++){
        //May be I should use a nested loop?
        //for(k = 0; k <)
         if(value < list[i]){
             for(int j= numElements-1; j>=i; j--){
                  list[j+1]= list[j];
              }
              list[i] = value;
              break;
        }
    }
    numElements++;

【讨论】:

  • 如果列表是[5 4 2] 并且值为3,你不会在第一次迭代时停止吗?
  • 这假设您已经将数组复制到新的较大的数组中,作为单独的步骤。
【解决方案2】:
int add = 4;

int[] newArray = new int[oldArray.length + 1];

int oldPos = 0;
int newPos = 1;
while (oldPos < oldArray.length) {
    if (add > oldArray[oldPos]) {
        break;
    }
    newArray[newPos] = oldArray[oldPos];
    oldPos++;
    newPos++;
}

newArray[newPos] = add;
newPos++;

while (oldPos < oldArray.length) {
    newArray[newPos] = oldArray[oldPos];
    oldPos++;
    newPos++;
}

或者可以使用 System.arraycopy 来完成复制操作——小数组较慢,但大数组较快。

【讨论】:

    【解决方案3】:

    其实只有一个for循环可以实现你的功能。

        int[] x = {7, 6, 6, 5, 3, 2, 1 };
        //Declare an int array with length = x.length+1;
        int[] bigger = new int[x.length+1];
        int add = 4;
        /** Define a variable to indicate that if a property location is found.*/
        boolean found = false;
        /** Define a variable to store an index for insert*/
        int indexToInsert = 0;
        for (int i = 0; i < x.length; i++){
             if ( !found && add >= x[i]){
                 found = true;
                 indexToInsert = i;
                 bigger[indexToInsert] = add;
                 i--;
             }
             else{
                 if(found)
                 {
                     bigger[i+1] = x[i]; 
                 }else
                 {
                     bigger[i] = x[i];
                 }
    
             }
        }
    
        /*
         * If a property index is not found. Then put the value at last. 
         */
        if(!found)
        {
            indexToInsert = x.length;//
            bigger[indexToInsert] = add;
        }
    

    一些例子运行如下:

    初始数组是[7, 6, 6, 5, 3, 2, 1]

    加 = 4

     [7, 6, 6, 5, 4, 3, 2, 1]
    

    添加 = -1

     [7, 6, 6, 5, 3, 2, 1, -1]
    

    加 = 100

     [100, 7, 6, 6, 5, 3, 2, 1]
    

    【讨论】:

      猜你喜欢
      • 2013-10-27
      • 2020-01-28
      • 1970-01-01
      • 2016-08-08
      • 1970-01-01
      • 2011-09-25
      • 2012-07-13
      • 1970-01-01
      相关资源
      最近更新 更多