我正在使用java。我有一个浮点数的数组,我也有一个整数数组的数组。
floats [i]对应于intarrays [i]。
我已经设置了一个插入排序来对浮动进行排序,它正确地完成了。但是,我正在努力弄清楚如何以与花车完全相同的方式对intarrays进行排序。
这是我的浮动排序代码。
for(int i = 1; i < floats.length; i++){
float key = floats[i];
int j;
for(j = i - 1; (j >= 0) && (key < floats[j]); j--){
floats[j + 1] = floats[j];
}
floats[j + 1] = key;
}
基本上,我应该如何将第二个数组集成到这个中,以便以完全相同的方式进行排序?
注意:我不能使用ArrayLists。
答案 0 :(得分:0)
使用您的索引跟踪器(i
和j
)来操纵intarrays
与floats
旁边的位置。
给定float[] floats
和int[][] intarrays
。
for(int i = 1; i < floats.length; i++){
float key = floats[i];
int[] intKey = intarrays[i]; // New code, identical to above line
int j;
for(j = i - 1; (j >= 0) && (key < floats[j]); j--){
floats[j + 1] = floats[j];
intarrays[j + 1] = intarrays[j]; // New code, identical to above line
}
floats[j + 1] = key;
intarrays[j + 1] = intKey; // New code, identical to above line
}