:
- :
if (a[x] < a[x+1])
:
if (a[a_index[x]] < a[a_index[x+1]])
And instead of:
swap(a[x], a[x+1])
You will be doing:
swap(a_index[x], a_index[x+1])
(where a_index is initialized to first contain the range of indices in sequential order (0..sizeof (a))
Since essentially a_index is just a lookup table, where the sorting value is the corresponding value in a. (In practice, this is just another level of indirection compared to what we usually do when we sort, since usually we will not directly compare (x) and (x + 1) either)
A similar solution can be made without doing in-place sorting if you are doing all your comparisons with the corresponding values in a, instead of comparing
source
share