About CLRS The following Oz code is derived from the examples provided in the book:
      "Introduction To Algorithms, Second Edition" by Thomas H. Corman, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein.
      http://mitpress.mit.edu/algorithms/

Chapter #5 Examples in Oz

% Utility functions
fun {RandomInt Min Max}
   X = {OS.rand}
   MinOS
   MaxOS
in
   {OS.randLimits ?MinOS ?MaxOS}
   Min + X*(Max - Min) div (MaxOS - MinOS)
end

{OS.srand 0}

proc {ArrayExchange A I J}
   X = A.I
in
   A.I := A.J
   A.J := X
end

Swap = ArrayExchange

% 5.3 Randomize In Place
proc {RandomizeInPlace A}
   N = {Array.high A}
in
   for I in 1..N do
      {ArrayExchange A I {RandomInt I N}}
   end
end

local
   A  = {Tuple.toArray 1#2#3#4#5#6}
in
   {RandomizeInPlace A}
   {Browse 'RandomizeInPlace'#{Array.toRecord '#' A}}
end

% 5.3 Permute With All
proc {PermuteWithAll A}
   N = {Array.high A}
in
   for I in 1..N do
      {Swap A I {RandomInt 1 N}}
   end
end

local
   A  = {Tuple.toArray 1#2#3#4#5#6}
in
   {PermuteWithAll A}
   {Browse 'PermuteWithAll'#{Array.toRecord '#' A}}
end

% 5.3 Permute By Cyclic
fun {PermuteByCyclic A}
   N = {Array.high A}
   Offset = {RandomInt 1 N}
   B = {NewArray {Array.low A} {Array.high A} _}
in
   for I in 1..N do
      local
         Dest = {NewCell I+Offset}
      in
         if @Dest > N then
            Dest := @Dest - N
         end
         B.@Dest := A.I
      end
   end
   B
end

local
   A  = {Tuple.toArray 1#2#3#4#5#6}
in
   {Browse 'PermuteByCyclic'#{Array.toRecord '#' {PermuteByCyclic A}}}
end

% 5.4 On Line Maximum
fun {OnLineMaximum Score K N}
   Return = {NewCell N}
   BestScore = {NewCell undefined}
in
   for I in 1..K do
      if @BestScore == undefined orelse Score.I > @BestScore then
         BestScore := Score.I
      end
   end
   for I in (K+1)..N do
      if Score.I > @BestScore then
         Return := I
      end
   end
   @Return
end

local
   A  = {Tuple.toArray 80#70#90#80#95#60}
in
   {Browse 'OnLineMaximum'#{OnLineMaximum A 4 6}}
end

Chris Rathman / Chris.Rathman@tx.rr.com