Selection Sort Program in C#

Hi programmers, welcome to new article of c#.net. This post i’ll write programs for selection sort in c# console application. selection sort is a simple sorting algorithm. Selection sort is used to compare array elements. It divided array into two parts, First sorted part at the left side and second unsorted part at the right side. let’s see the codes.

Example
output

Directly Test Below Codes into Editor.

using System;
namespace ConsoleApp1 {
    class Program {       
        static void Main(string[] args)
        {
            int[] arr = new int[55];
            int i, j, n,x,y, temp;

            Console.WriteLine("Enter the Size of Array :");
            n = int.Parse(Console.ReadLine());

            Console.WriteLine("Enter the Array Elements :");
            for(i = 0;i < n; i++)
            {
                arr[i] = int.Parse((Console.ReadLine()));
            }
            for(i = 0; i< (n-1);i++)
            {
                y = arr[i];
                x = i;
                for(j = i+1;j < n;j++)
                {
                    if(y > arr [j])
                    {
                        y = arr[j];
                        x = j;
                    }
                }
                temp = arr[i];
                arr[i] = arr[x];
                arr[x] = temp;
            }
            Console.WriteLine("After Selection Sort :");
            for (i = 0; i < n; i++)
                Console.WriteLine(arr[i] + " ");
        }       
    }
}

Happy Coding…Thanks.

Post Author: adama