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

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - http://www.code-sample.com and http://www.code-sample.xyz
My Books - Buy Books Online at Best Prices

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.