Hacker Rank Insertion Sort - Part 1
public class Solution {
// Complete the insertionSort1 function below.
static void insertionSort1(int n, int[] arr)
{
var i = arr.Length - 1;
var currElement = arr[i];
var j = i - 1;
for (; j >= 0; j--)
{
if (currElement < arr[j])
{
arr[j + 1] = arr[j];
Console.WriteLine(String.Join(" ", arr));
}
else
break;
}
arr[j + 1] = currElement;
Console.WriteLine(String.Join(" ", arr));
}
public static void Main(string[] args) {
int n = Convert.ToInt32(Console.ReadLine());
int[] arr = Array.ConvertAll(Console.ReadLine().Split(' '), arrTemp => Convert.ToInt32(arrTemp))
;
insertionSort1(n, arr);
}
}
Output :
1 3 2
1 3 3
1 2 3