Showing posts with label 11. Show all posts
Showing posts with label 11. Show all posts

Wednesday, October 15, 2014

Implement Insertion Sort Algorithms and test it with input of array of length 15 {9,13,15,11,14,1,8, 6,2,7,4,5,10,3,12}.

Implement Insertion Sort Algorithms and test it with input of array of length 15 {9,13,15,11,14,1,8, 6,2,7,4,5,10,3,12}.

CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Labtask4_1
{
    public class Insertion_Sort
    {
        int[] A = { 9, 13, 15, 11, 14, 1, 8, 6, 2, 7, 4, 5, 10, 3, 12 };
        public void Show()
        {
            for (int i = 0; i < 15; i++)
            {
                Console.Write("{0},", A[i]);
            }
            Console.WriteLine();
        }
        public void Sort()
        {
            for (int i = 0; i < 15; i++)
            {
                int num = A[i];
                int j = i - 1;
                while (j >= 0 && A[j] > num)
                {
                    A[j + 1] = A[j];
                    j--;
                }
                A[j + 1] = num;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Insertion_Sort A = new Insertion_Sort();
            A.Show();
            Console.WriteLine("*****AFTER SORTING THE ARRAY*****\n");
            A.Sort();
            A.Show();
            Console.ReadLine();
        }
    }

}