Created
November 4, 2016 17:46
-
-
Save abdulateef/9cfc78229f565130e70e6bc9d9589c53 to your computer and use it in GitHub Desktop.
Hanker Rank Insertion Sort - Part 2 Solution in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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