Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -1,34 +1,69 @@
class
COUNTING_SORT
feature
sort(ar: ARRAY[INTEGER]; min, max: INTEGER): ARRAY[INTEGER]
local
count: ARRAY[INTEGER]
i, j, z: INTEGER
do
create count.make_filled (0, 0, max-min)
from
i:= 0
until
i= ar.count
loop
count[ar[i]-min]:= count[ar[i]-min]+1
i:= i+1
end
across count as c loop io.put_string (c.item.out + "%T") end
z:= 0
from i:= min
until i>max
loop
from j:= 0
until j= count[i-min]
sort (ar: ARRAY [INTEGER]; min, max: INTEGER): ARRAY [INTEGER]
-- Sorted Array in ascending order.
require
ar_not_void: ar /= Void
lowest_index_zero: ar.lower = 0
local
count: ARRAY [INTEGER]
i, j, z: INTEGER
do
create Result.make_empty
Result.deep_copy (ar)
create count.make_filled (0, 0, max - min)
from
i := 0
until
i = Result.count
loop
ar[z]:=i
z:= z+1
j:= j+1
count [Result [i] - min] := count [Result [i] - min] + 1
i := i + 1
end
i:= i+1
z := 0
from
i := min
until
i > max
loop
from
j := 0
until
j = count [i - min]
loop
Result [z] := i
z := z + 1
j := j + 1
end
i := i + 1
end
ensure
Result_is_sorted: is_sorted (Result)
end
Result:= ar
end
feature {NONE}
is_sorted (ar: ARRAY [INTEGER]): BOOLEAN
--- Is 'ar' sorted in ascending order?
require
ar_not_empty: ar.is_empty = False
local
i: INTEGER
do
Result := True
from
i := ar.lower
until
i = ar.upper
loop
if ar [i] > ar [i + 1] then
Result := False
end
i := i + 1
end
end
end

View file

@ -1,24 +1,39 @@
class
APPLICATION
inherit
ARGUMENTS
create
make
feature
make
do
create test.make_filled(0,0,5)
test[0]:=-7
test[1]:=4
test[2]:=2
test[3]:=6
test[4]:=1
test[5]:=3
across test as t loop io.put_string (t.item.out + "%T") end
create count
test:=count.sort (test, -7, 6)
across test as ar loop io.put_string (ar.item.out+"%T") end
end
feature
make
do
create test.make_filled (0, 0, 5)
test [0] := -7
test [1] := 4
test [2] := 2
test [3] := 6
test [4] := 1
test [5] := 3
io.put_string ("unsorted:%N")
across
test as t
loop
io.put_string (t.item.out + "%T")
end
io.new_line
io.put_string ("sorted:%N")
create count
test := count.sort (test, -7, 6)
across
test as ar
loop
io.put_string (ar.item.out + "%T")
end
end
count: COUNTING_SORT
test: ARRAY[INTEGER]
test: ARRAY [INTEGER]
end

View file

@ -0,0 +1,24 @@
defmodule Sort do
def counting_sort([]), do: []
def counting_sort(list) do
{min, max} = minmax(list)
count = List.to_tuple(for _ <- min..max, do: 0)
counted = Enum.reduce(list, count, fn x,acc ->
i = x - min
put_elem(acc, i, elem(acc, i) + 1)
end)
Enum.reduce(max..min, [], fn n,acc ->
m = elem(counted, n - min)
List.duplicate(n, m) ++ acc
end)
end
defp minmax([h|t]), do: minmax(t, h, h)
defp minmax([], min, max), do: {min, max}
defp minmax([h|t], min, max) when h<min, do: minmax(t, h, max)
defp minmax([h|t], min, max) when max<h, do: minmax(t, min, h)
defp minmax([_|t], min, max) , do: minmax(t, min, max)
end
IO.inspect Sort.counting_sort([1,-2,-3,2,1,-5,5,5,4,5,9])

View file

@ -0,0 +1,33 @@
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)
for i in lo:hi
while cnt[i - lo + 1] > 0
b[z] = i
z += 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)

View file

@ -1,6 +1,6 @@
<?php
function counting_sort($arr, $min, $max)
function counting_sort(&$arr, $min, $max)
{
$count = array();
for($i = $min; $i <= $max; $i++)

View file

@ -2,7 +2,7 @@ $ages = array();
for($i=0; $i < 100; $i++) {
array_push($ages, rand(0, 140));
}
counting_sort(&$ages, 0, 140);
counting_sort($ages, 0, 140);
for($i=0; $i < 100; $i++) {
echo $ages[$i] . "\n";

View file

@ -1,32 +1,17 @@
/*REXX program sorts an array using the count-sort algorithm. */
call gen@ /*generate the array elements. */
call show@ 'before sort' /*show the before array elements.*/
call countSort # /*sort N entries of the @. array.*/
call show@ ' after sort' /*show the after array elements.*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────COUNTSORT subroutine────────────────*/
countSort: procedure expose @.; parse arg N; h=@.1; L=h
do i=2 to N; L=min(L,@.i); h=max(h,@.i); end /*i*/
_.=0; do j=1 for N; x=@.j; _.x=_.x+1; end /*j*/
x=1; do k=L to h; if _.k\==0 then do x=x for _.k
@.x=k
end /*x*/
end /*k*/
return
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
gen@: $=1 3 6 2 7 13 20 12 21 11 22 10 23 9 24 8 25 43 62 42 63 41 18 42 17 43 16 44 15 45 14 46 79 113 78 114 77 39 78 38
#=words($)
do j=1 for # /* [↓] assign 40 Recaman numbers.*/
@.j=word($,j) /*assign a Recaman # from a list.*/
end /*j*/
return
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
show@: do s=1 for #
say left('',9) "element" right(s,length(#)) arg(1)': ' @.s
end /*s*/
say copies('',50) /*show a pretty separator line. */
/*REXX program sorts an array using the count─sort algorithm. */
$=1 3 6 2 7 13 20 12 21 11 22 10 23 9 24 8 25 43 62 42 63 41 18 42 17 43 16 44 15 45 14 46 79 113 78 114 77 39 78 38
#=words($); w=length(#); do i=1 for #
@.i=word($,i) /*get a Recaman # from a list.*/
end /*i*/
call show 'before sort: ' /*show the before array elements. */
say copies('',55) /*show a pretty separator line. */
call countSort # /*sort a number of entries of @. array.*/
call show ' after sort: ' /*show the after array elements. */
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
countSort: procedure expose @.; parse arg N; L=@.1; h=L; _.=0; x=1
do j=1 for N; z=@.j; _.z=_.z+1; L=min(L,@.j); h=max(h,@.j); end /*j*/
do k=L to h; do x=x for _.k; @.x=k; end /*x*/; end /*k*/
return
/*────────────────────────────────────────────────────────────────────────────*/
show: do s=1 for #; say right("element",20) right(s,w) arg(1) @.s; end; return

View file

@ -7,11 +7,15 @@ class Array
min, max = minmax
count = Array.new(max - min + 1, 0)
each {|number| count[number - min] += 1}
(min..max).each_with_object([]) {|i, ary| count[i - min].times {ary << i}}
(min..max).each_with_object([]) {|i, ary| ary.concat([i] * count[i - min])}
end
end
ary = [9,7,10,2,9,7,4,3,10,2,7,10,2,1,3,8,7,3,9,5,8,5,1,6,3,7,5,4,6,9,9,6,6,10,2,4,5,2,8,2,2,5,2,9,3,3,5,7,8,4]
p ary.counting_sort.join(",")
# => "1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,4,4,4,4,5,5,5,5,5,5,6,6,6,6,7,7,7,7,7,7,8,8,8,8,9,9,9,9,9,9,10,10,10,10"
p ary = Array.new(20){rand(-10..10)}
# => [-3, -1, 9, -6, -8, -3, 5, -7, 4, 0, 5, 0, 2, -2, -6, 10, -10, -7, 5, -7]
p ary.counting_sort
# => [-10, -8, -7, -7, -7, -6, -6, -3, -3, -2, -1, 0, 0, 2, 4, 5, 5, 5, 9, 10]