My Thoughts & Views

C# and Sorting Algorithms : Bubble Sort

Posted on: November 26, 2007

— 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 ;
}
}
}
}

Advertisements

2 Responses to "C# and Sorting Algorithms : Bubble Sort"

GREAT post!!!
10x

Thanks to you too

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: