Implementation in C#

suggest change

Bubble 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;
        }
    }
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents