insertion sorting algorithms in c# .net

insertion sorting algorithms in c# .net

Hello everyone, I am going to share the code sample for the insertion sorting 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
{
    /// <summary>
    /// This is insertion sort algorithms.
    /// </summary>
    public class InsertionSort
    {     
        public void displayArray(int[] array, int n)
        {
            Console.WriteLine("\nThe sorted array elements.");
            for (int p = 0; p < n; p++)
            {
                Console.WriteLine(array[p]);
            }
        }

        static void Main(string[] args)
        {
            InsertionSort objIS = new InsertionSort();
            Console.WriteLine("Enter numbers of elements.");
            int number = Convert.ToInt32(Console.ReadLine());
            int[] array = new int[number];
            objIS.AcceptedArrays(array, number);
            objIS.InSort(array, number);
            objIS.displayArray(array, number);
            Console.ReadLine();
        }

        public void AcceptedArrays(int[] array, int n)
        {
            Console.WriteLine("\nEnter the numbers:");
            for (int l = 0; l < n; l++)
            {
                array[l] = Convert.ToInt32(Console.ReadLine());
            }
        }

        public void InSort(int[] array, int n)
        {
            for (int val = 1; val <= n - 1; val++)
            {
                for (int m = 1; m <= n - 1; m++)
                {
                    int tempVal = array[m];
                    for (int j = m - 1; j >= 0 && array[j] > tempVal; j--)
                    {
                        array[j + 1] = array[j];
                        array[j] = tempVal;
                    }
                }
            }
        }
    }
}



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

insertion sorting algorithms in c# .net insertion sorting algorithms in c# .net Reviewed by Anil Singh on 2:08 AM Rating: (5)
www.code-sample.com/. Powered by Blogger.
^