Anonymous user
Sorting algorithms/Permutation sort: Difference between revisions
m
→{{header|REXX}}: added/changed comments and whitespace, changed indentations, aligned more statements.
(added FreeBASIC) |
m (→{{header|REXX}}: added/changed comments and whitespace, changed indentations, aligned more statements.) |
||
Line 1,396:
=={{header|REXX}}==
<lang rexx>/*REXX program sorts and displays an array using the permutation-sort method. */
call gen
call show
say copies('
call
call
call show
say; say 'Permutation sort took' ? "permutations to find the sorted list."
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
.pAdd: #=#+1; do j=1 for N; #.#=#.# !.j; end; return
show
@.2= '====================================='▼
@.3= 'Famine───black_horse'▼
/* [↓] also assign to another array. */▼
do L=1 while @.L\==''; @@.L=@.L; end /* [↓] find number of entries in array*/▼
L=L-1 /*adjust the number of items by one. */▼
return▼
/*──────────────────────────────────────────────────────────────────────────────────────*/
▲ @.2 = '====================================='
_=word(q,1); do j=2 to words(q); x=word(q,j)▼
ele=right('element',
▲
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
.
do k=nm by -1 for nm; kp=k+1
if !.k<!.kp then do; i=k; leave; end
end /*k*/ /* [↓] swap two array elements*/
do j=i+1 while j<n; parse value !.j !.n with !.n !.j; n=n-1; end
if i==0 then return 0;
parse value !.j !.i with !.i !.j
return 1
/*──────────────────────────────────────────────────────────────────────────────────────*/
call .pAdd; do while .pNext(n,0); call
▲ return #
▲ call .permAdd; do while .permNext(n,0); call .permAdd; end /*while*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
do m=1 for #; _=word(#.? ,m); $=$ @._; end /*m*/
end /*?*/
▲show@: do j=1 for L; say ' element' right(j,length(L)) arg(1)":" @.j
'''output''' using the default (internal) inputs:
<pre>
element 1 before sort: ---
element 2 before sort: =====================================
element 3 before sort:
element 4 before sort:
element 5 before sort:
element 6 before sort:
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
element 1 after sort: ---
element 2 after sort: =====================================
element 3 after sort:
element 4 after sort:
element 5 after sort:
element 6 after sort:
Permutation sort took 21 permutations to find the sorted list.
|