Selection sort algorithm

Write a program to demonstrate Selection sort algorithm?

Solution:

For information about selection sore read the article

using System;
using System.Collections.Generic;

namespace Sorting
{
    class SelectionSort
    {
        static void Main()
        {
            int[] arr = { 12, 4, 45, 2, 10 };
            Sort(arr);
            Console.WriteLine($"Sorted:{string.Join(" ", arr)}");
            Console.ReadLine();
        }

        static void Sort(IList<int> arr)
        {
            for (var i = 0; i < arr.Count - 1; i++)
            {
                var index = i;
                for (var j = i + 1; j < arr.Count; j++)
                    if (arr[j] < arr[index])
                        index = j;

                var temp = arr[index];
                arr[index] = arr[i];
                arr[i] = temp;
            }
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *