Anonymous user
Sorting algorithms/Permutation sort: Difference between revisions
Sorting algorithms/Permutation sort (view source)
Revision as of 20:11, 23 November 2014
, 9 years ago→{{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@
call permsets
call permSort
call show@ ' after sort'
say; say 'Permutation sort took' ? "permutations to find the sorted list."
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
gen@: @.
@.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'
do
return
/*──────────────────────────────────INORDER subroutine──────────────────*/
Line 1,225:
_=x
end /*j*/
do k=1 for
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
call .permAdd
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*/;
/*──────────────────────────────────PERMSORT subroutine─────────────────*/
permSort: do ?=1 until inOrder(
end /*?*/
return
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
show@:
say copies('■', 70) /*show a nice separator line. */
return</lang>
'''output''' using the default input:
<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
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
</pre>
|