Recursively generate all arrangements for n elements

xiaoxiao2021-03-05  26

Usually we want to check all arrangements of n different elements to determine a best arrangement. For example, the arrangements of A, B, and C are: ABC, ACB, BAC, BCA, CAB, and CBA. N elements are arranged in N!

/ / Generate all arrangements VOID Perm (CHAR LIST [], INT K, INT M) {INT i; if (k == m) {for (i = 0; i <= m; i ) {Printf ("% C", list [i]); printf ("/ n");}}}} else {for (i = k; i <= m; i ) {swap (list k, list I); Perm (List, K 1, M); SWAP (List K, List I);}}}

Ref: << Data Structure, Algorithm & Applications >> P7 ~ P8

转载请注明原文地址:https://www.9cbs.com/read-32249.html

New Post(0)