forked from 790hanu/Annex-qr-code-simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Insertion.java
32 lines (30 loc) · 877 Bytes
/
Insertion.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
public class Insertion {
public static void main(String[] args) {
long s = System.nanoTime();
int i,j,key;
int []a=new int[100];
for(i=1;i<100;i++){
a[i]=(int)(Math.random()*100);
}
int swap=0;
int comp=0;
for(i=1;i<100;i++){
key=a[i];
j=i-1;
while(j>=0 && key<a[j]){
a[j+1]=a[j];
j--;
swap++;
}
comp++;
a[j+1]=key;
}
long e = System.nanoTime();
for(i=0;i<100;i++){
System.out.print(+a[i]+",");
}
System.out.println("no of swap: "+swap);
System.out.println("no of comparisons: "+comp);
System.out.println("total time in nano second is: "+(e-s));
}
}