-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Solution.java
31 lines (28 loc) · 913 Bytes
/
Solution.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
// github.com/RodneyShag
import java.util.Scanner;
public class Solution {
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();
}
scan.close();
insertionSortShifts(array);
}
public static void insertionSortShifts(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) { // find where to insert "value"
array[j] = array[j-1]; // shift elements (1 by 1) to make room for inserting
j--;
shifts++;
}
array[j] = value;
}
System.out.println(shifts);
}
}