Skip to content

Instantly share code, notes, and snippets.

@abdulateef
Created November 4, 2016 17:46
Show Gist options
  • Save abdulateef/9cfc78229f565130e70e6bc9d9589c53 to your computer and use it in GitHub Desktop.
Save abdulateef/9cfc78229f565130e70e6bc9d9589c53 to your computer and use it in GitHub Desktop.
Hanker Rank Insertion Sort - Part 2 Solution in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Algorithms
{
class InsertionSort2
{
public static void InsertionSorted(int[] list1)
{
int temp;
for(int i = 0; i < list1.Length; i++)
{
int j = i + 1;
while(j>0)
{
if(list1[j] < list1[j-1] )
{
temp = list1[j];
list1[j] = list1[j-1];
list1[j-1] = temp;
j--;
}
else
{
break;
}
}
for(int m = 0; m < list1.Length; m++)
{
Console.Write(list1[m] + " ");
}
Console.WriteLine(" ");
}
}
public static void ImplementInsertionSort()
{
// Console.WriteLine("Enter the Lenght of the array");
int N = Convert.ToInt32(Console.ReadLine());
int[] List = new int[N];
//Console.WriteLine("Enter the values of the array");
for(int i = 0; i < List.Length; i++)
{
List[i] = Convert.ToInt32(Console.ReadLine());
}
// printint the result
InsertionSorted(List);
// Console.WriteLine("Sorted List");
for(int m = 0; m < List.Length; m++)
{
Console.Write(List[m] + ",");
}
Console.WriteLine(" ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment