This commit is contained in:
Ingy döt Net 2013-06-05 21:47:54 +00:00
parent 1f1ad49427
commit 6f050a029e
2496 changed files with 37609 additions and 3031 deletions

View file

@ -0,0 +1,118 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class RSortingRadixsort00 {
public RSortingRadixsort00() {
return;
}
public static int[] lsdRadixSort(int[] tlist) {
List<Integer> intermediates;
int[] limits = getLimits(tlist);
tlist = rescale(tlist, limits[1]);
for (int px = 1; px <= limits[2]; ++px) {
@SuppressWarnings("unchecked")
Queue<Integer> bukits[] = new Queue[10];
for (int ix = 0; ix < tlist.length; ++ix) {
int cval = tlist[ix];
int digit = (int) (cval / Math.pow(10, px - 1) % 10);
if (bukits[digit] == null) {
bukits[digit] = new LinkedList<>();
}
bukits[digit].add(cval);
}
intermediates = new ArrayList<>();
for (int bi = 0; bi < 10; ++bi) {
if (bukits[bi] != null) {
while (bukits[bi].size() > 0) {
int nextd;
nextd = bukits[bi].poll();
intermediates.add(nextd);
}
}
}
for (int iw = 0; iw < intermediates.size(); ++iw) {
tlist[iw] = intermediates.get(iw);
}
}
tlist = rescale(tlist, -limits[1]);
return tlist;
}
private static int[] rescale(int[] arry, int delta) {
for (int ix = 0; ix < arry.length; ++ix) {
arry[ix] -= delta;
}
return arry;
}
private static int[] getLimits(int[] tlist) {
int[] lims = new int[3];
for (int i_ = 0; i_ < tlist.length; ++i_) {
lims[0] = Math.max(lims[0], tlist[i_]);
lims[1] = Math.min(lims[1], tlist[i_]);
}
lims[2] = (int) Math.ceil(Math.log10(lims[0] - lims[1]));
return lims;
}
private static void runSample(String[] args) {
int[][] lists = {
new int[] { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, },
new int[] { -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, -0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, },
new int[] { 2, 24, 45, 0, 66, 75, 170, -802, -90, 1066, 666, },
new int[] { 170, 45, 75, 90, 2, 24, 802, 66, },
new int[] { -170, -45, -75, -90, -2, -24, -802, -66, },
};
long etime;
lsdRadixSort(Arrays.copyOf(lists[0], lists[0].length)); // do one pass to set up environment to remove it from timings
for (int[] tlist : lists) {
System.out.println(array2list(tlist));
etime = System.nanoTime();
tlist = lsdRadixSort(tlist);
etime = System.nanoTime() - etime;
System.out.println(array2list(tlist));
System.out.printf("Elapsed time: %fs%n", ((double) etime / 1_000_000_000.0));
System.out.println();
}
return;
}
private static List<Integer> array2list(int[] arry) {
List<Integer> target = new ArrayList<>(arry.length);
for (Integer iv : arry) {
target.add(iv);
}
return target;
}
public static void main(String[] args) {
runSample(args);
return;
}
}

View file

@ -0,0 +1,72 @@
/* NetRexx */
options replace format comments java crossref symbols nobinary
runSample(arg)
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method radixSort(tlist = Rexx[]) public static returns Rexx[]
-- scale the array to start at zero to allow handling of -ve values
parse getLimits(tlist) maxn minn maxl .
tlist = rescale(tlist, minn)
loop px = maxl to 1 by -1
bukits = ''
loop ix = 0 to tlist.length - 1
cval = tlist[ix].right(maxl, 0)
parse cval . =(px) digit +1 .
bukits[digit] = bukits[digit] (cval + 0) -- simulates a stack
end ix
intermediates = ''
loop bi = 0 to 9
intermediates = intermediates bukits[bi] -- sumulates unstack
end bi
-- reload array
loop iw = 1 to intermediates.words()
tlist[iw - 1] = intermediates.word(iw)
end iw
end px
-- restore the array to original scale
tlist = rescale(tlist, -minn)
return tlist
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method rescale(arry = Rexx[], newbase) private static returns Rexx[]
loop ix = 0 to arry.length - 1
arry[ix] = arry[ix] - newbase
end ix
return arry
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method getLimits(arry = Rexx[]) private static returns Rexx
maxn = 0
minn = 0
maxl = 0
loop i_ = 0 to arry.length - 1
maxn = maxn.max(arry[i_])
minn = minn.min(arry[i_])
end i_
maxl = (maxn - minn).length()
return maxn minn maxl
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method runSample(arg) private static
lists = [-
[2, 24, 45, 0, 66, 75, 170, -802, -90, 1066, 666], -
[170, 45, 75, 90, 2, 24, 802, 66], -
[10, 9, 8, 7, 8, 5, 4, 3, 2, 1, 0], -
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], -
[-10, -9, -8, -7, -8, -5, -4, -3, -2, -1, -0], -
[-0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10], -
[-10, -19, -18, -17, -18, -15, -14, -13, -12, -11, -100], -
[10, 9, 8, 7, 8, 5, 4, 3, 2, 1, 0, -0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10], -
[-10, -9, -8, -7, -8, -5, -4, -3, -2, -1, -0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] -
]
loop il = 0 to lists.length - 1
tlist = lists[il]
say ' Input:' Arrays.asList(tlist)
say 'Output:' Arrays.asList(radixSort(tlist))
say
end il
return

View file

@ -0,0 +1,86 @@
/* NetRexx */
options replace format comments java crossref symbols nobinary
import java.util.Queue
runSample(arg)
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method radixSort(tlist = Rexx[]) public static returns Rexx[]
-- scale the array to start at zero to allow handling of -ve values
limits = ''
parse '!MAXN !MINN !MAXL' maxn_ minn_ maxl_ .
parse getLimits(tlist) maxn minn maxl .
limits[maxn_] = maxn
limits[minn_] = minn
limits[maxl_] = maxl
tlist = rescale(tlist, limits[minn_])
loop px = limits[maxl_] to 1 by -1
bukits = Queue[10] -- stacks for digits 0 .. 9
loop ix = 0 while ix < tlist.length
cval = tlist[ix].right(limits[maxl_], 0)
parse cval . =(px) digit +1 . -- extract next digit (fun with parse)
-- alternatively: digit = (cval % (10 ** (px - 1))) // 10
if bukits[digit] == null then bukits[digit] = LinkedList()
bukits[digit].add((cval + 0))
end ix
intermediates = ArrayList()
loop bi = 0 to 9
if bukits[bi] \= null then loop while bukits[bi].size() > 0
nextd = bukits[bi].poll()
intermediates.add(nextd)
end
end bi
-- reload result array
loop iw = 0 while iw < intermediates.size()
tlist[iw] = Rexx intermediates.get(iw)
end iw
end px
-- restore the array to original scale
tlist = rescale(tlist, -limits[minn_])
return tlist
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method rescale(arry = Rexx[], newbase) private static returns Rexx[]
loop ix = 0 to arry.length - 1
arry[ix] = arry[ix] - newbase
end ix
return arry
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method getLimits(arry = Rexx[]) private static returns Rexx
maxn = 0
minn = 0
maxl = 0
loop i_ = 0 to arry.length - 1
maxn = maxn.max(arry[i_])
minn = minn.min(arry[i_])
end i_
maxl = (maxn - minn).length()
return maxn minn maxl
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method runSample(arg) private static
lists = [-
[2, 24, 45, 0, 66, 75, 170, -802, -90, 1066, 666], -
[170, 45, 75, 90, 2, 24, 802, 66], -
[10, 9, 8, 7, 8, 5, 4, 3, 2, 1, 0], -
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], -
[-10, -9, -8, -7, -8, -5, -4, -3, -2, -1, -0], -
[-0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10], -
[-10, -19, -18, -17, -18, -15, -14, -13, -12, -11, -100], -
[10, 9, 8, 7, 8, 5, 4, 3, 2, 1, 0, -0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10], -
[-10, -9, -8, -7, -8, -5, -4, -3, -2, -1, -0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] -
]
loop il = 0 to lists.length - 1
tlist = lists[il]
say ' Input:' Arrays.asList(tlist)
say 'Output:' Arrays.asList(radixSort(tlist))
say
end il
return

View file

@ -5,62 +5,51 @@ aList='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',
'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, the abbreviated list is called the integer log function*/
mina=word(aList,1); maxa=mina
do n=1 for words(aList); x=word(aList,n); @.n=x /*list ──► array.*/
mina=min(x,mina); maxa=max(x,maxa)
mina=word(aList,1); maxa=mina
do n=1 for words(aList); x=word(aList,n); @.n=x /*list ──► array.*/
mina =min(x,mina); maxa=max(x,maxa)
width=max(length(abs(mina)),length(maxa))
end
end /*n*/
n=words(aList); w=length(n); call radSort n
do j=1 for n
say 'item' right(j,w) "after the radix sort:" right(@.j,width+1)
do j=1 for n
say 'item' right(j,w) "after the radix sort:" right(@.j,width+1)
end /*j*/
exit /*stick a fork in it, we're done.*/
/*───────────────────────────────────RADSORT subroutine─────────────────*/
radSort: procedure expose @. width; parse arg size; mote=c2d(' '); #=1
!.#._b=1
!.#._i=1
!.#._n=size; do i=1 for size; y=@.i; @.i=right(abs(y),width,0)
if y<0 then @.i='-'@.i
!.#._b=1; !.#._i=1
!.#._n=size; do i=1 for size; y=@.i; @.i=right(abs(y),width,0)
if y<0 then @.i='-'@.i
end /*i*/
/*══════════════════════════════════════where the rubber meets the road.*/
do while #\==0; ctr.=0; L='ffff'x; low=!.#._b; n=!.#._n; radi=!.#._i; H=
#=#-1
do j=low for n; parse var @.j =(radi) _ +1; ctr._=ctr._+1
if ctr._==1 then if _\=='' then do
if _<<L then L=_
if _>>H then H=_
end
do j=low for n; parse var @.j =(radi) _ +1; ctr._=ctr._+1
if ctr._==1 then if _\=='' then do
if _<<L then L=_
if _>>H then H=_
end
end /*j*/
if L>>H then iterate
if L>>H then iterate
_=
if L==H then if ctr._==0 then do; #=#+1; !.#._b=low
!.#._n=n
!.#._i=radi+1; iterate
end
L=c2d(L); H=c2d(H); ?=ctr._+low; top._=?; ts=mote; max=L
do k=L to H; _=d2c(k,1); cen=ctr._
if L==H then if ctr._==0 then do; #=#+1; !.#._b=low
!.#._n=n
!.#._i=radi+1; iterate
end
L=c2d(L); H=c2d(H); ?=ctr._+low; top._=?; ts=mote; max=L
do k=L to H; _=d2c(k,1); cen=ctr._
if cen>ts then parse value cen k with ts max
?=?+cen; top._=?
end /*k*/
pivot=low
do while pivot<low+n; it=@.pivot
do while pivot<low+n; it=@.pivot
do forever
parse var it =(radi) _ +1; cen=top._-1; if pivot>=cen then leave
top._=cen; ?=@.cen; @.cen=it; it=?
end /*forever*/
top._=pivot; @.pivot=it; pivot=pivot+ctr._
top._=pivot; @.pivot=it; pivot=pivot+ctr._
end /*while pivot<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>1 then call .radSortP top._,d; iterate; end
#=#+1; !.#._b=top._
@ -69,13 +58,14 @@ radSort: procedure expose @. width; parse arg size; mote=c2d(' '); #=1
end /*until i==max*/
end /*while #\==0 */
/*═════════════════════════════════════we're done with the heavy lifting*/
do i=1 for size; @.i=@.i+0; end /*i*/
#=0; do i=size by -1 to 1; if @.i>=0 then iterate; #=#+1; @@.#=@.i; end
do j=1 for size; if @.j <0 then iterate; #=#+1; @@.#=@.j; end
do k=1 for size; @.k=@@.k+0; end /*combine neg&pos radix lists*/
return
/*───────────────────────────────────.radSortP subroutine───────────────*/
.radSortP: parse arg bbb,nnn
do k=bbb+1 for nnn-1; q=@.k
.radSortP: parse arg bbb,nnn
do k=bbb+1 for nnn-1; q=@.k
do j=k-1 by -1 to bbb while q<<@.j; jp=j+1; @.jp=@.j; end
jp=j+1; @.jp=q
jp=j+1; @.jp=q
end /*k*/
return

View file

@ -0,0 +1,29 @@
#lang racket
(require data/queue)
(define (radix-sort l r)
(define queues (for/vector #:length r ([_ r]) (make-queue)))
(let loop ([l l] [R 1])
(define all-zero? #t)
(for ([x (in-list l)])
(define x/R (quotient x R))
(enqueue! (vector-ref queues (modulo x/R r)) x)
(unless (zero? x/R) (set! all-zero? #f)))
(if all-zero? l
(loop (let q-loop ([i 0])
(define q (vector-ref queues i))
(let dq-loop ()
(if (queue-empty? q)
(if (< i (sub1 r)) (q-loop (add1 i)) '())
(cons (dequeue! q) (dq-loop)))))
(* R r)))))
(for/and ([i 10000]) ; run some tests on random lists with a random radix
(define (make-random-list)
(for/list ([i (+ 10 (random 10))]) (random 100000)))
(define (sorted? l)
(match l [(list) #t] [(list x) #t]
[(list x y more ...) (and (<= x y) (sorted? (cons y more)))]))
(sorted? (radix-sort (make-random-list) (+ 2 (random 98)))))
;; => #t, so all passed