Sorting: Getting permutation index array
Instead of actually sorting an array, how would you go about obtaining the permutation index array, mapping the original array to the sorted? E.g. if the array to be sorted is
{ 12, 8, 3, 17, 10 }
the desired permutation index would be
{ 2, 1, 4, 0, 3 }
The solution I have seen (http://www.freshsources.com/199300F2.HTM#00F2_007D) declares the array to be sorted globally at the top, creates a compare function that compares elements in this array and hands it to qsort (with an index array). This works in the hypothetical case that you know the array you want to sort before you write your code, but what is the workaround when the array is found at run time?