0% found this document useful (0 votes)
2 views

Insertion Sort Algorithm

algorithm

Uploaded by

Min Shosho
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Insertion Sort Algorithm

algorithm

Uploaded by

Min Shosho
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Insertion Sort Algorithm

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[10] { 23, 9, 85, 12, 99, 34, 60, 15, 100, 1 };
int i, j, val, flag;
Console.WriteLine("Insertion Sort");
Console.Write("Initial array is: ");
for (i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
for (i = 1; i < arr.Length; i++)
{
val = arr[i];
flag = 0;
for (j = i - 1; j >= 0 && flag != 1;)
{
if (val < arr[j])
{
arr[j + 1] = arr[j];
j--;
arr[j + 1] = val;
}
else flag = 1;
}
}
Console.Write("\nSorted Array is: ");
for (i = 0; i < arr.Length; i++)
Console.Write(arr[i] + " ");
Console.ReadKey();
}
}
}

You might also like