How to find the permutation of a sort in Java

后端 未结 5 1046
攒了一身酷
攒了一身酷 2021-01-11 22:41

I want to sort an array and find the index of each element in the sorted order. So for instance if I run this on the array:

[3,2,4]

I\'d ge

5条回答
  •  臣服心动
    2021-01-11 23:23

    One way to achieve this is to make a list of pairs with the starting index as the second part of the pair. Sort the list of pairs lexicographically, then read off the starting positions from the sorted array.

    Starting array:

    [3,2,4]
    

    Add pairs with starting indexes:

    [(3,0), (2,1), (4,2)]
    

    Sort it lexicographically

    [(2,1), (3,0), (4,2)]
    

    then read off the second part of each pair

    [1,0,2]
    

提交回复
热议问题