# Bubble sort

Hey

i am trying to use a bubble sort, to try and sort numbers from 1 to 20 in order. so that array 1 is 1 and array 2 is 2 etc.

here is my code im using.

********************************************************************
Program arraytas(input,output);

uses crt,dos;

var

numbers : array [1..20] of integer;
choice : integer;
exitvar : integer;
count : integer;
small : integer;
num : integer;

(*************************** Smallest *************************************)

procedure smallest;

begin

numbers[1] := 4;
numbers[2] := 14;
numbers[3] := 13;
numbers[4] := 1;
numbers[5] := 7;
numbers[6] := 18;
numbers[7] := 20;
numbers[8] := 3;
numbers[9] := 8;
numbers[10] := 2;
numbers[11] := 12;
numbers[12] := 17;
numbers[13] := 19;
numbers[14] := 6;
numbers[15] := 9;
numbers[16] := 10;
numbers[17] := 11;
numbers[18] := 15;
numbers[19] := 5;
numbers[20] := 16;

small := numbers[1];

for count := 1 to 20 do
begin
if numbers[count] < small then
begin
small := numbers[count];
num := count

end;
end;

writeln( 'the smallest number is ', small, ' and is in position ', num);

end;

(************************* Largest ****************************************)

procedure largest;

begin

numbers[1] := 4;
numbers[2] := 14;
numbers[3] := 13;
numbers[4] := 1;
numbers[5] := 7;
numbers[6] := 18;
numbers[7] := 20;
numbers[8] := 3;
numbers[9] := 8;
numbers[10] := 2;
numbers[11] := 12;
numbers[12] := 17;
numbers[13] := 19;
numbers[14] := 6;
numbers[15] := 9;
numbers[16] := 10;
numbers[17] := 11;
numbers[18] := 15;
numbers[19] := 5;
numbers[20] := 16;

small := numbers[1];

for count := 1 to 20 do
begin
if numbers[count] > small then
begin
small := numbers[count];
num := count

end;
end;

writeln( 'the Largest number is ', small, ' and is in position ', num);

end;

(******************************* Main ***********************************)

begin

clrscr;

repeat

writeln('1. Find teh smallest number');
writeln('2. Find the Largest Number');
writeln('3. Quit');

if choice = 1 then
smallest;
if choice = 2 then
largest;
if choice = 3 then
exitvar := 3

until exitvar = 3

end.
******************************************************************

could anyone help me add the bulled sort part pls.

any help would be appreciated

ty

gary
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Since this is probably homework, I won't give you a solution.  Here is the basic algorithm

For each pair x(i) and x(i+1) compare
If out of order
swap
Repeat until no more swaps

mlmcc
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
lol no its not homework he said were goin over it nxt class, so i thought i ask to see if i could figure it out but i get te jist of it ty
0
Commented:
Good.  Your program  looks like one that I assign sometimes so I thought rather than give the answer I would explain the algorithm.

For the future.  We can help you with your programs but will not do them for you.  For instance if you had the bubble sort but it wasn't quite working we could help fix the problem.

mlmcc
0
Commented:
a tip
(psuedocode)

TempHolder: Integer;

if numA < numB
TempHolder = numA
numA = numB
numB = TempHolder

because if you do something like:

numA = numB
numB = numA

think about it... if numA is 4 and numB is 9 then it's like saying
numA = 9
numB = 9

since numA was already assigned before the numB assignment, therefore you need a temp variable to store one of the values
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Pascal

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.