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@ |
call show@ 'before sort' /*show the before array elements.*/ |
||
call permsets |
call permsets L /*generate items! permutations.*/ |
||
call permSort |
call permSort L /*invoke the permutation sort. */ |
||
call show@ ' after sort' |
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@: @. |
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' |
||
/*[↓] find # of entries in array.*/ |
|||
do |
do L=1 while @.L\==''; @@.L=@.L; end /*L*/ |
||
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 |
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 |
do f=1 for n; !.f=f; end /*f*/ |
||
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*/; |
.permAdd: #=#+1; do j=1 for N; #.#=#.# !.j; end /*j*/; return |
||
/*──────────────────────────────────PERMSORT subroutine─────────────────*/ |
/*──────────────────────────────────PERMSORT subroutine─────────────────*/ |
||
permSort: do ?=1 until inOrder( |
permSort: do ?=1 until inOrder($); $= /*look for the sorted permutation*/ |
||
do m=1 for #; _=word(#.?,m); $=$ @._; end /*m*/ |
|||
end /*?*/ |
end /*?*/ |
||
return |
return |
||
/*──────────────────────────────────SHOW@ subroutine────────────────────*/ |
/*──────────────────────────────────SHOW@ subroutine────────────────────*/ |
||
show@: |
show@: do j=1 for L /* [↓] display elements in array*/ |
||
say ' element' right(j,length(L)) arg(1)":" @.j |
|||
end /*j*/ |
|||
say copies('■', 70) /*show a nice separator line. */ |
|||
return</lang> |
return</lang> |
||
'''output''' 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 |
||
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ |
|||
─────────────────────────────────────────────────────────────────────────────── |
|||
Permutation sort took 21 permutations to find the sorted list. |
|||
</pre> |
</pre> |
||