Fast algorithm to calculate the orders of all elements in an array?

Suppose you are given an array A of n distinct elements taken from some completely ordered set. For example, you may be given

137 13 7 42 38

The goal is to create for this array of elements a corresponding array B such that B [i] is the number of elements in the original array that are less than A [i]. For example, in the above array we want to create

A = 137  13   7  42  38
B =   4   1   0   3   2

Since 137 is more than four other elements (13, 7, 42, 38), 13 is only more than one of the elements (7), 7 more than no other elements, etc.

In the most general case, when the elements in the array are arbitrary objects that can only be matched, any solution to this problem should be done in & Omega; (n lg n) in the worst case, because as soon as we have this table, we can sort the array O (n) times by creating a new array of n elements and then placing each element at the position indicated in the table. However, I do not know how fast we can build this table when the elements are not arbitrary values.

: , n integer . ? , , U.

, , - O (n lg n), , , , , , . , , .

+3
5

1: .

A  = 137  13   7  42  38
I  =   0   1   2   3   4

A' =   7  13  38  42 137
I' =   2   1   4   3   0

2: I'[i] = j B[j] = i.

I' =   2   1   4   3   0
i  =   0   1   2   3   4

B  =   4   1   0   3   2
+6

, , , , O (n log n), - . ( , B, A).

A , [1..M], - O (M + n) - ( , , L [1..M]), A, A, - L 1. M. B.

+2

"-O", N-LogN. {value, originalIndex}.

, rank[sorted[i].originalIndex]=i;

1 N-LogN.

U , O (kN) ( )

+1

, n ^ 2-n, , 1- 1- , 2- .

0

,

0

All Articles