Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,4 +1,7 @@
{{Sorting Algorithm}}Permutation sort, which proceeds by generating the possible permutations of the input array/list until discovering the sorted one.
{{Sorting Algorithm}}
{{omit from|GUISS}}
Permutation sort, which proceeds by generating the possible permutations
of the input array/list until discovering the sorted one.
Pseudocode:
'''while not''' InOrder(list) '''do'''

View file

@ -1,11 +1,9 @@
import std.stdio, std.algorithm, permutations2;
void permutationSort(T)(T[] items) pure /*nothrow*/ {
void permutationSort(T)(T[] items) pure nothrow @safe @nogc {
foreach (const perm; items.permutations!false)
if (perm.isSorted) {
items[] = perm[];
if (perm.isSorted)
break;
}
}
void main() {

View file

@ -1,6 +1,6 @@
import std.stdio, std.algorithm;
void permutationSort(T)(T[] items) pure nothrow {
void permutationSort(T)(T[] items) pure nothrow @safe @nogc {
while (items.nextPermutation) {}
}

View file

@ -0,0 +1,29 @@
fn inOrder arr =
(
if arr.count < 2 then return true
else
(
local i = 1
while i < arr.count do
(
if arr[i+1] < arr[i] do return false
i += 1
)
return true
)
)
fn permutations arr =
(
if arr.count <= 1 then return arr
else
(
for i = 1 to arr.count do
(
local rest = for r in 1 to arr.count where r != i collect arr[r]
local permRest = permutations rest
local new = join #(arr[i]) permRest
if inOrder new do return new
)
)
)

View file

@ -0,0 +1,4 @@
a = for i in 1 to 9 collect random 1 20
#(10, 20, 17, 15, 17, 15, 3, 11, 15)
permutations a
#(3, 10, 11, 15, 15, 15, 17, 17, 20)

View file

@ -1,22 +1,22 @@
/*REXX program sorts an array using the permutation-sort method. */
call gen@ /*generate the array elements. */
call show@ 'before sort' /*show the before array elements.*/
call permsets items /*generate items! permutations.*/
call permSort items /*invoke the permutation sort. */
call show@ ' after sort' /*show after array elements*/
call show@ 'before sort' /*show the before array elements.*/
call permsets L /*generate items! permutations.*/
call permSort L /*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 = 'Famineblack_horse'
@.4 = 'Deathpale_horse'
@.5 = 'Pestilence_[Slaughter]red_horse'
@.6 = 'Conquest_[War]white_horse'
list= /*[↓] find # of entries in array.*/
do items=1 while @.items\==''; @@.items=@.items; end /*items*/
items=items-1 /*adjust items slightly. */
gen@: @. = /*assign default value. */
@.1 = '---Four_horsemen_of_the_Apocalypse---'
@.2 = '====================================='
@.3 = 'Famineblack_horse'
@.4 = 'Deathpale_horse'
@.5 = 'Pestilence_[Slaughter]red_horse'
@.6 = 'Conquest_[War]white_horse'
/*[↓] find # of entries in array.*/
do L=1 while @.L\==''; @@.L=@.L; end /*L*/
L=L-1 /*adjust number of items by one. */
return
/*──────────────────────────────────INORDER subroutine──────────────────*/
inOrder: parse arg q /*see if list Q is in order. */
@ -24,12 +24,13 @@ _=word(q,1); do j=2 to words(q); x=word(q,j)
if x<_ then return 0 /*Out of order? Then not sorted.*/
_=x
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. */
/*──────────────────────────────────PERMSETS subroutine─────────────────*/
permsets: procedure expose !. # #.; parse arg n,#.; #=0
do f=1 for n; !.f=f; end /*f*/; call .permAdd /*populate 1st perm*/
do while .permNext(n,0); call .permAdd; end /*while ···*/
permsets: procedure expose !. # #.; parse arg n,#.; #=0
do f=1 for n; !.f=f; end /*f*/
call .permAdd /*populate 1st perm*/
do while .permNext(n,0); call .permAdd; end /*while*/
return #
.permNext: procedure expose !.; parse arg n,i; nm=n-1
do k=nm by -1 for nm; kp=k+1
@ -39,14 +40,15 @@ return #
if i==0 then return 0; do j=i+1 while !.j<!.i; end /*j*/
parse value !.j !.i with !.i !.j
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: do ?=1 until inOrder(aList) /*look for the sorted permutation*/
aList=; do m=1 for items; _=word(#.?,m); aList=aList @._; end /*m*/
permSort: do ?=1 until inOrder($); $= /*look for the sorted permutation*/
do m=1 for #; _=word(#.?,m); $=$ @._; end /*m*/
end /*?*/
return
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
show@: widthH=length(items) /*maximum width of any line. */
do j=1 for items; say 'element' right(j,widthH) arg(1)":" @.j; end /*j*/
say copies('', 79) /*show a nice separator line. */
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