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

# Bubble Sort.

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

0
NeoMind
1 Solution

Commented:
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

Commented:
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;