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.
Filter by Category
Bubble sort is one of the simplest sorting algorithms and this guide shows how to implement it in C# programming language.
Insertion sort is one of the simplest sorting algorithms for sorting single dimensional arrays of various different data types.
Selection sort algorithm is one of the simplest sorting algorithms out there. You can use it to sort different data type arrays.
C# arrays are data structures for storing multiple items of the same type. This guide shows you how to use different varieties of arrays.
SLIC superpixel segmentation is a modern operation for reducing irrelevant detail for shortening computational time in further processing.
K means clustering is a optimization method of partitioning an image by measuring Euclidean distances between pixels and cluster means.
Region splitting and merging is a texture segmentation operation, where we use descriptors such as local mean intensity and standard deviation
Region growing segmentation is a process, with which we can extract regions from image based on the properties of pixels inside them.
Adaptive thresholding operation computes thresholds for each pixel locally and therefore segments images more accurately.
Multilevel thresholding is an extension of Otsu's method of thresholding, which basically works for an arbitrary number of thresholds.
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.
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.
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.