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

Bubble Sort.

Hi,
   I have  a linked list and I would like to know how we make a bubble sort.

Please help
0
NeoMind
Asked:
NeoMind
1 Solution
 
zfactCommented:
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
 
alb66Commented:
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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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