An
array is an ordered list of things (or
elements). Each element is at a specific position (or
index into the array).
When you sort an array, you explicitly
reorder the elements in the array -- that is, each element gets a new index number.
What you are asking to do is keep track of the original indices associated with each element, as well as that element.
There are two basic ways you do this: recover it or keep it.
If you can assume that the elements are unique, then you can recover the original index by simply searching for it in the original array.
The other method is to encode the original index as part of the element. Meaning that the element is no longer just a single number -- it is
two numbers -- and the sorting criteria must now take that pair of numbers and ignore one of them.
1 2 3 4 5 6 7
|
struct person
{
int original_index;
int chickens_eaten;
};
person people[ 10 ];
|
When you fill the array, make sure that you put the values 0..N in the original_index.
When you sort the array, compare only
people[j].chickens_eaten
Hope is helps.