简体   繁体   English

插入排序与外部数组?

[英]Insertion sort with external array?

I'm using java. 我正在使用Java。 I have an array of floating point numbers, and I also have an array of integer arrays. 我有一个浮点数数组,也有一个整数数组数组。

floats[i] corresponds to intarrays[i]. floats [i]对应于intarrays [i]。

I've set up a insertion sort to sort the floats, and it does so correctly. 我已经设置了一个插入排序来对浮点数进行排序,并且这样做正确。 However, I'm struggling to figure out how to sort the intarrays in the exact same fashion that the floats were. 但是,我正在努力找出如何以与浮点数完全相同的方式对整数数组进行排序。

Here is my code for sorting the floats. 这是我对浮点数进行排序的代码。

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

Essentially, how should I integrate the second array into this so it sorts in the exact same way? 本质上,我应该如何将第二个数组集成到该数组中,以便以完全相同的方式排序?

NOTE: I can't use ArrayLists. 注意:我不能使用ArrayLists。

Use your index trackers ( i and j ) to manipulate the positions of intarrays alongside floats . 用你的指数跟踪器( ij )操纵的位置intarrays一起floats

Given float[] floats and int[][] intarrays . 给定float[] floatsint[][] 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM