forked from rajatgoyal715/Hackerrank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunningTimeOfAlgorithms.java
More file actions
41 lines (36 loc) · 904 Bytes
/
RunningTimeOfAlgorithms.java
File metadata and controls
41 lines (36 loc) · 904 Bytes
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
package Sorting;
import java.util.*;
import java.io.*;
/*
* @author -- rajatgoyal715
*/
public class RunningTimeOfAlgorithms {
public static void insertionSort(int[] A){
int N = 0;
for(int i = 1; i < A.length; i++){
int value = A[i];
int j = i - 1;
while(j >= 0 && A[j] > value){
A[j + 1] = A[j];
j = j - 1;
N++;
}
A[j + 1] = value;
}
System.out.println(N);
}
public static void printArray(int[] ar) {
for(int n: ar){
System.out.print(n+" ");
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] ar = new int[n];
for(int i=0;i<n;i++){
ar[i]=in.nextInt();
}
insertionSort(ar);
}
}