Sorting algorithms/Permutation sort: Difference between revisions

Content added Content deleted
(added FreeBASIC)
m (→‎{{header|REXX}}: added/changed comments and whitespace, changed indentations, aligned more statements.)
Line 1,396: Line 1,396:
=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program sorts and displays an array using the permutation-sort method. */
<lang rexx>/*REXX program sorts and displays an array using the permutation-sort method. */
call gen@ /*generate the array elements. */
call gen /*generate the array elements. */
call show@ 'before sort' /*show the before array elements. */
call show 'before sort' /*show the before array elements. */
say copies('', 70) /*show separator line between displays.*/
say copies('', 75) /*show separator line between displays.*/
call permsets L /*generate items (!) permutations. */
call psets L /*generate items (!) permutations. */
call permSort L /*invoke the permutation sort. */
call pSort L /*invoke the permutation sort. */
call show@ ' after sort' /*show the after array elements. */
call show ' after sort' /*show the after array elements. */
say; say 'Permutation sort took' ? "permutations to find the sorted list."
say; say 'Permutation sort took' ? "permutations to find the sorted list."
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
.pAdd: #=#+1; do j=1 for N; #.#=#.# !.j; end; return
gen@: @.=; @.1= '---Four_horsemen_of_the_Apocalypse---'
show: do j=1 for L; say ele right(j,wL) arg(1)":" translate(@.j,,'_'); end; return
@.2= '====================================='
@.3= 'Famine───black_horse'
@.4= 'Death───pale_horse'
@.5= 'Pestilence_[Slaughter]───red_horse'
@.6= 'Conquest_[War]───white_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
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
inOrder: parse arg q /*see if Q list is in order.*/
gen: @.=; @.1 = '---Four_horsemen_of_the_Apocalypse---'
@.2 = '====================================='
_=word(q,1); do j=2 to words(q); x=word(q,j)
if x<_ then return 0 /*Out of order? Not sorted. */
@.3 = 'Famine───black_horse'
_=x
@.4 = 'Death───pale_horse'
end /*j*/
@.5 = 'Pestilence_[Slaughter]───red_horse'
do k=1 for #; _=word(#.?,k); @.k=@@._; end /*k*/
@.6 = 'Conquest_[War]───white_horse'
return 1 /*they're all in order finally*/
ele=right('element', 15) /*literal used for the display.*/
do L=1 while @.L\==''; @@.L=@.L; end; L=L-1; wL=length(L); return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
.permAdd: #=#+1; do j=1 for N; #.#=#.# !.j; end /*j*/; return
isOK: parse arg q /*see if Q list is in order. */
_=word(q, 1); do j=2 to words(q); x=word(q, j); if x<_ then return 0
_=x /*Out of order? [↑] ¬ sorted*/
end /*j*/
do k=1 for #; _=word(#.?, k); @.k=@@._; end /*k*/
return 1 /*they're all in order finally.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
.permNext: procedure expose !.; parse arg n,i; nm=n-1
.pNext: procedure expose !.; parse arg n,i; nm=n-1
do k=nm by -1 for nm; kp=k+1
do k=nm by -1 for nm; kp=k+1
if !.k<!.kp then do; i=k; leave; end
if !.k<!.kp then do; i=k; leave; end
end /*k*/
end /*k*/ /* [↓] swap two array elements*/
do j=i+1 while j<n; parse value !.j !.n with !.n !.j; n=n-1; end
do j=i+1 while j<n; parse value !.j !.n with !.n !.j; n=n-1; end
if i==0 then return 0; do j=i+1 while !.j<!.i; end /*j*/
if i==0 then return 0; do j=i+1 while !.j<!.i; end /*j*/
parse value !.j !.i with !.i !.j
parse value !.j !.i with !.i !.j
return 1
return 1
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
permsets: procedure expose !. # #.; parse arg n,#.; #=0
psets: procedure expose !. # #.; parse arg n,#.; #=0; do f=1 for n; !.f=f; end /*f*/
do f=1 for n; !.f=f; end /*f*/
call .pAdd; do while .pNext(n,0); call .pAdd; end /*while*/
return #
call .permAdd; do while .permNext(n,0); call .permAdd; end /*while*/
return #
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
permSort: do ?=1 until inOrder($); $= /*find sorted permutation.*/
pSort: do ?=1 until isOK($); $= /*find sorted permutation.*/
do m=1 for #; _=word(#.? ,m); $=$ @._; end /*m*/
do m=1 for #; _=word(#.? ,m); $=$ @._; end /*m*/
end /*?*/
end /*?*/
return
return</lang>
/*──────────────────────────────────────────────────────────────────────────────────────*/
show@: do j=1 for L; say ' element' right(j,length(L)) arg(1)":" @.j
end /*j*/ /* [↑] display array elements*/
return</lang>
'''output''' &nbsp; using the default (internal) inputs:
'''output''' &nbsp; using the default (internal) inputs:
<pre>
<pre>
element 1 before sort: ---Four_horsemen_of_the_Apocalypse---
element 1 before sort: ---Four horsemen of the Apocalypse---
element 2 before sort: =====================================
element 2 before sort: =====================================
element 3 before sort: Famine───black_horse
element 3 before sort: Famine───black horse
element 4 before sort: Death───pale_horse
element 4 before sort: Death───pale horse
element 5 before sort: Pestilence_[Slaughter]───red_horse
element 5 before sort: Pestilence [Slaughter]───red horse
element 6 before sort: Conquest_[War]───white_horse
element 6 before sort: Conquest [War]───white horse
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
element 1 after sort: ---Four_horsemen_of_the_Apocalypse---
element 1 after sort: ---Four horsemen of the Apocalypse---
element 2 after sort: =====================================
element 2 after sort: =====================================
element 3 after sort: Conquest_[War]───white_horse
element 3 after sort: Conquest [War]───white horse
element 4 after sort: Death───pale_horse
element 4 after sort: Death───pale horse
element 5 after sort: Famine───black_horse
element 5 after sort: Famine───black horse
element 6 after sort: Pestilence_[Slaughter]───red_horse
element 6 after sort: Pestilence [Slaughter]───red horse


Permutation sort took 21 permutations to find the sorted list.
Permutation sort took 21 permutations to find the sorted list.