Bubble Sort

Bubble sort (a.k.a Sinking Sort and Comparison Sort) is a sorting algorithm that works by repeatedly swapping and adjacent elements if they are in wrong order.



									public static void BubbleSort(ref int[] data)
{
	for (int i = 1; i < data.Length; ++i)
	{
		for (int j = 0; j < data.Length - i; ++j)
		{
			if (data[j] > data[j + 1])
			{
				data[j] ^= data[j + 1];
				data[j + 1] ^= data[j];
				data[j] ^= data[j + 1];
			}
		}
	}
}
								


Example

									int[] data = new int[] { -1, 25, -58964, 8547, -119, 0, 78596 };
BubbleSort(ref data);
								


Output

									-58964
-119
-1
0
25
8547
78596