-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPart1D.java
76 lines (69 loc) · 1.36 KB
/
Part1D.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.util.Arrays;
import java.util.Random;
public class Part1D {
public static void main(String[] args) {
int n= Integer.parseInt(args[0]);
Random rand= new Random();
int[] a= new int[n];
int[] random= new int[n];
int count=0;
for (int i=0; i<n;i++) {
random[i]= rand.nextInt(10000000);
}
a=Arrays.copyOf(random, n);
for (int i=1; i<n;i++) {
int j=i;
int k=j-1;
count+=2;
while (k>=0 && a[j]<a[k]) {
int aj=a[j];
a[j]=a[k];
a[k]=aj;
j--;
k--;
count+=4;
}
}
int randomCount=count;
System.out.println("random case count: "+ randomCount);
count=0;
Arrays.sort(a);
for (int i=1; i<n;i++) {
int j=i;
int k=j-1;
count+=2;
while (k>=0 && a[j]<a[k]) {
int aj=a[j];
a[j]=a[k];
a[k]=aj;
j--;
k--;
count+=4;
}
}
int bestCount=count;
System.out.println("best case count: "+ bestCount);
int[] worst= new int[n];
Arrays.sort(a);
for (int i=0; i<n;i++) {
worst[n-1-i]=a[i];
}
a= Arrays.copyOf(worst, n);
count=0;
for (int i=1; i<n;i++) {
int j=i;
int k=j-1;
count+=2;
while (k>=0 && a[j]<a[k]) {
int aj=a[j];
a[j]=a[k];
a[k]=aj;
j--;
k--;
count+=4;
}
}
int worstCount=count;
System.out.println("worst case count: "+ worstCount);
}
}