C# Program to Implement Selection Sort

C# Program to Implement Selection Sort

Sorting is a fundamental concept in programming, and Selection Sort is one of the easiest algorithms to understand. Unlike Bubble Sort, which repeatedly swaps adjacent elements, Selection Sort works by selecting the smallest (or largest) element from the unsorted part of the array and moving it to the correct position. This step-by-step approach makes it straightforward and educational for beginners.

Pluralsight Logo
Accelerate your tech career
with hands-on learning.
Whether you're a tech newbie or a total pro,
get the skills and confidence to land your next move.
Start 10-Day Free Trial

Selection Sort is widely used in situations where simplicity and readability are more important than efficiency. It’s particularly helpful for small datasets or teaching purposes, as it clearly demonstrates the principles of algorithmic thinking. Learning Selection Sort also builds a foundation for understanding more complex sorting algorithms like Quick Sort or Merge Sort. Implementing it in C# allows beginners to practice array handling, loops, and conditional logic in a structured way.

Program 1: Selection Sort Using Loops (Ascending Order)

This program demonstrates the standard Selection Sort algorithm with predefined data in ascending order.

using System;

class Program
{

    static void Main()
    {

        int[] arr = { 64, 25, 12, 22, 11 };
        int n = arr.Length;

        for (int i = 0; i < n - 1; i++)
        {

            int minIndex = i;

            for (int j = i + 1; j < n; j++)
            {

                if (arr[j] < arr[minIndex])
                    minIndex = j;

            }

            int temp = arr[minIndex];
            arr[minIndex] = arr[i];
            arr[i] = temp;

        }

        Console.WriteLine("Sorted array in ascending order:");

        foreach (int num in arr)
            Console.Write(num + " ");

        Console.WriteLine();

    }

}

In this program, the outer loop keeps track of the current position to place the smallest element. The inner loop searches for the minimum element in the unsorted part of the array. Beginners can clearly see how the smallest values gradually move to the start of the array.

Program 2: Selection Sort in Descending Order

By changing the comparison condition, Selection Sort can also sort arrays in descending order.

using System;

class Program
{

    static void Main()
    {

        int[] arr = { 64, 25, 12, 22, 11 };
        int n = arr.Length;

        for (int i = 0; i < n - 1; i++)
        {

            int maxIndex = i;

            for (int j = i + 1; j < n; j++)
            {

                if (arr[j] > arr[maxIndex])
                    maxIndex = j;

            }

            int temp = arr[maxIndex];
            arr[maxIndex] = arr[i];
            arr[i] = temp;

        }

        Console.WriteLine("Sorted array in descending order:");

        foreach (int num in arr)
            Console.Write(num + " ");

        Console.WriteLine();

    }

}

This variation demonstrates how a small change in logic can reverse the sorting order. It teaches beginners that Selection Sort is flexible and easy to modify.

Program 3: Optimized Selection Sort (Using Fewer Swaps)

This version minimizes unnecessary swaps by checking whether the selected minimum is already in the correct position.

using System;

class Program
{

    static void Main()
    {

        int[] arr = { 64, 25, 12, 22, 11 };
        int n = arr.Length;

        for (int i = 0; i < n - 1; i++)
        {

            int minIndex = i;

            for (int j = i + 1; j < n; j++)
            {

                if (arr[j] < arr[minIndex])
                    minIndex = j;

            }

            if (minIndex != i)
            {
                int temp = arr[minIndex];
                arr[minIndex] = arr[i];
                arr[i] = temp;
            }

        }

        Console.WriteLine("Optimized Selection Sort (Ascending):");

        foreach (int num in arr)
            Console.Write(num + " ");

        Console.WriteLine();

    }

}

This optimization shows beginners how reducing unnecessary operations can improve performance. Even small improvements like avoiding redundant swaps are an important programming lesson.

Program 4: Selection Sort Using Recursion

Selection Sort can also be implemented recursively, giving beginners a different perspective on solving the problem.

using System;

class Program
{

    static void SelectionSortRecursive(int[] arr, int start, int n)
    {

        if (start >= n - 1)
            return;

        int minIndex = start;

        for (int i = start + 1; i < n; i++)
        {

            if (arr[i] < arr[minIndex])
                minIndex = i;

        }

        int temp = arr[minIndex];
        arr[minIndex] = arr[start];
        arr[start] = temp;

        SelectionSortRecursive(arr, start + 1, n);

    }

    static void Main()
    {

        int[] arr = { 64, 25, 12, 22, 11 };

        SelectionSortRecursive(arr, 0, arr.Length);

        Console.WriteLine("Recursive Selection Sort (Ascending):");

        foreach (int num in arr)
            Console.Write(num + " ");

        Console.WriteLine();

    }

}

The recursive approach helps beginners understand how repeated operations can be modeled as function calls. Each call handles one pass, placing the correct element at the current position.

Frequently Asked Questions (FAQ)

Q1: What is Selection Sort?
Selection Sort is a simple algorithm that repeatedly selects the smallest (or largest) element from the unsorted part of the array and moves it to the correct position.

Q2: When should I use Selection Sort?
It is best for small datasets, educational purposes, and scenarios where simplicity is important. For large datasets, algorithms like Quick Sort or Merge Sort are more efficient.

Q3: How does Selection Sort differ from Bubble Sort?
Selection Sort minimizes swaps by directly placing the minimum element in the correct position, while Bubble Sort swaps adjacent elements repeatedly. Both have O(n²) time complexity.

Q4: Can Selection Sort be used for descending order?
Yes. Simply change the comparison from < to > to sort in descending order.

Q5: Can Selection Sort be implemented recursively?
Yes. Recursive Selection Sort works by finding the minimum element in the current pass and calling itself for the next pass, as shown in the example above.

Conclusion

Selection Sort is an excellent starting point for beginners learning C# and sorting algorithms. It provides a clear understanding of loops, comparisons, and array manipulation. By experimenting with ascending, descending, optimized, and recursive versions, learners can strengthen their problem-solving skills and gain confidence for more advanced sorting techniques. Practicing with predefined data and different approaches ensures that beginners grasp both the logic and implementation details of this classic algorithm.

Scroll to Top