selection sort algorithm c#.net

selection sort algorithm c#.net

Hello everyone, I am going to share the code sample for the selection sort algorithms using c# with console application as given below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class SelectionSorting
    {
        public static void SelectionSort()
        {

            int[] arrayNumber = new int[100];
            int minNumber;
            bool isNumber = false;
            int sizeNumber;

            Console.WriteLine("Enter the mumbers of element in the array.");

            string sizeStr = Console.ReadLine();
            isNumber = Int32.TryParse(sizeStr, out sizeNumber);

            if (isNumber)
            {
                Console.WriteLine("Enter array values numeric.");
                for (int i = 0; i < sizeNumber; i++)
                {
                    int tempVal;
                    string arrayValue = Console.ReadLine();
                    isNumber = Int32.TryParse(arrayValue, out tempVal);
                    if (isNumber)
                    {
                        arrayNumber[i] = tempVal;
                    }
                    else
                    {
                        Console.WriteLine("Enter the array numeric only values.");
                        break;
                    }
                }

                for (int j = 0; j < sizeNumber - 1; j++)
                {
                    minNumber = j;

                    for (int k = j + 1; k < sizeNumber; k++)
                    {
                        if (arrayNumber[minNumber] > arrayNumber[k])
                        {
                            minNumber = k;
                        }
                    }

                    if (minNumber != j)
                    {
                        int finalNum = arrayNumber[j];
                        arrayNumber[j] = arrayNumber[minNumber];
                        arrayNumber[minNumber] = finalNum;
                    }
                }

                Console.WriteLine("Results of Selection Sort.");
                for (int l = 0; l < sizeNumber; l++)
                {
                    Console.WriteLine(arrayNumber[l]);
                }
            }
            else
            {
                Console.WriteLine("The array numeric only.");
            }
        }

        static void Main()
        {
            SelectionSort();
            Console.Read();
        }
    }

}
ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.