【问题标题】:Sort an Integer Array and Store result with its index in Java对整数数组进行排序并使用 Java 中的索引存储结果
【发布时间】:2023-04-04 00:53:02
【问题描述】:

大家好,我需要对整数数组中的一些整数元素进行排序,并且需要存储排序列表的索引

假设数组中的元素是

x[]= {10,20,40,70,80,50,30};

我需要得到排序顺序的索引,比如在这种情况下我需要得到4,3,5,2,6,0(升序)(从0开始的数组x)

【问题讨论】:

标签: java


【解决方案1】:

一种简单的方法(在算法上并不聪明)是从包含值和索引的现有列表中创建一个新的对象列表(或数组):

class ValueAndIndex implements Comparable<ValueAndIndex> {
    final int value;
    final int index;

    ValueAndIndex(int value, int index) {
        this.value = value;
        this.index = index;
    }

    @Override public int compareTo(ValueAndIndex other) {
       // compare on value;
        if (this.value < other.value) {
            return -1;
        } else if (this.value > other.value) {
            return 1;
        } else {
            return 0;
        }
    }
}

现在,在列表中创建此类的实例:

List<ValueAndIndex> secondaryList = new ArrayList<ValueAndIndex>(x.length);
for (int i = 0; i < x.length; ++i) {
    secondaryList.add(new ValueAndIndex(x[i], i));
}

排序这个列表:

Collections.sort(secondaryList);

现在,索引仍在此列表中:

int [] indexesInSortedOrder = new int[x.length];
for (int i = 0; i < secondaryList.size(); ++i) {
    indexesInSortedOrder[i] = secondaryList.get(i).index;
}

System.out.println(Arrays.toString(indexesInSortedOrder));

【讨论】:

    【解决方案2】:

    可能的解决方案

     //sort the array intio a new array
     y[] = x;
     Arrays.sort(y); //sort ascending
    
     //final array of indexes
     int index_array[] = new int[7];
    
     //iteretate on x arrat
     for(int i=0; i<7; i++)
        //search the position of a value of the original x array into the sorted y array, store the position in the index array
        index_array[i] = arrays.binarySearch(x,y[i]);
    

    【讨论】:

    • 这不应该是一个可接受的解决方案,它会因为重复值而失败,为什么仅限于长度为 7 的示例数组?
    • 此解决方案不起作用。 1) 由于 x 和 y 都指向同一个内存位置,调用 Arrays.sorting 后两者都会指向排序后的数组。 2) 假设数组 x 未排序。但是你仍然不能对未排序的数组进行二分搜索。
    【解决方案3】:

    你可以创建一个数组

    y[] = {0,1,2,3,4,5,6};
    

    对于任何排序算法,当您切换移动数组x 中的两个元素时,在数组y 中执行相同操作

    【讨论】:

      【解决方案4】:

      您需要的一种方法(我理解):

      1. 确定原始数组的大小n
      2. 创建结果数组R 并使用0 . . n-1 初始化其元素
      3. 最后实现一种排序算法,它对原始数组的副本(!)进行排序,同时切换R 中的元素

      示例运行:

          Copied  Result
          Array 
      ------------------
      1.  2-3-1   0-1-2
      2.  2-1-3   0-2-1
      3.  1-2-3   2-0-1
      

      【讨论】:

        【解决方案5】:
        public Map sortDecendingDFSGlobal() {
            Map<String, Object> multiValues = new HashMap<String, Object>();
        
            double[] dfs = this.global_dfs;
            int[] index = new int[dfs.length];
        
            for (int i = 0; i < dfs.length; i++) {
                index[i] = i;//for required indexing
            }
            for (int i = 0; i < dfs.length; i++) {
                //sorting dfsglobal in decending order
                double temp = dfs[i];
                double swap = dfs[i];
                int swapIndex = i;
        
                //keeping track of changing indexing during sorting of dfsglobal
                int indStart = index[i];
                int indSwap = index[i];
                int number = i;
                for (int j = i; j < dfs.length; j++) {
                    if (temp < dfs[j]) {
                        temp = dfs[j];
                        swapIndex = j;
        
                        indSwap = index[j];
                        number = j;
                    }
                }
                dfs[i] = temp;
                dfs[swapIndex] = swap;
        
                index[i] = indSwap;
                index[number] = indStart;
            }
            //again sorting the index matrix for exact indexing
            for (int i = 0; i < index.length - 1; i++) {
               for(int j = i; j < index.length - 1; j++ )
               {               
        
                   if(dfs[j] == dfs[j + 1] && index[j] > index[j + 1])
                   {
                       int temp = index[j];
                       index[j] = index[j+1];
                       index[j + 1] = temp;
                   }
               }
            }
        
            this.sortedDFS = dfs;
            this.arrIndex = index;
        
            multiValues.put("sorted", dfs);
            multiValues.put("index", index);
            return multiValues;
        } //SortedDecendingDFSGlobal()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-03-17
          • 2013-02-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多