Monday, November 26, 2007

C# and Sorting Algorithms : Bubble Sort

-- It is slowest sorting algorithm in use
-- It is considered as Most In efficient Sorting Algorithm.

How it works?

The basic idea is to compare two neighboring objects, and to swap them if they are in the wrong order. This process is repeated until it completly sorts the list. This causes larger values to "bubble" to the end of the list while smaller values "sink" towards the begining of the list.



C# Code :

// integers array to hold values
private int[] a = new int[100];

// number of elements in array
private int x;

// Bubble Sort Algorithm
public void sortArray()
{
int i ;
int j ;
int temp ;

for( i = (x - 1); i >= 0; i-- )
{
for( j = 1; j <= i; j++ )
{
if
( a[j-1] > a[j] )
{
temp = a[j-1] ;
a[j-1] = a[j] ;
a[j] = temp ;
}
}
}
}

3 comments:

Anonymous said...

GREAT post!!!
10x

Kiran Kumar said...

Thanks to you too

Anonymous said...

thanks for sharing this site. you can download lots of ebook from here


http://feboook.blogspot.com