使用Java中的索引对整数数组和存储结果进行排序

时间:2012-03-30 07:53:32

标签: java

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

假设数组中的元素是

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

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

5 个答案:

答案 0 :(得分:3)

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

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));

答案 1 :(得分:0)

你可以创建一个数组

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

使用任何排序算法,当你在数组x中移动两个元素时,在数组y中执行相同的操作

答案 2 :(得分:0)

可能的解决方案

 //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]);

答案 3 :(得分:0)

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

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

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

答案 4 :(得分:0)

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()
相关问题