[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Minlist, Maxlist (sorting an array)



PureBytes Links

Trading Reference Links

Bob Fulks provided the following code some time ago for sorting any size
array from smallest to largest. You'll need to extract the actual sort
routine from his indicator code. By changing one line, it can sort from
largest to smallest instead of the default smallest to largest:

if Arr[J] < Arr[K] then begin
change to
if Arr[J] > Arr[K] then begin

After a normal sort, the third smallest value would be Arr[3].
After a reverse sort, the third largest value would be Arr[3].

-- 
  Dennis

------------------------------------------------------------------------
Attached is EasyLanguage code for a very fast sort routine. This code is an
indicator that test the sort code so you should be able to extract the code
that actually does the sort.

I use it all the time. (I think I got the algorithm from Bob Brickey of
Scientific Approaches but I have forgotten.)

Bob Fulks



Input:    Max(100);
Vars:     K(0), J(0);
Array:    Arr[100](0);

if Date > 970901 then Print("1  ", Date:6:0, CurrentBar:5:0, K:3:0,
   Arr[K]:5:2);

if Date = 970908 then begin
  for K = 1 to Max begin
     Arr[K] = Close[K];
     Print("2  ", Date:6:0, CurrentBar:5:0, K:3:0, Arr[K]:5:2);
  end;
  K = 0;
end;

if LastBarOnChart then begin

  for K = 1 to Max - 1 begin
     for J = K + 1 to Max begin
       if Arr[J] < Arr[K] then begin
          Value1 = Arr[K];
          Arr[K] = Arr[J];
          Arr[J] = Value1;
          Print(K:3:0, J:3:0, Arr[K]:5:2, Arr[J]:5:2);
       end;
     end;
  end;

  for K = 1 to Max begin
     Print("3  ", Date:6:0, CurrentBar:5:0, K:3:0, Arr[K]:5:2);
  end;

end;

Plot1(0,"Plot1");