Problem Statement
A description of the problem can be found on Hackerrank.
Solution
Assign to auxiliary variable the last element of the array. Iterate from last element to 0th. If an actual element is lower than auxiliary, assign auxiliary to actual index, else switch actual element with last actual.
I created solution in:
All solutions are also available on my GitHub.
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
import java.util.*; public class InsertionSort1 { public static void insertIntoSorted(int[] ar) { // Fill up this function int num = ar[ar.length - 1]; boolean placed = false; for(int j = ar.length - 2; j >= 0; j--) { if(ar[j] > num) { ar[j + 1] = ar[j]; printArray(ar); } else { ar[j + 1] = num; printArray(ar); placed = true; break; } } if(!placed) { ar[0] = num; printArray(ar); } } /* Tail starts here */ public static void main(String[] args) { Scanner in = new Scanner(System.in); int s = in.nextInt(); int[] ar = new int[s]; for(int i=0;i<s;i++){ ar[i]=in.nextInt(); } insertIntoSorted(ar); in.close(); } private static void printArray(int[] ar) { for(int n: ar){ System.out.print(n+" "); } System.out.println(""); } } |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
'use strict'; let insertIntoSorted = arr => { let num = arr[arr.length - 1]; let placed = false; for(let j = arr.length - 2; j >= 0; j--) { if(arr[j] > num) { arr[j + 1] = arr[j]; printArray(arr); } else { arr[j + 1] = num; printArray(arr); placed = true; break; } } if(!placed) { arr[0] = num; printArray(arr); } }; let printArray = arr => console.log(arr.join(" ")); const processData = input => { let lines = input.split('\n'); let arr = lines[1].split(' ').map(i => parseInt(i)); insertIntoSorted(arr) }; process.stdin.resume(); process.stdin.setEncoding("ascii"); var _input = ""; process.stdin.on("data", input => _input += input); process.stdin.on("end", () => processData(_input)); |
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import scala.io.Source import scala.util.control.Breaks._ object InsertionSort1 extends App { val lines = Source.stdin.getLines.toList val arr = lines(1).split(" ").map(_.toInt) insertIntoSorted(arr) def insertIntoSorted(arr: Array[Int]): Unit = { val insertionValue = arr(arr.length - 1) var placed = false breakable { Range(arr.length - 2, -1, -1).foreach(i => { if (arr(i) > insertionValue) { arr(i + 1) = arr(i) printArray(arr) } else { arr(i + 1) = insertionValue printArray(arr) placed = true break } }) } if(!placed) { arr(0) = insertionValue printArray(arr) } } def printArray(arr: Array[Int]): Unit = { println(arr.mkString(" ")) } } |
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
def print_array(arr) puts arr.join(" ") end def insertionSort(ar) num = ar[ar.size - 1] placed = false (ar.size - 2).downto(0) do |j| if(ar[j] > num) ar[j + 1] = ar[j] print_array(ar) else ar[j + 1] = num print_array(ar) placed = true break end end if(!placed) ar[0] = num print_array(ar) end end count = gets.to_i ar = gets.strip.split.map {|i| i.to_i} insertionSort(ar) |