What shuffling algorithms exist besides Fisher-Yates and finding the “next permutation”?

In particular, in the field of one-dimensional sets of elements of the same type, such as a vector of integers.

Say, for example, you had a 32,768 vector containing sorted integers from 0 to 32,767.

What I mean by “next permutation” performs the following permutation in the lexical ordering system.

Wikipedia lists two , and I wonder if there are any more (besides something god: P)

+3
source share
5 answers

O (N) implementation This is based on Zn Eyal Schneider mapping! → P (n)

def get_permutation(k, lst):
    N = len(lst)
    while N:
        next_item = k/f(N-1)
        lst[N-1], lst[next_item] = lst[next_item], lst[N-1]
        k = k - next_item*f(N-1)
        N = N-1
    return lst

O (N ^ 2), . , -, . ( ), , Fisher-Yates, . , (N! - k) k, , k [0, N!], N! - k.

"" . , . .

0 N! . , ( ), n- . , .

. S = {a b c d}, S . p - , (b a c d), p S, b a, a c, c d d b. q - , (d b c a), pq , q, p, (d a b)(c). , q d b p b a, pq d a. , pq , b d c. 1 , .

.

  • . (a b)(c d) (c d)(a b)
  • . (a b c) = (b c a) = (c a b)

, , , . , , ( , ). , . , , , , p > q q > p, p = q.

O (N! logN! + N!) . (EDIT: , , , ), n'th. , .

+6

, aaronasterling . N! , , , .

, . , < 0,1,0 > , # 0 [0,1,2], №1 [1,2], # 0 [1]. < 0,2,1 > . < 0,0,... 0 > , < N-1, N-2,... 0 > . " ".

, N O (N ^ 2) , , .

Kth- {0,1,2..., N-1} :

getPermutation(k, N) {
    while(N > 0) {
        nextItem = floor(k / (N-1)!)
        output nextItem
        k = k - nextItem * (N-1)!
        N = N - 1
    }
}

O (N ^ 2) (- ) O (N! log N) .

- -

getPermutation (4,3) < 2,0,0 > . < C, A, B > , ​​ 4 {A, B, C}:

ABC
ACB
BAC
BCA
CAB
CBA
+4

LFSR PRNG , , .

+2

, , .

, , . n/(n+m), n - m .

: .

+2

. 2 , . O (n lg n) .

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

0
source

Source: https://habr.com/ru/post/1762164/


All Articles