Thursday, July 29, 2010

ASP.Net Bubble Sort

The bubble sort is the slowest sort in use.


The bubble sort works by comparing each item in the list with the item next to it, and swapping them if required. The algorithm repeats this process until it makes a pass all the way through the list without swapping any items. This causes larger values to "bubble" to the end of the list while smaller values "sink" towards the beginning of the list.


The bubble sort is considered to be the most inefficient sorting algorithm.






Bubble Sort Routine Code


// array of integers 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;
}
}
}
}

No comments:

Post a Comment