Advertisement
electricmaster

bubbleSort.dpr

Dec 20th, 2014
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.21 KB | None | 0 0
  1. type
  2.   numberArray = array of integer;
  3.  
  4. function sort(data: numberArray; reverse: boolean): numberArray; // bubble sort the numbers
  5. var
  6.   t: array[0..1] of integer;
  7.   i, j: integer;
  8. begin
  9.   j := 0;
  10.   while j < lines do begin
  11.     for i := 1 to (lines - j) do begin
  12.       if reverse = false then begin // least to greatest
  13.         if data[i-1] > data[i] then begin
  14.           t[0] := data[i];
  15.           t[1] := data[i-1];
  16.           data[i-1] := t[0];
  17.           data[i] := t[1];
  18.         end;
  19.       end
  20.       else begin // greatest to least
  21.         if data[i-1] < data[i] then begin
  22.           t[0] := data[i];
  23.           t[1] := data[i-1];
  24.           data[i-1] := t[0];
  25.           data[i] := t[1];
  26.         end;
  27.       end;
  28.     end; //for
  29.     j := j+1;
  30.   end; // while
  31.   sort := data;
  32. end;
  33.  
  34. function mode(data: numberArray): integer;
  35. var
  36.   i, j, max, value: integer;
  37.   tally: numberArray;
  38. begin
  39.   max := 0; value := 0;
  40.   data := sort(data, true);
  41.   setLength(tally, data[1]);
  42.   for i := 1 to length(data) do
  43.     tally[data[i]] := tally[data[i]];
  44.   for j := 1 to length(data) do begin
  45.     if(tally[j] > max) then begin
  46.       max := tally[j];
  47.       value := j;
  48.     end;
  49.   end;
  50.   mode := value;
  51. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement