Suppose A={1,2,3,4}, p={36,3,97,19}, sort A using p as sort keys. You can get {2,4,1,3}.
It is an example in the book introducton to algorithms. It says it can be done in nlogn.
Can anyone give me some idea about how it can be done? My thought is you need to keep track of each element in p to find where it ends up, like p[1] ends up at p[3] then A[1] ends up at A[3]. Can anyone use merge sort or other nlogn sorting to get this done?
I’m new to algorithm and find it a little intimidating 🙁 thanks for any help.
I don’t see this difficult, since complexity of a sorting algorithm is usually measured on number of comparisons required you just need to update the position of elements in array
Aaccording to the elements inB. You won’t need to do any comparison in addition to ones already needed to sortBso complexity is the same.Every time you move an element, just move it in both arrays and you are done.