Hey guys,
I've made a function that returns the index positions of the highest to lowest values within a array.
Essentially rather than sorting the array, it gives the positions of the sorted elements
e.g array {4,12,434,6,24)
it will return {2,4,1,3,0}, meaning the highest number in in position 2 then next highest is in pos 4 etc.
I couldn't find anything that could do this in the <algorithm> library so i made my own.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
int * order_high(const int * input_array, unsigned int size){
int * temp, *output_array;
bool * value_used;
unsigned int j;
value_used = new bool[size];
//initialize bool array
for(unsigned int i = 0; i < size; ++i)
value_used[i] = false;
temp = new int[size];
output_array = new int[size];
for(unsigned int i =0; i < size; ++i) // copy array to temp
temp[i] = input_array[i];
sort(temp,temp+size);
for(unsigned int i = size; i != 0; --i){ // go down the sorted array from highest (sorted) value to lowest (sorted) value
for(j = size-1; temp[i-1] != input_array[j] || value_used[j] == true; --j) // find matching position in the input array & make sure we havent already included it
;
value_used[j] = true;
output_array[(size-1) - (i-1)] = j; // put that position into the output array
}
return output_array;
}
|
My question is there a better way of doing this? My way is very hackish, and seems needlessly complicated for what im doing.