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,33 +1,20 @@
function countsort{T<:Integer}(a::Array{T,1})
(lo, hi) = extrema(a)
b = zeros(T, length(a))
cnt = zeros(Int, hi - lo + 1)
for i in a
cnt[i - lo + 1] += 1
end
z = one(Int)
function countsort(a::Vector{<:Integer})
lo, hi = extrema(a)
b = zeros(a)
cnt = zeros(eltype(a), hi - lo + 1)
for i in a cnt[i-lo+1] += 1 end
z = 1
for i in lo:hi
while cnt[i - lo + 1] > 0
while cnt[i-lo+1] > 0
b[z] = i
z += 1
cnt[i - lo + 1] -= 1
cnt[i-lo+1] -= 1
end
end
return b
end
a = Uint8[rand(1:typemax(Uint8)) for i in 1:20]
println("Sort of Unsigned Bytes:")
println(" Before Sort:")
println(" ", a)
a = countsort(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 = countsort(a)
println("\n After Sort:")
println(" ", a)
v = rand(UInt8, 20)
println("# unsorted bytes: $v\n -> sorted bytes: $(countsort(v))")
v = rand(1:2 ^ 10, 20)
println("# unsorted integers: $v\n -> sorted integers: $(countsort(v))")