Insertion Sort – Part 1 in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct]

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank, Algorithm Solutions of Problem Solving Section in Java. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for Insertion Sort – Part 1 in Java-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

Introduction To Algorithm

The word Algorithm means “a process or set of rules to be followed in calculations or other problem-solving operations”. Therefore Algorithm refers to a set of rules/instructions that step-by-step define how a work is to be executed upon in order to get the expected results. 

Advantages of Algorithms:

  • It is easy to understand.
  • Algorithm is a step-wise representation of a solution to a given problem.
  • In Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the programmer to convert it into an actual program.

Link for the ProblemInsertion Sort – Part 1 – Hacker Rank Solution

Insertion Sort - Part 1 – Hacker Rank Solution


One common task for computers is to sort data. For example, people might want to see all their files on a computer sorted by size. Since sorting is a simple problem with many different possible solutions, it is often used to introduce the study of algorithms.

Insertion Sort
These challenges will cover Insertion Sort, a simple and intuitive sorting algorithm. We will first start with a nearly sorted list.

Insert element into sorted list
Given a sorted list with an unsorted number  in the rightmost cell, can you write some simple code to insert  into the array so that it remains sorted?

Since this is a learning exercise, it won’t be the most efficient way of performing the insertion. It will instead demonstrate the brute-force method in detail.

Assume you are given the array  indexed . Store the value of . Now test lower index values successively from  to  until you reach a value that is lower than , at  in this case. Each time your test fails, copy the value at the lower index to the current index and print your array. When the next lower indexed value is smaller than , insert the stored value at the current index and print the entire array.


Start at the rightmost index. Store the value of . Compare this to each element to the left until a smaller value is reached. Here are the results as described:

1 2 4 5 5
1 2 4 4 5
1 2 3 4 5

Function Description

Complete the insertionSort1 function in the editor below.

insertionSort1 has the following parameter(s):

  • n: an integer, the size of 
  • arr: an array of integers to sort


  • None: Print the interim and final arrays, each on a new line. No return value is expected.

Input Format

The first line contains the integer , the size of the array .
The next line contains  space-separated integers .


Output Format

Print the array as a row of space-separated integers each time there is a shift or insertion.

Sample Input

2 4 6 8 3

Sample Output

2 4 6 8 8 
2 4 6 6 8 
2 4 4 6 8 
2 3 4 6 8 


 is removed from the end of the array.
In the st line , so  is shifted one cell to the right.
In the nd line , so  is shifted one cell to the right.
In the rd line , so  is shifted one cell to the right.
In the th line , so  is placed at position .

Next Challenge

In the next Challenge, we will complete the insertion sort.

Insertion Sort - Part 1 – Hacker Rank Solution
import java.util.Scanner;

 * @author Kanahaiya Gupta
public class InsertionSortPart1 {
	public static void insertIntoSorted(int[] ar) {
		int length = ar.length;
		int elementNeedToBeInserted = ar[length - 1];
		for (int i = length - 2; i >= 0; i--) {
			if (ar[i] > elementNeedToBeInserted) {
				ar[i + 1] = ar[i];

			} else {
				ar[i + 1] = elementNeedToBeInserted;
			if ((i == 0) && (ar[i] > elementNeedToBeInserted)) {
				ar[i] = elementNeedToBeInserted;


	/* Tail starts here */
	public static void main(String[] args) {
		Scanner in = new Scanner(;
		int s = in.nextInt();
		int[] ar = new int[s];
		for (int i = 0; i < s; i++) {
			ar[i] = in.nextInt();

	private static void printArray(int[] ar) {
		for (int n : ar) {
			System.out.print(n + " ");

Leave a Comment

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.