Skip to content

Instantly share code, notes, and snippets.

@dajobe
Created February 17, 2012 06:29
Show Gist options
  • Save dajobe/1851222 to your computer and use it in GitHub Desktop.
Save dajobe/1851222 to your computer and use it in GitHub Desktop.
{$C3000}
program bubble(input, output);
{ Program : bubble
Input : 1) Number of data items (maximum 500)
2) The data items Operation : Sorts the data using a bubble sort, the number of comparisons
and data accesses being counted.
Output : The number of comparisons and accesses and the sorted data in ascending order. Bugs : 1) The program does not check for invalid data type on input
so entering an invalid integer will cause a fatal crash.
}
const maxitems = 500;
type datatype = array [1..maxitems] of integer;
var
nocomparisons, noaccesses, size: integer;
data: datatype;
{ =================================================================================================================== }
{ Function : readsizedata
Input : From file (input) ie the terminal keyboard, the number of data items to be read in ( an integer)
Operation : Repeats the question until a valid number is entered
Output : The number of data items
}
function readsizedata: integer;
var
tempsize: integer;
begin
writeln('Please enter the number of data items');
read(tempsize);
while (tempsize < 2) or (tempsize > maxitems) do begin
writeln('Please enter a number greater than 1 and less than ', maxitems+1: 3);
read(tempsize)
end; { while }
readsizedata := tempsize
end; { readsizedata }
{ ==================================================================================================================== }
{ Procedure : inputdata
Input : Number of items to accept
Operation : Reads the data items from the input file and without checking for validity, stores them in the data array.
Output : A data array containing integers
}
procedure inputdata(var data: datatype; size: integer);
var
counter: integer;
begin
writeln;
for counter := 1 to size do begin
write('Please enter item (', counter: 3, ') :');
read(data[counter])
end
end; { inputdata }
{ =================================================================================================================== }
{ Procedure : sortdata
Input : The number of data items and the items themselves
Operation : Sorts the data using a bubble sort into ascending order
and counts the number of array accesses and the number of
comparisons.
Output : The sorted data , the number of comparisons and the number
of array accesses
}
procedure sortdata(var data: datatype;
size: integer;
var n comparisons: integer;
var noaccesses: integer);
var
limit, counter, tempdatum: integer;
finished: boolean;
begin
nocomparisons := 0;
noaccesses := 0;
limit := size;
repeat
finished := TRUE;
for counter := 1 to limit - 1 do begin
nocomparisons := nocomparisons + 1;
noaccesses := noaccesses + 2;
if data[counter] > data[counter + 1] then begin
noaccesses := noaccesses + 4;
tempdatum := data[counter];
data[counter] := data[counter + 1];
data[counter + 1] := tempdatum;
finished := FALSE
end
end;
limit := limit - 1
until (limit = 2) or finished
end; { sortdata }
{ ==================================================================================================================== }
{ Procedure : printdata
Input : The data array, its size, the number of comparisons and accesses
Operation : Prints the header information and the data items in order
and Output
}
procedure printdata(data: datatype; size, nocomparisons, noaccesses: integer);
var
counter: integer;
begin
writeln;
writeln('There were ', nocomparisons: 5, ' comparisons');
writeln(' and ', noaccesses: 5, ' accesses');
writeln;
for counter := 1 to size do
writeln('Item (', counter: 3, ') ', data[counter]: 10);
writeln;
end; { printdata }
{ ==================================================================================================================== }
{ Function : userisfinished
Input : Single character 'y' or 'n' followed by return
Operation : Checks if input is valid and repeats until it is so
Output : TRUE or FALSE depending on the input
}
function userisfinished : boolean;
var
answer : char;
begin
readln;
{ To clear the end of line character }
write('Do you want to enter more sets of data?');
repeat
readln(answer);
if not(answer in ['y','n']) then
begin
writeln;
write('Please type either y or n, more data? ');
end;
until (answer in ['y','n']);
userisfinished := (answer='n')
end; { userisfinished }
{ =================================================================================================================== }
begin { main program }
repeat
writeln;
size := readsizedata;
inputdata(data, size);
sortdata(data, size, nocomparisons, noaccesses;
printdata(data, size, nocomparisons, noaccesses)
until userisfinished
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment