June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,36 +1,22 @@
function beadsort{T<:Integer}(a::Array{T,1})
(lo, hi) = extrema(a)
if lo < 1
throw(DomainError())
end
hi = convert(Int, hi)
function beadsort(a::Vector{<:Integer})
lo, hi = extrema(a)
if lo < 1 throw(DomainError()) end
len = length(a)
abacus = falses(len, hi)
for (i, v) in enumerate(a)
abacus[i,1:v] = true
abacus[i, 1:v] = true
end
for i in 1:hi
v = sum(abacus[:,i])
v = sum(abacus[:, i])
if v < len
abacus[1:end-v,i] = false
abacus[end-v+1:end,i] = true
abacus[1:end-v, i] = false
abacus[end-v+1:end, i] = true
end
end
return T[sum(abacus[i,:]) for i in 1:len]
return collect(eltype(a), sum(abacus[i,:]) for i in 1:len)
end
a = Uint8[rand(1:typemax(Uint8)) for i in 1:20]
println("Sort of Unsigned Bytes:")
println(" Before Sort:")
println(" ", a)
a = beadsort(a)
println("\n After Sort:")
println(" ", a, "\n")
a = [rand(1:2^10) for i in 1:20]
println("Sort of Integers:")
println(" Before Sort:")
println(" ", a)
a = beadsort(a)
println("\n After Sort:")
println(" ", a)
v = rand(UInt8, 20)
println("# unsorted bytes: $v\n -> sorted bytes: $(beadsort(v))")
v = rand(1:2 ^ 10, 20)
println("# unsorted integers: $v\n -> sorted integers: $(beadsort(v))")

View file

@ -1,11 +1,11 @@
/*REXX program sorts a list of integers using the bead sort algorithm. */
/* [↑] define two dozen grasshopper numbers. */
/*REXX program sorts a list (four groups) of integers using the bead sort algorithm.*/
/* [↓] define two dozen grasshopper numbers. */
gHopper= 1 4 10 12 22 26 30 46 54 62 66 78 94 110 126 134 138 158 162 186 190 222 254 270
/* [] these are also called hexagonal pyramidal #s. */
/* [] these are also called hexagonal pyramidal #s. */
greenGrocer= 0 4 16 40 80 140 224 336 480 660 880 1144 1456 1820 2240 2720 3264 3876 4560
/*define twenty-three Bernoulli numerator numbers. */
/* [↓] define twenty-three Bernoulli numerator numbers*/
bernN= '1 -1 1 0 -1 0 1 0 -1 0 5 0 -691 0 7 0 -3617 0 43867 0 -174611 0'
/* [] also called the Reduced Totient function, and is*/
/* [] also called the Reduced Totient function, and is*/
/*also called Carmichael lambda, or the LAMBDA function*/
psi= 1 1 2 2 4 2 6 2 6 4 10 2 12 6 4 4 16 6 18 4 6 10 22 2 20 12 18 6 28 4 30 8 10 16
y= gHopper greenGrocer bernN psi /*combine the four lists into one list.*/
@ -15,8 +15,8 @@ call show ' after sort', beadSort(y) /*display the list after sor
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
beadSort: procedure; parse arg low . 1 high . 1 z,$; @.=0 /*$: the list to be sorted. */
do j=1 until z==''; parse var z x z /*pick the meat off the bone.*/
x=x/1; @.x=@.x+1 /*normalize X; bump counter.*/
do j=1 until z==''; parse var z x z /*pick the meat off the bone.*/
x= x / 1; @.x= @.x + 1 /*normalize X; bump counter.*/
low=min(low, x); high=max(high, x) /*track lowest and highest #.*/
end /*j*/
/* [↓] now, collect beads and*/
@ -24,8 +24,7 @@ beadSort: procedure; parse arg low . 1 high . 1 z,$; @.=0 /*$: the list to be
end /*m*/
return $
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: parse arg txt,y; w=length( words(y) )
do k=1 for words(y) /* [↑] twenty pad blanks. */
say right('element',30) right(k,w) txt":" right(word(y,k),9)
end /*k*/
return
show: parse arg txt,y; z=words(y); w=length(z)
do k=1 for z
say right('element',30) right(k,w) txt":" right( word(y,k), 9)
end /*k*/; return