For each pair x(i) and x(i+1) compare

If out of order

swap

Repeat until no more swaps

mlmcc

Solved

Posted on 2004-11-17

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('Please select your required tool');

writeln('1. Find teh smallest number');

writeln('2. Find the Largest Number');

writeln('3. Quit');

readln(choice);

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

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;

(*************************

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;

(*************************

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;

(*************************

begin

clrscr;

repeat

writeln('Please select your required tool');

writeln('1. Find teh smallest number');

writeln('2. Find the Largest Number');

writeln('3. Quit');

readln(choice);

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

4 Comments

For each pair x(i) and x(i+1) compare

If out of order

swap

Repeat until no more swaps

mlmcc

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.

Glad i could help

mlmcc

(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

Title | # Comments | Views | Activity |
---|---|---|---|

Pascal -- How do I use the variables of a procedure in another? | 4 | 681 | |

SetWindowsHookEx capturing unicode (universal) characters ? | 1 | 1,756 | |

Determine Drive Types | 2 | 463 | |

find a node in VST | 2 | 37 |

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**16** Experts available now in Live!