How to obtain the index permutation after the sorting

前端 未结 7 1452
野性不改
野性不改 2020-12-13 06:24

Given an array arr = {5, 16, 4, 7}, we can sort it through sort(arr, arr+sizeof(arr)/sizeof(arr[0])). so now the array arr = {4, 5, 7, 16}

7条回答
  •  星月不相逢
    2020-12-13 07:16

    Why not put some satellite data? Instead of sorting the numbers, just sort pairs of numbers and their indices. Since the sorting is first done on the first element of the pair, this shouldn't disrupt a stable sorting algorithm.

    For unstable algorithms, this will change it to a stable one.

    But note that if you try sorting this way it generates the index while sorting, not after.

    Also, since knowing the permutation index would lead to a O(n) sorting algorithm, so you cannot do it faster than O(nlogn).

提交回复
热议问题