How To Make Bubble Sort Algorithm In C# – Made Easy

Bubble sort is one of the simplest sorting algorithms and this guide shows how to implement it in C# programming language.


Andraz Krzisnik
How To Make Bubble Sort Algorithm In C#...

Bubble sort is another simpler approach to sorting objects in single dimensional arrays. However, unlike selection sort and insertion sort, it doesn’t need to partition array into sorted and unsorted parts.

In essence, it goes through the array comparing adjacent objects for each object in the array. If these neighboring values aren’t in correct order, we swap them. In other words, we sort values that are included inside a small bubble of 2 objects, hence the name, bubble sort.

However, in order to sort the entire array, we need to iterate through the array multiple times. Therefore, it’s very inefficient when we try to use it on arrays that hold larger amount of objects.

Bubble sort in C#

To get a better understanding how it actually works, let’s just dive into the implementation.

public static class BubbleSort
    {
        public static void Sort<T>(T[] array) where T : IComparable
        {
            for (int i = 0; i < array.Length; i++)
            {
                for (int j = 0; j < array.Length - 1; j++)
                {
                    if (array[j].CompareTo(array[j+1]) > 0)
                    {
                        Swap(array, j, j + 1);
                    }
                }
            }
        }

        private static void Swap<T>(T[] array, int first, int second)
        {
            T temp = array[first];
            array[first] = array[second];
            array[second] = temp;
        }
    }

First of all, as you can see, we created a separate public static class, which holds 2 methods inside. However, there is only one of the two, that’s public, which is the Sort method. The other method inside the class is for swapping values inside the array and since this is its only purpose, we made it private. In other words, we can only call it inside the class where we declared it.

What we also see in the code above is that the Sort method accepts data type T, which is a generic data type. In other words, we can use this method for various different data types.

However, we also use the “where” clause, which will act as a filter for our method. The condition each array has to meet is that its objects data type is compatible with CompareTo method of the IComparable interface.

Next thing we need to focus on is that we’re using nested for loops. Furthermore, while the implementation might seem simple enough, this is the main problem when it comes to efficiency.

Conclusion

I hope this tutorial helped you gain a better understanding on how bubble sort works.

If you want to see it in action, you can also download the demo project and try it out yourself.

Related Articles

Order-Statistic Filters

How To Make Alpha Trimmed Mean Filter For Images

Alpha trimmed mean filter is a combination of mean filters and order-statistic filters. This guide shows how to use them with C#.

Posted on by Andraz Krzisnik
Grayscale Morphology

How To Make Morphological Gradient Work With C#

Morphological gradient is a grayscale morphological operation in image processing, which emphasized boundaries and supresses homogenous areas

Posted on by Andraz Krzisnik