In Insertion Sort Part 1, you inserted one element into an array at its correct sorted position. Using the same approach repeatedly, can you sort an entire array?
Guideline: You already can place an element into a sorted array. How can you use that code to build up a sorted array, one element at a time? Note that in the first step, when you consider an array with just the first element, it is already sorted since there’s nothing to compare it to.
In this challenge, print the array after each iteration of the insertion sort, i.e., whenever the next element has been inserted at its correct position. Since the array composed of just the first element is already sorted, begin printing after placing the second element.
Example.
n = 7
arr = [3,4,7,5,6,2,1]
Working from left to right, we get the following output:
3 4 7 5 6 2 1 3 4 7 5 6 2 1 3 4 5 7 6 2 1 3 4 5 6 7 2 1 2 3 4 5 6 7 1 1 2 3 4 5 6 7
Function Description
Complete the insertionSort2 function in the editor below.
insertionSort2 has the following parameter(s):
- int n: the length of arr
- int arr[n]: an array of integers
Prints
At each iteration, print the array as space-separated integers on its own line.
Input Format
The first line contains an integer,n , the size of arr.
The next line contains n space-separated integers arr[i].
Constraints
1<=n<=1000
1000<=arr[i]<=10000, 0<=i<n
Output Format
Print the entire array on a new line at every iteration.
Sample Input
STDIN Function ----- -------- 6 n = 6 1 4 3 5 6 2 arr = [1, 4, 3, 5, 6, 2]
Sample Output
1 4 3 5 6 2 1 3 4 5 6 2 1 3 4 5 6 2 1 3 4 5 6 2 1 2 3 4 5 6
Insertion Sort – Part 2 HackerRank Solution in C
//program for inserting any unsorted number at right position using INERTION SORT.. #include<stdio.h> int main() { int size,num,i,j,t; int *x; scanf("%d",&size); x=(int *)malloc(sizeof(int)*size); for(i=0;i<size;i++) scanf("%d",&x[i]); //insertion sort.. for(i=1;i<size;i++) { num=x[i]; for(j=i-1;j>=0&&num<x[j];j--) x[j+1]=x[j]; x[j+1]=num; for(t=0;t<size;t++) printf("%d ",x[t]); printf("\n"); } }
Insertion Sort – Part 2 HackerRank Solution in C++
#include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <queue> #include <stack> #include <bitset> #include <cstdio> #include <vector> #include <cstdlib> #include <numeric> #include <sstream> #include <iostream> #include <algorithm> using namespace std; /* Head ends here */ void insertionSort(vector <int> ar) { int n = ar.size(); for(int i=1;i<n;i++){ int curr = ar[i]; for(int j=i-1;j>=0;j--){ if(ar[j]>curr){ ar[j+1]=ar[j]; if(j==0) ar[j]=curr; } else{ ar[j+1]=curr; j=-1; } } for(int t=0;t<n;t++) cout<<ar[t]; cout<<endl; } } /* Tail starts here */ int main() { vector <int> _ar; int _ar_size; cin >> _ar_size; for(int _ar_i=0; _ar_i<_ar_size; _ar_i++) { int _ar_tmp; cin >> _ar_tmp; _ar.push_back(_ar_tmp); } insertionSort(_ar); return 0; }
Insertion Sort – Part 2 HackerRank Solution in Java
/* Head ends here */ import java.util.*; public class Solution { public static void insertIntoSortedArray(int[] ar, int i) { int n= ar[i]; i--; while(i>=0 && ar[i]>n){ ar[i+1]=ar[i]; //shift right i--; } ar[i+1]= n; printArray(ar); } static void insertionSort(int[] ar) { for(int i=1;i<ar.length;i++){ insertIntoSortedArray(ar, i); } } /* Tail starts here */ static void printArray(int[] ar) { for(int n: ar){ System.out.print(n+" "); } System.out.println(""); } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] ar = new int[n]; for(int i=0;i<n;i++){ ar[i]=in.nextInt(); } insertionSort(ar); } }
Insertion Sort – Part 2 HackerRank Solution in Python
#!/bin/python # Head ends here def printArray(a): x="" for number in a: x += str(number) + " " print x def insertionSort(a): for i in xrange(0, len(a)): j = i; while (j > 0 and a[j] < a[j-1]): temp = a[j]; a[j] = a[j-1]; a[j-1] = temp; j -= 1; if (i != 0): printArray(a) # Tail starts here m = input() ar = [int(i) for i in raw_input().strip().split()] insertionSort(ar)
Insertion Sort – Part 2 HackerRank Solution in C#
using System; using System.Collections.Generic; using System.IO; class Solution { /* Head ends here */ static void insertionSort(int[] ar) { int n = ar.Length; for (int i = 1; i < n; i++) { for (int j = i-1; j >=0 && ar[j]>(ar[j+1]); j--) { swap(ref ar[j], ref ar[j + 1]); } PrintArray(ar); } } static void swap(ref int a, ref int b) { int temp = a; a = b; b = temp; } static void PrintArray(int[] ar) { int length = ar.Length; for (int i = 0; i < length; i++) { Console.Write(ar[i] + " "); } Console.WriteLine(); } /* Tail starts here */ static void Main(String[] args) { int _ar_size; _ar_size = Convert.ToInt32(Console.ReadLine()); int[] _ar = new int[_ar_size]; string input = Console.ReadLine(); string[] inputs = input.Split(' '); for (int _ar_i = 0; _ar_i < _ar_size; _ar_i++) { _ar[_ar_i] = Convert.ToInt32(inputs[_ar_i]); } insertionSort(_ar); } }
Attempt Insertion Sort – Part 2 HackerRank Challenge
Link – https://www.hackerrank.com/challenges/insertionsort2/
Next HackerRank Challenge Solution
Link – https://exploringbits.com/correctness-and-the-loop-invariant-hackerrank-solution/
Aayush Kumar Gupta is the founder and creator of ExploringBits, a website dedicated to providing useful content for people passionate about Engineering and Technology. Aayush has completed his Bachelor of Technology (Computer Science & Engineering) from 2018-2022. From July 2022, Aayush has been working as a full-time Devops Engineer.