# C# and Sorting Algorithms : Bubble Sort

Posted November 26, 2007

on:— 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 ;

}

}

}

}

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

Thanks to you too

1 | Anonymous

December 27, 2007 at 4:42 pm

GREAT post!!!

10x