How to Selection Sort Algorithm in C# Made Easy

Selection sort algorithm is one of the simplest sorting algorithms out there. You can use it to sort different data type arrays.


Andraz Krzisnik
How to Selection Sort Algorithm in C# Made...

Selection sort is just one of many operations out there that we can use on arrays. In general, what sorting algorithms are good for, is arranging items in a ascending or descending order. However, different sorting algorithms present us with different efficiencies.

I will dedicate this tutorial only to selection sort algorithm using C# programming, since it’s one of the simplest sorting algorithms out there.

In essence, this algorithm divides the array into 2 parts, sorted and unsorted. Furthermore, through each iteration, it takes a look at the items left in the unsorted part.

In order to sort the items, it picks either the lowest or highest item from unsorted section and exchanges it with the first item in the sorted array. Whether it picks the lowest or highest value with each iteration depends on the order we want to sort.

It’s a pretty simple concept, but you need to keep in mind that it doesn’t come without a drawback. In order to sort the entire array, it takes as many iterations through it as there are items inside. Therefore, it makes it very inefficient when we’re working with arrays that hold a large number of items.

Selection Sort Algorithm Implementation in C#

In case that explanation above isn’t quite clear enough, I’m going to implement it by first creating static class SelectionSort.

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

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

As you can see from the code above, we created a static class, which holds 2 methods for the whole operation.

First of all, the Sort method is a generic static method, which takes 1 parameter, the array we want to sort. Secondly, this method can only sort data types that IComparable interface accepts. In other words, we can use this same method on different data type arrays.

The second method in the class is meant to, as it’s name suggests, swap values within array when we’re sorting it. Therefore, we use it within the first method.

Conclusion

Thank you for visiting my blog and I hope you found this post about selection sort useful.

You can also download the demo project and try it out yourself.

Related Articles

Thresholding

How To Make Adaptive Thresholding Algorithm With C#

Adaptive thresholding operation computes thresholds for each pixel locally and therefore segments images more accurately.

Posted on by Andraz Krzisnik
Image Noise

How To Make Exponential Noise On Image – C# Guide

Exponential noise is one of the noise models we can use to simulate corruption of data. This guide show how to use it on images.

Posted on by Andraz Krzisnik