C# Program to Implement Selection Sort

This is a C# Program to perform a selection sort.

Problem Description

This C# Program Performs a Selection Sort.

Problem Solution

Here Selection sort is an algorithm of sorting an array where it loop from the start of the loop, and check through other elements to find the minimum value. After the end of the first iteration, the minimum value is swapped with the current element. The iteration then continues from the 2nd element and so on.

Program/Source Code

Here is source code of the C# Program to Perform a Selection Sort. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Perform a Selection Sort
 */
using System;
class Program
{
    static void Main(string[] args)
    {
        int array_size = 10;
        int[] array = new int[10] { 100, 50, 20, 40, 10, 60, 80, 70, 90, 30 };
        Console.WriteLine("The Array Before Selection Sort is: ");
        for (int i = 0; i < array_size; i++)
        {
            Console.WriteLine(array[i]);
        }
        int tmp, min_key;
 
        for (int j = 0; j < array_size - 1; j++)
        {
            min_key = j;
 
            for (int k = j + 1; k < array_size; k++)
            {
                if (array[k] < array[min_key])
                {
                    min_key = k;
                }
            }
 
            tmp = array[min_key];
            array[min_key] = array[j];
            array[j] = tmp;
        }
 
        Console.WriteLine("The Array After Selection Sort is: ");
        for (int i = 0; i < 10; i++)
        {
            Console.WriteLine(array[i]);
        }
        Console.ReadLine();
    }
}
Program Explanation

This C# program is used to perform a selection sort. Using for loop we have already defined a coefficient element values of an ‘array[]’ variable. For loop is used to perform selection sort. The Selection sort is an algorithm of sorting an array where it loop from the start of the loop, and check through other elements to find the minimum value.

advertisement
advertisement

After the end of the first iteration, the minimum value is swapped with the current element. Then the iteration will continue from the 2nd element and so on. If condition statement is used to check that the value of element variable is less than the value of a minimum element.

If the condition is true, then execute the statement and assign the element value to the ‘min_key’ variable. Interchange the value of array element using temporary variable ‘t’ and print the values.

Runtime Test Cases
 
The Array Before Selection Sort is :
100
50
20
40
10
60
80
70
90
30
The Array After Selection Sort is :
10
20
30
40
50
60
70
80
90
100

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.