Solved

# Bubble sort

Posted on 2004-11-17
7,342 Views
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
0
Question by:garypyne

LVL 100

Accepted Solution

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

Author Comment

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

LVL 100

Expert Comment

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

LVL 6

Expert Comment

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

## Featured Post

### Suggested Solutions

Outlook Free & Paid Tools
For Sennheiser, comfort, quality and security are high priority areas. This paper addresses the security of Bluetooth technology and the supplementary security that Sennheiser’s Contact Center and Office (CC&O) headsets provide.
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…