I need a float[]
to be sorted. And I need to know where the old indices are in new array. That's why I can't use Array.Sort();
or whatever. So I would like to write a function that sorts the array for me and remembers from what index it took each value:
float[] input = new float[] {1.5, 2, 0, 0.4, -1, 96, -56, 8, -45};
// sort
float[] output; // {-56, -45, -1, 0, 0.4, 1.5, 2, 8, 96};
int[] indices; // {6, 8, 4, 2, 3, 0, 1, 7, 5};
Size of arrays would be around 500. How should I approach this ? What sorting algorithm etc.
Array.Sort()
is very fast I'll take it.
float[] input = new float[] { 1.5F, 2, 0, 0.4F, -1, 96, -56, 8, -45 };
int[] indices = new int[input.Length];
for (int i = 0; i < indices.Length; i++) indices[i] = i;
Array.Sort(input, indices);
// input and indices are now at the desired exit state
Basically, the 2-argument version of Array.Sort
applies the same operations to both arrays, running the actual sort comparisons on the first array. This is normally used the other way around - to rearrange something by the desired indices; but this works too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With