Selection Sort Algorithm and Program in C#

Selection Sorting is the procedure to arrange data in a specific order either in ascending or descending. In programming also we need to sort the data (array, list, etc.) in so many cases. Every programming language has some built-in methods to sort the data but the beauty of programming comes when you implement the logic by yourself.

There are multiple ways to sort the data and selection sort is also one of them. You can also sort the data using Bubble sort.

How Selection Sort Works

  • We will start by selecting the smallest element from the array and swap it with the first element of the array.
  • Now, the smallest element is at the target place but the remaining array i.e. from index 1 to last will still be unsorted.
  • Again we will apply the same procedure to select the smallest element from the remaining array ( from index 1 to last) and will swap it with index 1 and so on.
  • After array.Length-1 iteration we will have a sorted array.

Selection Sort Pictorial Example

Let’s assume we have the following array and we need to sort it using the Selection Sort technique.

int[] arr = { 12, 10, 8, 1, 25 };

Let’s see the iterations.

Iteration 1

  • We will start the sorting by selecting the smallest element from the array.
  • In our case, the smallest element is at the index 3.
  • Now we will swap this index 3 element with the index 0 element.
Selection sort algorithm in C# (Iteration 1)
Selection sort algorithm in C# (Iteration 1)

Here is how our looks after the first iteration.

int[] arr = { 1, 10, 8, 12, 25 };

Focus: After iteration 1 the smallest element is at its target place. Meaning in the second iteration we will need to work from index 1 to the last.

Let’s see how it goes.

Iteration 2

Selection sort algorithm in C# (Iteration 2)
Selection sort algorithm in C# (Iteration 2)

Things are going well. Now after iteration 2, we have the first two elements at their target place.

Time to proceed with iteration 3.

Iteration 3

Selection sort algorithm in C# (Iteration 3)
Selection sort algorithm in C# (Iteration 3)

Ok, So this one was a bit special case because the smallest element is already at its target place. But still, we have to execute our logic and need to handle this type of situation as well.

Iteration 4

Selection sort algorithm in C# (Iteration 4)
Selection sort algorithm in C# (Iteration 4)

Did you see the output of the fourth iteration? It’s done! The array is sorted in ascending order.

Write a program for selection sort in C#

Before writing the program, let’s see the requirement.

Question: How many loops are required?
Ans: 2
One loop is used for the iterations. In our case, there are 4 iterations while the length of the array is 5. Meaning the outer loop will run from 0 to array.Length-1
The second inner loop will be used to find out the smallest element and for the swapping concept.

Time to write the code.

public static int[] SelectionSort(int[] array)
{
    for (int i = 0; i < array.Length; i++)
    {
        var index = i;

        for (int j = i; j < array.Length; j++)
        {
            if (array[j] < array[index])
            {
                index = j;
            }
        }

        (array[i], array[index]) = (array[index], array[i]);
    }

    return array;
}

The above code will sort the input array in ascending order using the selection sort in C#.

Do you want to practice it more?

Programming is all about practice. I want you to write a program to sort the given array in descending order in your favorite programming language.

Once you are done will this program don’t forget to write it in the comment section below.

Thank you for reading. I hope it was helpful.