September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -0,0 +1,27 @@
function radixsort(tobesorted::Vector{Int64})
arr = deepcopy(tobesorted)
for shift in 63:-1:0
tmp = Vector{Int64}(undef, length(arr))
j = 0
for i in 1:length(arr)
if (shift == 0) == ((arr[i] << shift) >= 0)
arr[i - j] = arr[i]
else
tmp[j + 1] = arr[i]
j += 1
end
end
tmp[j+1:end] .= arr[1:length(tmp)-j]
arr = tmp
end
arr
end
function testradixsort()
arrays = [[170, 45, 75, -90, -802, 24, 2, 66], [-4, 5, -26, 58, -990, 331, 331, 990, -1837, 2028]]
for array in arrays
println(radixsort(array))
end
end
testradixsort()

View file

@ -1,5 +1,5 @@
sub radsort (@ints) {
my $maxlen = [max] @ints».chars;
my $maxlen = max @ints».chars;
my @list = @ints».fmt("\%0{$maxlen}d");
for reverse ^$maxlen -> $r {

View file

@ -0,0 +1,70 @@
#python3.7 <
def flatten(some_list):
"""
Flatten a list of lists.
Usage: flatten([[list a], [list b], ...])
Output: [elements of list a, elements of list b]
"""
new_list = []
for sub_list in some_list:
new_list += sub_list
return new_list
def radix(some_list, idex=None, size=None):
"""
Recursive radix sort
Usage: radix([unsorted list])
Output: [sorted list]
"""
# Initialize variables not set in the initial call
if size == None:
largest_num = max(some_list)
largest_num_str = str(largest_num)
largest_num_len = len(largest_num_str)
size = largest_num_len
if idex == None:
idex = size
# Translate the index we're looking at into an array index.
# e.g., looking at the 10's place for 100:
# size: 3
# idex: 2
# i: (3-2) == 1
# str(123)[i] -> 2
i = size - idex
# The recursive base case.
# Hint: out of range indexing errors
if i >= size:
return some_list
# Initialize the bins we will place numbers into
bins = [[] for _ in range(10)]
# Iterate over the list of numbers we are given
for e in some_list:
# The destination bin; e.g.,:
# size: 5
# e: 29
# num_s: '00029'
# i: 3
# dest_c: '2'
# dest_i: 2
num_s = str(e).zfill(size)
dest_c = num_s[i]
dest_i = int(dest_c)
bins[dest_i] += [e]
result = []
for b in bins:
# Make the recursive call
# Sort each of the sub-lists in our bins
result.append(radix(b, idex-1, size))
# Flatten our list
# This is also called in our recursive call,
# so we don't need flatten to be recursive.
flattened_result = flatten(result)
return flattened_result

View file

@ -0,0 +1,21 @@
#python3.7 <
def flatten(l):
return [y for x in l for y in x]
def radix(l, p=None, s=None):
if s == None:
s = len(str(max(l)))
if p == None:
p = s
i = s - p
if i >= s:
return l
bins = [[] for _ in range(10)]
for e in l:
bins[int(str(e).zfill(s)[i])] += [e]
return flatten([radix(b, p-1, s) for b in bins]

View file

@ -1,25 +1,24 @@
/*REXX program performs a radix sort on an integer array (can be negative/zero/positive)*/
call gen /*call subroutine to generate numbers. */
call radSort n /*invoke the radix sort subroutine. */
/* [↓] display sorted items ───► term.*/
do j=1 for n; say 'item' right(j,w) "after the radix sort:" right(@.j,w); end
do j=1 for n; say 'item' right(j, w) "after the radix sort:" right(@.j, w)
end /*j*/ /* [↑] display sorted items ───► term.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
gen: w=0 /*the max width of an number in the ILF*/
ILF=' 0 2 3 4 5 5 7. 6 6 7 11 7 13 9 8 8 17 8 19 9 10 13 23 9 10 15' ,
' 9 11 29 10 31 10 14 19 12 10 37 21 16 11 41 12 43 15 11 25 47 11 14 12 20 17' ,
'53 11 16 13 22 31 59 12 61 33 13 12 18 16 67 21 26 14 71 12 73 39 13 23 18 18' ,
'79 13 12 43 83 14 22 45 32 17 89 13 20 27 34 49 24 13 97 16 17 14 101' ,
gen: ILF= 0 2 3 4 5 5 7. 6 6 7 11 7 13 9 8 8 17 8 19 9 10 13 23 9 10 15 ,
9 11 29 10 31 10 14 19 12 10 37 21 16 11 41 12 43 15 11 25 47 11 14 12 20 17 ,
53 11 16 13 22 31 59 12 61 33 13 12 18 16 67 21 26 14 71 12 73 39 13 23 18 18 ,
79 13 12 43 83 14 22 45 32 17 89 13 20 27 34 49 24 13 97 16 17 14 101 ,
'22 103 19 15 55 107 13 109 18 40 15 113 -42'
/*excluding -42, abbreviated above list is called the integer log function*/
n=words(ILF) /* I────── L── F───────*/
do m=1 for n; _=word(ILF,m)+0; @.m=_; w=max(w,length(_)); end
return /* [↑] W: max width of numbers. */
n= words(ILF) /* I────── L── F───────*/
w= 0; do m=1 for n; _= word(ILF,m) +0; @.m= _; w= max(w, length(_) )
end /*m*/; return /*W: is the maximum width ↑ of numbers*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
radSort: procedure expose @. w; parse arg size; mote=c2d(' '); #=1; !.#._n=size
radSort: procedure expose @. w; parse arg size; mote= c2d(' '); #= 1; !.#._n= size
!.#._b=1;
!.#._i=1; do i=1 for size; y=@.i; @.i=right(abs(y), w, 0); if y<0 then @.i='-'@.i
end /*i*/ /* [↑] negative case*/
!.#._i=1; do i=1 for size; y=@.i; @.i= right(abs(y), w, 0); if y<0 then @.i= '-'@.i
end /*i*/ /* [↑] negative case.*/
do while #\==0; ctr.=0; L='ffff'x; low=!.#._b; n=!.#._n; $=!.#._i; H=
#=#-1 /* [↑] is the radix. */
@ -29,34 +28,36 @@ radSort: procedure expose @. w; parse arg size; mote=c2d(' '); #=1; !
end /*j*/ /* └┴─────◄─── << is a strict comparison.*/
_= /* ┌──◄─── >> " " " " */
if L>>H then iterate /*◄─────┘ */
if L==H & ctr._==0 then do; #=#+1; !.#._b=low; !.#._n=n; !.#._i=$ + 1; iterate
if L==H & ctr._==0 then do; #= #+1; !.#._b= low; !.#._n= n; !.#._i= $+1; iterate
end
L=c2d(L); H=c2d(H); ?=ctr._ + low; top._=?; ts=mote
max=L
do k=L to H; _=d2c(k,1); c=ctr._ /* [↓] swap two item radices.*/
if c>ts then parse value c k with ts max; ?=?+c; top._=?
L= c2d(L); H= c2d(H); ?= ctr._ + low; top._= ?; ts= mote
max= L
do k=L to H; _= d2c(k,1); c= ctr._ /* [↓] swap 2 item radices.*/
if c>ts then parse value c k with ts max; ?= ?+c; top._= ?
end /*k*/
piv=low /*set PIVot to the low part of the sort*/
do while piv < low+n
it=@.piv
do forever; parse var it =($) _ +1; c=top._ -1
if piv>=c then leave; top._=c; ?=@.c; @.c=it; it=?
piv= low /*set PIVot to the low part of the sort*/
do while piv<low+n
it= @.piv
do forever; parse var it =($) _ +1; c= top._ -1
if piv>=c then leave; top._= c; ?= @.c; @.c= it; it= ?
end /*forever*/
top._=piv; @.piv=it; piv=piv + ctr._
top._= piv; @.piv=it; piv=piv + ctr._
end /*while piv<low+n */
i=max
do until i==max; _=d2c(i,1); i=i+1; if i>H then i=L; d=ctr._
if d<=mote then do; if d<2 then iterate; b=top._
do k=b+1 for d-1; q=@.k
do j=k-1 by -1 to b while q<<@.j; jp=j+1; @.jp=@.j; end
jp=j+1; @.jp=q
end /*k*/
iterate
end
#=#+1; !.#._b=top._; !.#._n=d; !.#._i=$+1
i= max
do until i==max; _= d2c(i, 1); i= i+1; if i>H then i= L; d= ctr._
if d<=mote then do; if d<2 then iterate; b= top._
do k=b+1 for d-1; q= @.k
do j=k-1 by -1 to b while q<<@.j; jp= j+1; @.jp= @.j
end /*j*/
jp= j+1; @.jp= q
end /*k*/
iterate
end
#= #+1; !.#._b= top._; !.#._n= d; !.#._i= $ + 1
end /*until i==max*/
end /*while #\==0 */
#=0 /* [↓↓↓] handle neg. and pos. arrays. */
do i=size by -1 to 1; if @.i>=0 then iterate; #=#+1; @@.#=@.i; end
do j=1 for size; if @.j>=0 then do; #=#+1; @@.#=@.j; end; @.j=@@.j+0; end
return /* [↑↑↑] combine 2 lists into 1 list. */
#= 0 /* [↓↓↓] handle neg. and pos. arrays. */
do i=size by -1 to 1; if @.i>=0 then iterate; #=#+1; @@.#=@.i
end /*i*/
do j=1 for size; if @.j>=0 then do; #= #+1; @@.#= @.j; end; @.j= @@.j+0
end /*j*/; return /* [↑↑↑] combine 2 lists into 1 list. */