Advertisement
electricmaster

bubbleSort.dpr

May 20th, 2015
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.08 KB | None | 0 0
  1. program bubbleSort;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.   SysUtils, StrUtils, Math;
  7.  
  8. const
  9.   IN_FILE = 'bubblesort.in';
  10.   OUT_FILE = 'bubblesort.out';
  11.   ENTRIES = 10;
  12. type
  13.   numberArray = array of integer;
  14. var
  15.   infile, outfile: textfile;
  16.   username: string;
  17.   lines, i, loops: integer;
  18.   data: numberArray;
  19.  
  20. function NoL: integer; // number of lines in infile
  21. var
  22.   i: integer;
  23.   value: string;
  24. begin
  25.   i := 0;
  26.   if not fileexists(IN_FILE) then begin
  27.     Result := 0;
  28.     exit;
  29.   end;
  30.   reset(infile);
  31.   while NOT eof(infile) do begin
  32.     readln(infile);
  33.     i := i+1;
  34.   end;
  35.   Result := i;
  36. end;
  37.  
  38. procedure construct;
  39. begin
  40.   if NOT fileexists(IN_FILE) then begin // a little housekeeping
  41.     writeln('[ERROR] File ', IN_FILE, ' does not exist.');
  42.     readln;
  43.     exit;
  44.   end;
  45.   assignfile(infile, IN_FILE);
  46.   reset(infile);
  47.   assignfile(outfile, OUT_FILE);
  48.   rewrite(outfile);
  49.   lines := NoL();
  50.  
  51.   writeln('What is your name?');
  52.   readln(username);
  53.   writeln;
  54.   writeln(outfile, 'Run by ', username, '.');
  55.   writeln(outfile);
  56. end;
  57.  
  58. function fetchData(): numberArray; overload; // Pull all data from infile
  59. var
  60.   i: integer;
  61.   data: numberArray;
  62. begin
  63.   if NOT fileexists(IN_FILE) then exit;
  64.   reset(infile);
  65.   setlength(data, lines);
  66.   for i := 1 to lines do readln(infile, data[i]);
  67.   Result := data;
  68. end;
  69.  
  70. function fetchData(subint, list: integer): numberArray; overload; // Pull selected data from infile
  71. { parameters: subint: number of entries per list, list: which list [ex. 1: 1-10, 2: 11-20, etc] }
  72. var
  73.   i, start: integer;
  74.   data, ans: numberArray;
  75. begin
  76.   if NOT fileexists(IN_FILE) then exit;
  77.   reset(infile);
  78.   data := fetchData();
  79.   setlength(ans, subint);
  80.   start := 1+subint*(list-1);
  81.   for i := 1 to 10 do
  82.     ans[i] := data[i+subint*(list-1)];
  83.   fetchData := ans;
  84. end;
  85.  
  86. function sort(data: numberArray; reverse: boolean): numberArray; // bubble sort the numbers
  87. var
  88.   t: array[0..1] of integer;
  89.   i, j: integer;
  90. begin
  91.   j := 0;
  92.   while j < length(data) do begin
  93.     for i := 1 to (length(data) - j) do begin
  94.       if reverse = false then begin // least to greatest
  95.         if data[i-1] > data[i] then begin
  96.           t[0] := data[i];
  97.           t[1] := data[i-1];
  98.           data[i-1] := t[0];
  99.           data[i] := t[1];
  100.         end;
  101.       end
  102.       else begin // greatest to least
  103.         if (data[i] > data[i-1]) AND (i > 1) then begin
  104.           t[0] := data[i-1];
  105.           t[1] := data[i];
  106.           data[i] := t[0];
  107.           data[i-1] := t[1];
  108.         end;
  109.       end;
  110.     end; //for
  111.     j := j+1;
  112.   end; // while
  113.   sort := data;
  114. end;
  115.  
  116. function mean(data: numberArray): real;
  117. var
  118.   l, ans: real;
  119.   i: integer;
  120. begin
  121.   ans := 0;
  122.   l := length(data);
  123.   for i := 1 to length(data) do ans := ans+data[i];
  124.   ans := ans/l;
  125.   mean := ans;
  126. end;
  127.  
  128. function median(data: numberArray): integer;
  129. begin
  130.   data := sort(data, false);
  131.   median := data[round(length(data)/2)];
  132. end;
  133.  
  134. function mode(data: numberArray): integer;
  135. var
  136.   i, max, value: integer;
  137.   occurances: numberArray;
  138. begin
  139.   data := sort(data, false);
  140.   max := 0;
  141.   setlength(occurances, data[length(data)]);
  142.   for i := 1 to length(data) do
  143.     occurances[data[i]] := occurances[data[i]]+1;
  144.   for i := 0 to length(occurances) do begin
  145.     if(occurances[i] > 0) AND (occurances[i] > max) then begin
  146.       max := occurances[i];
  147.       value := i;
  148.     end;
  149.   end;
  150.   mode := value;
  151. end;
  152.  
  153. procedure displayData(data: numberArray); overload; // Send data to outfile using LINE BREAKS
  154. var
  155.   i: integer;
  156. begin
  157.   for i := 1 to length(data) do writeln(outfile, data[i]);
  158. end;
  159. procedure displayData(data: numberArray; horizontal: boolean); overload; // Send data to outfile using COMMAS
  160. var
  161.   i: integer;
  162. begin
  163.   for i := 1 to length(data) do begin
  164.     if i = length(data) then write(outfile, data[i])
  165.     else write(outfile, data[i],', ');
  166.   end;
  167. end;
  168.  
  169. procedure destruct;
  170. begin
  171.   if fileexists(IN_FILE) then begin
  172.     writeln(outfile, 'This program was written by Michael MacLean 11/24/14.');
  173.     closefile(outfile);
  174.     writeln('Output saved to ', OUT_FILE,'.');
  175.     readln;
  176.   end;
  177. end;
  178.  
  179. begin //driver
  180.   construct;
  181.  
  182.   loops := floor(lines/ENTRIES); // number of times to loop code
  183.   for i := 1 to loops do begin
  184.     writeln(outfile, 'ORIGINAL DATA FOR LIST ', floatToStr(i), ': ');
  185.     displayData(fetchData(ENTRIES, i), true);
  186.     writeln(outfile); writeln(outfile);
  187.     writeln(outfile, 'LIST ', floatToStr(i), ' SORTED FROM LEAST TO GREATEST: ');
  188.     displayData(sort(fetchData(ENTRIES, i), false), true);
  189.     writeln(outfile); writeln(outfile);
  190.     writeln(outfile, 'LIST ', floatToStr(i), ' SORTED FROM GREATEST TO LEAST: ');
  191.     displayData(sort(fetchData(ENTRIES, i), true), true);
  192.     writeln(outfile); writeln(outfile);
  193.     writeln(outfile, 'List ', floatToStr(i), '''s mean: ', mean(fetchData(ENTRIES, i)):0:0);
  194.     writeln(outfile, 'List ', floatToStr(i), '''s median: ', median(fetchData(ENTRIES, i)));
  195.     writeln(outfile, 'List ', floatToStr(i), '''s mode: ', mode(fetchData(ENTRIES, i)));
  196.     writeln(outfile);
  197.   end;
  198.  
  199.   destruct;
  200. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement