September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,59 @@
' version 07-04-2017
' compile with: fbc -s console
' Heap's algorithm non-recursive
Function permutation_sort(a() As ULong) As ULong
Dim As ULong i, j, count
Dim As ULong lb = LBound(a), ub = UBound(a)
Dim As ULong n = ub - lb +1
Dim As ULong c(lb To ub)
While i < n
If c(i) < i Then
If (i And 1) = 0 Then
Swap a(0), a(i)
Else
Swap a(c(i)), a(i)
End If
count += 1
For j = lb To ub -1
If a(j) > a(j +1) Then j = 99
Next
If j < 99 Then Return count
c(i) += 1
i = 0
Else
c(i) = 0
i += 1
End If
Wend
End Function
' ------=< MAIN >=------
Dim As ULong k, p, arr(0 To 9)
Randomize Timer
Print "unsorted array"
For k = LBound(arr) To UBound(arr)
arr(k) = Rnd * 1000
Print arr(k) & IIf(k = UBound(arr), "", ", ");
Next
Print : Print
p = permutation_sort(arr())
Print "sorted array"
For k = LBound(arr) To UBound(arr)
Print arr(k) & IIf(k = UBound(arr), "", ", ");
Next
Print : Print
Print "sorted array in "; p; " permutations"
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End

View file

@ -0,0 +1,14 @@
# v0.6
using Combinatorics
function permsort(x::Array)
for perm in permutations(x)
if issorted(perm)
return perm
end
end
end
x = randn(10)
@show x permsort(x)

View file

@ -0,0 +1,49 @@
// version 1.1.2
fun <T : Comparable<T>> isSorted(list: List<T>): Boolean {
val size = list.size
if (size < 2) return true
for (i in 1 until size) {
if (list[i] < list[i - 1]) return false
}
return true
}
fun <T : Comparable<T>> permute(input: List<T>): List<List<T>> {
if (input.size == 1) return listOf(input)
val perms = mutableListOf<List<T>>()
val toInsert = input[0]
for (perm in permute(input.drop(1))) {
for (i in 0..perm.size) {
val newPerm = perm.toMutableList()
newPerm.add(i, toInsert)
perms.add(newPerm)
}
}
return perms
}
fun <T : Comparable<T>> permutationSort(input: List<T>): List<T> {
if (input.size == 1) return input
val toInsert = input[0]
for (perm in permute(input.drop(1))) {
for (i in 0..perm.size) {
val newPerm = perm.toMutableList()
newPerm.add(i, toInsert)
if (isSorted(newPerm)) return newPerm
}
}
return input
}
fun main(args: Array<String>) {
val input = listOf('d', 'b', 'e', 'a', 'f', 'c')
println("Before sorting : $input")
val output = permutationSort(input)
println("After sorting : $output")
println()
val input2 = listOf("first", "second", "third", "fourth", "fifth", "sixth")
println("Before sorting : $input2")
val output2 = permutationSort(input2)
println("After sorting : $output2")
}

View file

@ -0,0 +1,30 @@
-- Return an iterator to produce every permutation of list
function permute (list)
local function perm (list, n)
if n == 0 then coroutine.yield(list) end
for i = 1, n do
list[i], list[n] = list[n], list[i]
perm(list, n - 1)
list[i], list[n] = list[n], list[i]
end
end
return coroutine.wrap(function() perm(list, #list) end)
end
-- Return true if table t is in ascending order or false if not
function inOrder (t)
for pos = 2, #t do
if t[pos] < t[pos - 1] then
return false
end
end
return true
end
-- Main procedure
local list = {2,3,1} --\ Written to match task pseudocode,
local nextPermutation = permute(list) --\ more idiomatic would be:
while not inOrder(list) do --\
list = nextPermutation() --/ for p in permute(list) do
end --/ stuffWith(p)
print(unpack(list)) --/ end

View file

@ -1,53 +1,43 @@
/*REXX program sorts and displays an array using the permutation-sort method. */
call gen@ /*generate the array elements. */
call show@ 'before sort' /*show the before array elements. */
say copies('', 70) /*show separator line between displays.*/
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."
call gen /*generate the array elements. */
call show 'before sort' /*show the before array elements. */
say copies('', 75) /*show separator line between displays.*/
call pSort 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 all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
gen@: @.=; @.1= '---Four_horsemen_of_the_Apocalypse---'
@.2= '====================================='
@.3= 'Famineblack_horse'
@.4= 'Deathpale_horse'
@.5= 'Pestilence_[Slaughter]red_horse'
@.6= 'Conquest_[War]white_horse'
/* [↓] also assign to another array. */
do L=1 while @.L\==''; @@.L=@.L; end /* [↓] find number of entries in array*/
L=L-1 /*adjust the number of items by one. */
return
.pAdd: #=#+1; do j=1 for N; #.#=#.# !.j; end; return /*add a permutation.*/
show: do j=1 for L; say @e right(j,wL) arg(1)":" translate(@.j,,'_'); end; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
inOrder: parse arg q /*see if Q list is in order.*/
_=word(q,1); do j=2 to words(q); x=word(q,j)
if x<_ then return 0 /*Out of order? Not sorted. */
_=x
end /*j*/
do k=1 for #; _=word(#.?,k); @.k=@@._; end /*k*/
return 1 /*they're all in order finally*/
gen: @.=; @.1 = '---Four_horsemen_of_the_Apocalypse---'
@.2 = '====================================='
@.3 = 'Famineblack_horse'
@.4 = 'Deathpale_horse'
@.5 = 'Pestilence_[Slaughter]red_horse'
@.6 = 'Conquest_[War]white_horse'
@e=right('element', 15) /*literal used for the display.*/
do L=1 while @.L\==''; @@.L=@.L; end; L=L-1; wL=length(L); return
/*──────────────────────────────────────────────────────────────────────────────────────*/
.permAdd: #=#+1; do j=1 for N; #.#=#.# !.j; end /*j*/; return
isOrd: parse arg q /*see if Q list is in order. */
_=word(q, 1); do j=2 to words(q); x=word(q, j); if x<_ then return 0; _=x
end /*j*/ /* [↑] Out of order? ¬sorted*/
do k=1 for #; _=word(#.?, k); @.k=@@._; end /*k*/; return 1 /*in order.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
.permNext: procedure expose !.; parse arg n,i; nm=n-1
do k=nm by -1 for nm; kp=k+1
if !.k<!.kp then do; i=k; leave; end
end /*k*/
do j=i+1 while j<n; parse value !.j !.n with !.n !.j; n=n-1; end
if i==0 then return 0; do j=i+1 while !.j<!.i; end /*j*/
parse value !.j !.i with !.i !.j
return 1
.pNext: procedure expose !.; parse arg n,i; nm=n-1
do k=nm by -1 for nm; kp=k+1
if !.k<!.kp then do; i=k; leave; end
end /*k*/ /* [↓] swap two array elements*/
do j=i+1 while j<n; parse value !.j !.n with !.n !.j; n=n-1; end /*j*/
if i==0 then return 0 /*0: indicates no more perms. */
do j=i+1 while !.j<!.i; end /*j*/ /*search perm for a lower value*/
parse value !.j !.i with !.i !.j /*swap two values in !. array.*/
return 1
/*──────────────────────────────────────────────────────────────────────────────────────*/
permsets: procedure expose !. # #.; parse arg n,#.; #=0
do f=1 for n; !.f=f; end /*f*/
call .permAdd; do while .permNext(n,0); call .permAdd; end /*while*/
return #
/*──────────────────────────────────────────────────────────────────────────────────────*/
permSort: do ?=1 until inOrder($); $= /*find sorted permutation.*/
do m=1 for #; _=word(#.? ,m); $=$ @._; end /*m*/
end /*?*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
show@: do j=1 for L; say ' element' right(j,length(L)) arg(1)":" @.j
end /*j*/ /* [↑] display array elements*/
return
pSort: parse arg n,#.; #=0 /*generate L items (!) permutations.*/
do f=1 for n; !.f=f; end /*f*/
call .pAdd; do while .pNext(n, 0); call .pAdd; end /*while*/
do ?=1 until isOrd($); $= /*find perm.*/
do m=1 for #; _=word(#.?, m); $=$ @._; end /*m*/ /*build list*/
end /*?*/
return

View file

@ -0,0 +1,8 @@
rns:=T(4, 65, 2, 31, 0, 99, 2, 83, 782, 1);
fcn psort(list){ len:=list.len(); cnt:=Ref(0);
foreach ns in (Utils.Helpers.permuteW(list)){ // lasy permutations
cnt.set(1);
ns.reduce('wrap(p,n){ if(p>n)return(Void.Stop); cnt.inc(); n });
if(cnt.value==len) return(ns);
}
}(rns).println();