?
Solved

Bubble Sort.

Posted on 2003-02-28
5
Medium Priority
?
289 Views
Last Modified: 2013-11-20
Hi,
   I have  a linked list and I would like to know how we make a bubble sort.

Please help
0
Comment
Question by:NeoMind
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 

Accepted Solution

by:
zfact earned 200 total points
ID: 8041249
Hi NeoMind,
Check the link below, this will give you full info.. about the sort. This link is good. check it out.

http://www.funducode.com/freec/datastructure/datastruct6/datastruct6.htm

All The Best,
Zfact
0
 
LVL 19

Expert Comment

by:alb66
ID: 8041318
I wrote this class CSort to make any collection class sortable:


class CSort
{
public:
     CSort() {}
     ~CSort(){}
   
   void      BubbleSort( int nElem );

   virtual BOOL      Is1LowerThan2( int n1, int n2 ) = 0;
   virtual void      Exchange( int i, int j ) = 0;
}

void CSort::BubbleSort( int nN )
{
   for (int nI=0; nI<nN-1; nI++)
   {
      BOOL bOneAtLeast = FALSE;
      for (int nK=0; nK<nN-1-nI; nK++)
      {
         if ( Is1LowerThan2( nK+1, nK ))
         {
            Exchange( nK, nK+1 );
            bOneAtLeast = TRUE;
         }
      }
     
      if( !bOneAtLeast )
         return;
   }
}

You must derive your List class from CSort and then call BubbleSort();

This is an example of a sortable array:

class CSortWordArray :     public CWordArray,
                                        public CSort
{
public:
   virtual BOOL      Is1LowerThan2( int n1, int n2 )               { return GetAt( n1 ) < GetAt( n2 ); }
   virtual void      Exchange( int i, int j )     { WORD w = GetAt(i); SetAt( i, GetAt(j) ); SetAt( j, w ); }
};

...
CSortWordArray array;
array.Add( 10 );
array.Add( 2 );
array.Add( 3 );
array.BubbleSort( array.GetSize() );
...

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

770 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