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 Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

selection sort algorithm c#.net selection sort algorithm c#.net Reviewed by Anil Singh on 2:34 AM Rating: (5)
www.code-sample.com/. Powered by Blogger.
^