You are viewing a single comment's thread. Return to all comments →
From my HackerRank solutions.
import java.util.Scanner; public class Solution { public static void insertionSortShiftCounter(int[] array) { int shifts = 0; for (int i = 1; i < array.length; i++) { int j = i; int value = array[i]; while (j >= 1 && array[j-1] > value) { array[j] = array[j-1]; j--; shifts++; } array[j] = value; } System.out.println(shifts); } public static void main(String[] args) { Scanner scan = new Scanner(System.in); int s = scan.nextInt(); int[] array = new int[s]; for (int i = 0; i < s; i++) { array[i] = scan.nextInt(); } insertionSortShiftCounter(array); } }
Let me know if you have any questions.
Seems like cookies are disabled on this browser, please enable them to open this website
Running Time of Algorithms
You are viewing a single comment's thread. Return to all comments →
Java solution - passes 100% of test cases
From my HackerRank solutions.
Let me know if you have any questions.