Sorting algorithms/Permutation sort: Difference between revisions

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


Permuation sort took 21 "sorts".
Permutation sort took 21 permutations to find the sorted list.
</pre>
</pre>