How to obtain the index permutation after the sorting

前端 未结 7 1453
野性不改
野性不改 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:03

    Yes, there is one with O(NlogN) time.Since the sorting takes O(NlogN) time anyway, this will not affect overall time complexity.
    Time Complexity: O(NlogN)
    Space Complexity:O(N)
    where N=number of elements in input array

    Algorithm:

    1. Make copy of input array(P) call it Q.
    2. Sort input array P.
    3. For each number in Q, do binary search to find index of that element in P.

提交回复
热议问题