Implementation in C#
suggest changeBubble sort is also known as Sinking Sort. It is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order.
Bubble sort example
The following example implements bubble sort in C#:
public static class BubbleSort
{
public static void Sort(int[] values)
{
for (int end = values.Length - 1; end > 0; end--)
{
bool swapped = false;
for (int index = 0; index < end; index++)
{
if (values[index] <= values[index + 1]) continue;
(values[index], values[index + 1]) = (values[index + 1], values[index]);
swapped = true;
}
if (!swapped) return;
}
}
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents