forked from adityabisoi/ds-algo-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.java
43 lines (37 loc) · 1.42 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
import java.util.Scanner;
/*
In insertion sort we shift each element ahead until we meet element which is less than the key value;
First loop visits all the elements of array starting from index 1
Second loop shifts all the value which are greater than key until
it meets a element which has less value than key value
*/
public class solution {
// Complete the runningTime function below.
public static void runningTime(int[] arr) {
int shifts=0; // no of shifts done will be stored in this variable
int n=arr.length;
//1st loop
for(int i=1; i<n; i++){
int j=i-1;
int temp=arr[i]; //key value
// 2nd loop
while(j>=0&&arr[j]>temp){
shifts++; //do 1 shift and increase the value of shift
arr[j+1]=arr[j];
j--;
}
arr[j+1]=temp; // assigning key value to the index which is greater than j beacuse arr[j] is less
//than key beacuse of which we came out of loop
}
System.out.println(shifts); // now print the value of shift as asked in question
}
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();
}
runningTime(array);
}
}