Solved

Bubble Sort - Accending and Deccending Order ?

Posted on 2000-04-13
5
848 Views
Last Modified: 2008-01-09
Hello,

I need some help on figuring out how to use a bubble sort method, to arrange all the numbers that a user enters in for (Number[x]) , in accending and deccending order.

Any help would be greatly appreciated.




-----------------------------

Program AccArray;


Uses CRT, dos;

Var   Number: array [1..5] of integer;
      Arr: array [1..100] of integer;
      numbers,numtotal,x,prevnumber,max,i,j:integer;
      ans,Yes,No:char;



Begin

  Repeat

        clrscr;

        numtotal:= 0 ;

        Repeat
               write('How many numbers (maximum = 100)? ') ;
               readln(numbers);
               writeln;

                 Begin
                   If numbers > 100 then
                      Begin
                        writeln('ERROR - Please enter a number between 1 and 100') ;
                        writeln;
                      End
                 End;

                 Begin
                   If numbers < 1 then
                     Begin
                       writeln('ERROR - Please enter a number between 1 and 100' );
                       writeln;
                     End
                 End;

                 Begin
                   If numbers < 101 then
                     If numbers > 0  then
                      numtotal:= 5;
                 End;



             Until numtotal = 5  ;



       For x:= 1 to numbers do
       Begin

         write('Enter  # ' , x  ,  ' : '  ) ;
         readln(Number[x]) ;

       End;

       writeln;


{

I need some kind of bubble sort code in here, to arrange the numbers I entered for Number[x] in accending and deccending order.

}

       





                writeln;
                Write ('Would you like to enter more numbers (Y/N)? ') ;
                readln (ans);
                writeln;


  Until ans = 'N' ;





End.
0
Comment
Question by:R_K
5 Comments
 

Expert Comment

by:psychorakz
ID: 2714051
http://www.cs.adfa.oz.au/teaching/studinfo/ada/Searching/bubble_sort.html
all you'll need, info/algorithem/source
enjoy
   -kay
0
 
LVL 6

Accepted Solution

by:
My name is Mud earned 200 total points
ID: 2714889
Program BubbleSort;
Uses
  Crt;
Type
  PointerToList = ^List;
  List = Record
    Value: Integer;
    PrevItem: PointerToList;
    NextItem: PointerToList;
  End;
Var
  Start,Previous,Current,Finish: PointerToList;
  mValue: Integer;

{ Function to count the number of items in the list.}
Function Count_Items: Integer;
Var
  I: Integer;
Begin
  Current:=Start;
  I:=0;
  While Current <> Nil Do
    Begin
      I:=I+1;
      Current:=Current^.NextItem;
    End;
  Count_Items:=I;
End;

{ Procedure to add items into the list }
Procedure AddItem (Newvalue: Integer);
Begin
  {Check If this is the first time the structure is going to be created}
  If Current = NIL Then
    Begin
      New(Current); {Get memory for the data}
      With Current^ do  {Fill in the data}
        Begin
          Value:=NewValue;
          NextItem:=NIL; {Since there are any more Up this points NIL}
          PrevItem:=NIL  {Since there are any more Down this points NIL}
        End;
      Start:=Current;
      Finish:=Current
    End
  Else  {Now this is not the first time}
    Begin
      New(Finish);
      Finish^.PrevItem:=Current; {Current <- Finish}
      Finish^.NextItem:=NIL;     {Finish -> Nil}
      Current^.NextItem:=Finish; {Current -> Finish}
      Current:=Finish;       {Current:=Finish}
      With Current^ do  {Fill in the data}
        Begin
          Value:=NewValue;
        End
    End
End;

{ Procedure to sort the list using the bubble sort method }
Procedure Sort_List;
Var
  Index1,Index2: Integer;
  MaxNum: Integer;

  Procedure ChangePlaces(Var Prev,Curr: PointerToList);
  Var
    Temp: Integer;
  Begin
    Temp:=Prev^.Value;
    Prev^.Value:=Curr^.Value;
    Curr^.Value:=Temp;
  End;

Begin
  MaxNum:=Count_Items;
  Previous:=Start;
  Current:=Finish;
  For Index1:=1 To MaxNum-1 Do
    Begin
      For Index2:=MaxNum DownTo Index1+1 Do
        Begin
          If Previous^.Value > Current^.Value Then
            ChangePlaces(Previous,Current);
          Current:=Current^.PrevItem;
        End;
      Previous:=Previous^.NextItem;
      Current:=Finish;
    End
End;

{ Procedure to print the list }
Procedure Print_List;
Begin
  Current:=Start;
  While Current <> Nil Do
    Begin
      Write(Current^.Value,'  ');
      Current:=Current^.NextItem;
    End
End;

Procedure Dispose_All;
Begin
  Current:=Start;
  While Current^.NextItem <> Nil Do
    Begin
      Previous:=Current;
      Current:=Current^.NextItem;
      If Previous <> Nil Then
        Dispose(Previous);
    End;
  Dispose(Current);
End;

{ The main program }
Begin
  ClrScr;
  Start := Nil;
  { Adding items to the list }
  Repeat
    Write (' Enter a Number : ');
    Readln (mValue);
    If mValue <> 0 Then
      AddItem (mValue);
  Until mValue = 0;
  { Sorting the list }
  Sort_List;
  { Printing the list }
  Print_List;
  Dispose_All;
End.
0
 

Expert Comment

by:DU
ID: 2717399
Thanks a lot guys, that really helped me out.

Also whatboy, thanks for taking the time to give me an example, I really appreciate it!

0
 

Author Comment

by:R_K
ID: 2717410
opps, wrong user name :)

Thanks again!
0
 

Author Comment

by:R_K
ID: 2719804
The finished version of my program.

Thanks to psychorakz, whatboy, and my good friend Benjamin!

--------------------------
Program AccArray;


Uses CRT, dos;

Var   Number: array [1..5] of integer;
      numbers,numtotal,x,prevnumber,max,i,j:integer;
      ans,Yes,No:char;



Begin

  Repeat

        clrscr;

        numtotal:= 0 ;

        Repeat
               write('How many numbers (maximum = 100)? ') ;
               readln(numbers);
               writeln;

                 Begin
                   If numbers > 100 then
                      Begin
                        writeln('ERROR - Please enter a number between 1 and 100') ;
                        writeln;
                      End
                 End;

                 Begin
                   If numbers < 1 then
                     Begin
                       writeln('ERROR - Please enter a number between 1 and 100' );
                       writeln;
                     End
                 End;

                 Begin
                   If numbers < 101 then
                     If numbers > 0  then
                      numtotal:= 5;
                 End;



             Until numtotal = 5  ;



       For x:= 1 to numbers do
       Begin

         write('Enter  # ' , x  ,  ' : '  ) ;
         readln(Number[x]) ;

       End;

       writeln;

             Begin
               for i:= 1 to 100 do
                   for x:= 1 to numbers do
                       if number[x] = i then
                           writeln('Ascending Order - Array  ' , x , ' equals ' , i );
             End;

             writeln;

             Begin
                for i:= 100 downto 1 do
                   for x:= 1 to numbers do
                       if number[x] = i then
                           writeln('Descending Order - Array  ' , x , ' equals ' , i );
             End;




                writeln;
                Write ('Would you like to enter more numbers (Y/N)? ') ;
                readln (ans);
                writeln;


  Until ans = 'N' ;





End.


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to delete from listbox using edit box 6 292
Delphi & Indy 10 Whois over Web Proxy 3 1,193
qrdesign delphi 5 pdf 5 433
Old Old code - Turbo Pascal issue 17 96
Cloud-based technologies and services will continue to grow in popularity in 2017 thanks to the simple, scalable and cost-effective solutions they deliver. Here are three areas where cloud adoption is poised to really take off.
A customer recently asked me about anti-malware and the different deployment options available for his business. Daily news about cyberattacks, zero-day vulnerabilities, and companies that suffered a security breach made him wonder if the endpoint a…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 members asked questions and received personalized solutions in the past 7 days.

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

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now