Advertisement
Guest User

Untitled

a guest
Nov 18th, 2014
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rexx 3.84 KB | None | 0 0
  1. /*REXX program performs a   radix sort   on a  stemmed  integer array.  */
  2. 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',
  3.       '29 10 31 10 14 19 12 10 37 21 16 11 41 12 43 15 11 25 47 11 14 12',
  4.       '20 17 53 11 16 13 22 31 59 12 61 33 13 12 18 16 67 21 26 14 71 12',
  5.       '73 39 13 23 18 18 79 13 12 43 83 14 22 45 32 17 89 13 20 27 34 49',
  6.       '24 13 97 16 17 14 101 22 103 19 15 55 107 13 109 18 40 15 113 -42'
  7. /*excluding -42, the abbreviated list is called the integer log function*/
  8. mina=word(aList,1);   maxa=mina
  9.      do n=1  for words(aList); x=word(aList,n); @.n=x  /*list ──► array.*/
  10.      mina =min(x,mina);  maxa=max(x,maxa)
  11.      width=max(length(abs(mina)),length(maxa))
  12.      end   /*n*/
  13. n=words(aList);    w=length(n);    call radSort n
  14.          do j=1  for n
  15.          say 'item' right(j,w) "after the radix sort:"  right(@.j,width+1)
  16.          end    /*j*/
  17. exit                                   /*stick a fork in it, we're done.*/
  18. /*───────────────────────────────────RADSORT subroutine─────────────────*/
  19. radSort:  procedure expose @. width;  parse arg size;  mote=c2d(' ');  #=1
  20. !.#._b=1;    !.#._i=1
  21. !.#._n=size;     do i=1  for size;    y=@.i;    @.i=right(abs(y),width,0)
  22.                  if y<0  then @.i='-'@.i
  23.                  end   /*i*/
  24. /*══════════════════════════════════════where the rubber meets the road.*/
  25.   do while #\==0; ctr.=0; L='ffff'x; low=!.#._b; n=!.#._n; radi=!.#._i; H=
  26.   #=#-1
  27.         do j=low  for n;     parse var @.j =(radi) _ +1;     ctr._=ctr._+1
  28.         if ctr._==1  then if _\==''  then do
  29.                                           if _<<L  then L=_
  30.                                           if _>>H  then H=_
  31.                                           end
  32.         end   /*j*/
  33.   if L>>H  then iterate
  34.   _=
  35.   if L==H  then if ctr._==0  then do;  #=#+1;   !.#._b=low
  36.                                                 !.#._n=n
  37.                                                 !.#._i=radi+1;     iterate
  38.                                   end
  39.   L=c2d(L);  H=c2d(H);    ?=ctr._+low;      top._=?;    ts=mote;     max=L
  40.                do k=L  to H;    _=d2c(k,1);    cen=ctr._
  41.                if cen>ts  then  parse value    cen  k    with    ts  max
  42.                ?=?+cen;   top._=?
  43.                end   /*k*/
  44.   pivot=low
  45.      do  while  pivot<low+n;         it=@.pivot
  46.           do forever
  47.           parse var it =(radi) _ +1; cen=top._-1; if pivot>=cen then leave
  48.           top._=cen;  ?=@.cen;  @.cen=it;  it=?
  49.           end    /*forever*/
  50.      top._=pivot;     @.pivot=it;    pivot=pivot+ctr._
  51.      end         /*while pivot<low+n*/
  52.   i=max
  53.       do until i==max;   _=d2c(i,1);   i=i+1;   if i>H then i=L;   d=ctr._
  54.       if d<=mote then do; if d>1 then call .radSortP top._,d; iterate; end
  55.       #=#+1;     !.#._b=top._
  56.                  !.#._n=d
  57.                  !.#._i=radi+1
  58.       end   /*until i==max*/
  59.   end       /*while #\==0 */
  60. /*═════════════════════════════════════we're done with the heavy lifting*/
  61. #=0;   do i=size by -1 to 1; if @.i>=0  then iterate; #=#+1; @@.#=@.i; end
  62.        do j=1 for size;      if @.j <0  then iterate; #=#+1; @@.#=@.j; end
  63.        do k=1 for size;  @.k=@@.k+0; end  /*combine neg&pos radix lists*/
  64. return
  65. /*───────────────────────────────────.radSortP subroutine───────────────*/
  66. .radSortP:     parse arg bbb,nnn
  67.           do k=bbb+1  for nnn-1;    q=@.k
  68.               do j=k-1 by -1 to bbb while q<<@.j;  jp=j+1;  @.jp=@.j;  end
  69.           jp=j+1;      @.jp=q
  70.           end   /*k*/
  71. return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement