Permutations/Rank of a permutation: Difference between revisions
Content added Content deleted
(Add Perl) |
No edit summary |
||
Line 1,525: | Line 1,525: | ||
12 items, permute 422 is 0 1 2 3 4 5 9 8 10 7 6 11 |
12 items, permute 422 is 0 1 2 3 4 5 9 8 10 7 6 11 |
||
12 items, permute 1212 is 0 1 2 3 4 6 10 5 9 7 8 11 |
12 items, permute 1212 is 0 1 2 3 4 6 10 5 9 7 8 11 |
||
</pre> |
|||
=={{header|Ring}}== |
|||
<lang ring> |
|||
# Project : Permutations/Rank of a permutation |
|||
# Date : 2017/10/21 |
|||
# Author : Gal Zsolt (~ CalmoSoft ~) |
|||
# Email : <calmosoft@gmail.com> |
|||
list = [0, 1, 2, 3] |
|||
for perm = 0 to 23 |
|||
str = "" |
|||
for i = 1 to len(list) |
|||
str = str + list[i] + ", " |
|||
next |
|||
see nl |
|||
str = left(str, len(str)-2) |
|||
see "(" + str + ") -> " + perm |
|||
nextPermutation(list) |
|||
next |
|||
func nextPermutation a |
|||
elementcount = len(a) |
|||
if elementcount < 1 then return ok |
|||
pos = elementcount-1 |
|||
while a[pos] >= a[pos+1] |
|||
pos -= 1 |
|||
if pos <= 0 permutationReverse(a, 1, elementcount) |
|||
return ok |
|||
end |
|||
last = elementcount |
|||
while a[last] <= a[pos] |
|||
last -= 1 |
|||
end |
|||
temp = a[pos] |
|||
a[pos] = a[last] |
|||
a[last] = temp |
|||
permutationReverse(a, pos+1, elementcount) |
|||
func permutationReverse a, first, last |
|||
while first < last |
|||
temp = a[first] |
|||
a[first] = a[last] |
|||
a[last] = temp |
|||
first += 1 |
|||
last -= 1 |
|||
end |
|||
</lang> |
|||
Output: |
|||
<pre> |
|||
(0, 1, 2, 3) -> 0 |
|||
(0, 1, 3, 2) -> 1 |
|||
(0, 2, 1, 3) -> 2 |
|||
(0, 2, 3, 1) -> 3 |
|||
(0, 3, 1, 2) -> 4 |
|||
(0, 3, 2, 1) -> 5 |
|||
(1, 0, 2, 3) -> 6 |
|||
(1, 0, 3, 2) -> 7 |
|||
(1, 2, 0, 3) -> 8 |
|||
(1, 2, 3, 0) -> 9 |
|||
(1, 3, 0, 2) -> 10 |
|||
(1, 3, 2, 0) -> 11 |
|||
(2, 0, 1, 3) -> 12 |
|||
(2, 0, 3, 1) -> 13 |
|||
(2, 1, 0, 3) -> 14 |
|||
(2, 1, 3, 0) -> 15 |
|||
(2, 3, 0, 1) -> 16 |
|||
(2, 3, 1, 0) -> 17 |
|||
(3, 0, 1, 2) -> 18 |
|||
(3, 0, 2, 1) -> 19 |
|||
(3, 1, 0, 2) -> 20 |
|||
(3, 1, 2, 0) -> 21 |
|||
(3, 2, 0, 1) -> 22 |
|||
(3, 2, 1, 0) -> 23 |
|||
</pre> |
</pre> |
||