Bubble sort walks through the array comparing neighbors and swaps every pair that is in the wrong order. After the first pass the largest element has “bubbled up” to the end; after the second, the two largest are in place, and so on. It is the simplest sorting algorithm to write and the slowest of the common ones, so it is mostly used for teaching.
The version below is generic, works on arrays and Span<T>, and stops as soon as a pass makes no swaps.
Usage
int[] numbers = { 1, 5, 4, 11, 20, 8, 2, 98, 90, 16 };
BubbleSort.Sort(numbers);
Console.WriteLine(string.Join(", ", numbers));
// 1, 2, 4, 5, 8, 11, 16, 20, 90, 98
It sorts anything that implements IComparable<T>: string[], DateTime[], decimal[] or your own types.
Implementation
public static class BubbleSort
{
public static void Sort<T>(T[] items) where T : IComparable<T>
=> Sort(items.AsSpan());
public static void Sort<T>(Span<T> items) where T : IComparable<T>
{
for (int end = items.Length - 1; end > 0; end--)
{
bool swapped = false;
// After each pass, items[end] holds its final value.
for (int j = 0; j < end; j++)
{
if (items[j].CompareTo(items[j + 1]) > 0)
{
(items[j], items[j + 1]) = (items[j + 1], items[j]);
swapped = true;
}
}
// No swaps means everything is already in order.
if (!swapped) break;
}
}
}
How it works
The outer loop shrinks the unsorted range from the right. Each inner pass compares items[j] with items[j + 1] and swaps them if the left one is greater, which carries the largest remaining value to position end. That element is never touched again, so the next pass can stop one position earlier.
The swapped flag is the one optimization worth having. If a full pass finds nothing to swap, the array is sorted and there is no reason to keep going. Without it, bubble sort does the full n²/2 comparisons even on an array that was sorted to begin with.
The comparison uses > 0, so equal neighbors are never swapped. That keeps the sort stable.
The swap uses tuple syntax, which C# 7 and later compile to a plain three-step exchange with a temporary. The older version of this post had a separate Swap(ref int, ref int) helper; it did the same thing but only for int. It also bubbled the smallest value toward the front instead of the largest toward the back. Both directions are valid.
Complexity
| Case | Time | Extra space |
|---|---|---|
| Best (already sorted) | O(n) with early exit | O(1) |
| Average | O(n²) | O(1) |
| Worst (reverse order) | O(n²) | O(1) |
Bubble sort is stable and sorts in place.
Should you use bubble sort?
No, apart from learning and interviews. It has the same O(n²) complexity as insertion sort but does many more writes, because it moves an element one swap at a time where insertion sort shifts a run of elements and writes the moving element once. Insertion sort keeps the O(n) best case and stability, so if you need a simple quadratic sort for tiny or nearly sorted inputs, use insertion sort instead.
For real code, call Array.Sort, List<T>.Sort or span.Sort(). They use introsort and are far faster on anything beyond a handful of elements.