Sorting algorithms/Permutation sort: Difference between revisions

m
→‎{{header|REXX}}: added whitespace and comments, remove an unneeded statement.
(solved for maxscript)
m (→‎{{header|REXX}}: added whitespace and comments, remove an unneeded statement.)
Line 1,201:
<lang rexx>/*REXX program sorts an array using the permutation-sort method. */
call gen@ /*generate the array elements. */
call show@ 'before sort' 'before sort' /*show the before array elements.*/
call permsets itemsL /*generate items! permutations.*/
call permSort itemsL /*invoke the permutation sort. */
call show@ ' after sort' /*show the after array elements.*/
say; say 'Permutation sort took' ? "permutations to find the sorted list."
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
gen@: @.= = /*assign default value. */
@.1 = '---Four_horsemen_of_the_Apocalypse---'
@.2 = '====================================='
@.3 = 'Famine───black_horse'
@.4 = 'Death───pale_horse'
@.5 = 'Pestilence_[Slaughter]───red_horse'
@.6 = 'Conquest_[War]───white_horse'
list= /*[↓] find # of entries in array.*/
do itemsL=1 while @.itemsL\==''; @@.itemsL=@.itemsL; end /*itemsL*/
itemsL=itemsL-1 /*adjust items slightly. /*adjust number of items by one. */
return
/*──────────────────────────────────INORDER subroutine──────────────────*/
Line 1,225:
_=x
end /*j*/
do k=1 for items#; _=word(#.?,k); @.k=@@._; end /*k*/ /*here it is*/
return 1 /*they're all in order finally. */
/*──────────────────────────────────PERMSETS subroutine─────────────────*/
permsets: procedure expose !. # #.; parse arg n,#.; #=0
do f=1 for n; !.f=f; end /*f*/; call .permAdd /*populate 1st perm /*f*/
call .permAdd do while .permNext(n,0); call .permAdd; end /*whilepopulate 1st ···perm*/
do while .permNext(n,0); call .permAdd; end /*while*/
return #
.permNext: procedure expose !.; parse arg n,i; nm=n-1
Line 1,240 ⟶ 1,241:
parse value !.j !.i with !.i !.j
return 1
.permAdd: #=#+1; do j=1 for N; #.#=#.# !.j; end /*j*/; return
/*──────────────────────────────────PERMSORT subroutine─────────────────*/
permSort: do ?=1 until inOrder(aList$); $= /*look for the sorted permutation*/
aList=; do m=1 for items#; _=word(#.?,m); aList $=aList$ @._; end end /*m*/
end /*?*/
return
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
show@: widthH=length(items) /*maximum width of anydo j=1 for L /* line.[↓] display elements in array*/
do j=1 for items; say ' element' right(j,widthHlength(L)) arg(1)":" @.j; end /*j*/
say copies('─', 79) end /*show a nice separator line. j*/
say copies('■', 70) /*show a nice separator line. */
return</lang>
'''output''' &nbsp; using the default input:
{{out}}
<pre>
element 1 before sort: ---Four_horsemen_of_the_Apocalypse---
element 2 before sort: =====================================
element 3 before sort: Famine───black_horse
element 4 before sort: Death───pale_horse
element 5 before sort: Pestilence_[Slaughter]───red_horse
element 6 before sort: Conquest_[War]───white_horse
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
───────────────────────────────────────────────────────────────────────────────
element 1 after sort: ---Four_horsemen_of_the_Apocalypse---
element 2 after sort: =====================================
element 3 after sort: Conquest_[War]───white_horse
element 4 after sort: Death───pale_horse
element 5 after sort: Famine───black_horse
element 6 after sort: Pestilence_[Slaughter]───red_horse
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
───────────────────────────────────────────────────────────────────────────────
 
PermuationPermutation sort took 21 "sorts"permutations to find the sorted list.
</pre>