• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7351
  • Last Modified:

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('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
0
garypyne
Asked:
garypyne
  • 2
1 Solution
 
mlmccCommented:
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
 
garypyneAuthor 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
 
mlmccCommented:
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.

Glad i could help

mlmcc
0
 
PoeticAudioCommented:
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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now