如何数组排序,并保持在java中的指数跟踪 [英] How to sort an array and keep track of the index in java
问题描述
我想排序(递减)的整数数组,但跟踪原来的指数。
我的意思是,例如,如果我有这样的数组:
B [] = {4,5,3,5,2}
使用Arrays.sort后(B,Collections.reverseOrder()),它变成
(我用Arrays.sort,因为在这个实例B只有长度为5,但在我的问题b的长度可以是1<&b.length个LT; 70
B [] = {5,5,4,3,2}
但我想以某种方式有原来的指数,我的意思是知道
bOrignalIndex [] = {1,3,0,2,4}
我不知道,如果我在清晰的问题,请向我的一切。
我有这一块在C ++中code,可以是有帮助的,因为它不会是我想要
N = 4
M = 5
托德[] =
[0] 0
[1] 1
[2] 2
[3] 3
TS [] =
[0] 4
[1] 5
[2] 3
[3] 5 托德[MAXT],TS [MAXT]
布尔ORD(INT A,INT B){
返回TS [一个]≥ TS并[b] }
诠释主要(无效){
对于(INT M,N; scanf函数(%D,&安培;男,&安培; N)){
布尔可能= TRUE;
FOR(I = 0; I< M,I ++){//为每个团队
scanf函数(%D,TS + I); //读取团队规模
托德[我] =我;
}
排序(托德,托德+ M,ORD)
事情是这样做后,托德已经通过索引排序的数组,即:
托德[] =
[0] 1
[1] 3
[2] 0
[3] 2
尝试排序对(价值指数)按值比较
:
公共类对实现可比<对GT&; {
公众最终诠释指数;
公众最终int值; 公众对(INT指数,int值){
this.index =指数;
THIS.VALUE =价值;
} @覆盖
公众诠释的compareTo(对等){
//乘以-1作为作者需要降序排序
返回-1 * Integer.valueOf(THIS.VALUE).compareTo(other.value);
}
}
然后,当你要进行排序:
公共静态无效的主要(字串[] args){
对[] = yourArray一双新[10]; //填充数组
yourArray [0] =一双新的(0,5); yourArray [1] =新的对(1,10); //等等
Arrays.sort(yourArray);
}
现在,你有对
按值
降序排序对象的数组。每个对象还包含首页
- 原始数组的地方
P上。 S.我写的样本Java作为问题有的Java
标记。虽然,在 C ++
的想法是一样的,唯一的实现是一个有点不同。
I am trying to sort (decreasing) an array of integers but keeping track of the original index.
I mean, for example if I have this array:
b[] = { 4, 5, 3, 5, 2 }
after using Arrays.sort(b, Collections.reverseOrder()) it turns into ( I am using Arrays.sort, because in this example b is only length 5, but in my problem the length of b could be 1 < b.length < 70
b[] = { 5, 5, 4, 3, 2 }
but I want to somehow have the original index, I mean knowing that
bOrignalIndex[] = { 1, 3, 0, 2, 4 }
I don't know if my question in clear, please ask me everything. I have this piece of code in C++ that can be helpful because it does what I want
n=4
m=5
tord[] =
[0] 0
[1] 1
[2] 2
[3] 3
ts[] =
[0] 4
[1] 5
[2] 3
[3] 5
tord[MAXT], ts[MAXT];
bool ord(int a, int b){
return ts[a] > ts[b]; }
int main(void){
for(int m, n; scanf("%d %d", &m, &n)){
bool possible = true;
FOR(i=0;i<m, i++){ // for each team
scanf("%d", ts + i); // read team size
tord[i] = i;
}
sort(tord, tord + m, ord)
The thing is after doing this, tord has the array ordered by index, that is:
tord[] =
[0] 1
[1] 3
[2] 0
[3] 2
Try sorting pairs of (value, index)
compared by value:
public class Pair implements Comparable<Pair> {
public final int index;
public final int value;
public Pair(int index, int value) {
this.index = index;
this.value = value;
}
@Override
public int compareTo(Pair other) {
//multiplied to -1 as the author need descending sort order
return -1 * Integer.valueOf(this.value).compareTo(other.value);
}
}
Then, when you're going to sort:
public static void main(String[] args) {
Pair[] yourArray = new Pair[10];
//fill the array
yourArray[0] = new Pair(0, 5); yourArray[1] = new Pair(1, 10); //and so on
Arrays.sort(yourArray);
}
Now, you have an array of Pair
object ordered by value
descending. Each object also contains index
- the place in the original array.
P. S. I wrote the sample in Java as the question has java
tag. Although, in C++
the idea is the same, only the implementation is a little bit different.
这篇关于如何数组排序,并保持在java中的指数跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!