Sorting of arrays with keeping the ID deutsch

By Xjs.

Hi.

While working for the actual “Jugend-Forscht” project I had the problem of having an array of integers, at which always one ID matched one function value. Now I wanted to sort the whole by function value, but afterwards I wanted to have the ID again.

Simple Quicksort won’t work, because then the indices appear.

So I thought the following: I multiply the function value with the maximum function value possible, and add the array index (which is smaller than the maximal function value) afterwards.

Works the following:

int array[5] = {99, 71, 22, 40, 1}; // fictive data
for (int i = 0; i < 5; i++)
{
    array[i] *= FUNC_MAX;
    array[i] += i;
}
sortarray (array, 5); // function to sort the array.

Bye,

Jannis.

Leave a Reply (Some HTML allowed).