c#,insertion,sort

Yavuz 8/11/2016 0

This Code Shows How To Perform Insertion Sort. Here it takes an element from the list and places it in the correct location in the list. This process is repeated until there are no more unsorted items in the list.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[5] { 83, 12, 3, 34, 60 };
            int i;
            Console.WriteLine("The Array is :");
            for (i = 0; i < 5; i  )
            {
                Console.WriteLine(arr[i]);
            }
            insertsort(arr, 5);
            Console.WriteLine("The Sorted Array is :");
            for (i = 0; i < 5; i  )
                Console.WriteLine(arr[i]); 
            Console.ReadLine();
        }        
        static void insertsort(int[] data, int n)
        {
            int i, j;
            for (i = 1; i < n; i  )
            {
                int item = data[i];
                int ins = 0;
                for (j = i - 1; j >= 0 && ins != 1; )
                {
                    if (item < data[j])
                    {
                        data[j   1] = data[j];
                        j--;
                        data[j   1] = item;
                    }
                    else ins = 1;
                }
            }
        }
    }
} 

/*
Here is the output of the C# Program: The Array is : 83 12 3 34 60 The Sorted Array is : 3 12 34 60 83

*/

Report Bug

Please Login to Report Bug

Reported Bugs

Comments

Please Login to Comment

Comments