-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeMesurment.java
204 lines (157 loc) · 6.03 KB
/
TimeMesurment.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Random;
import java.util.Arrays;
public class TimeMesurment {
public static void main(String[] args) {
System.out.println("Performing the time measure of the O(N*logN) sorting algorithm ");
resultArraysSort();
System.out.println("");
System.out.println("Performing the time measure of the Bubble sorting algorithm ");
resultBubble();
System.out.println("");
System.out.println("Performing the time measure of the Insertion sorting algorithm ");
resultInsertion();
System.out.println("");
System.out.println("Performing the time measure of the Selection sorting algorithm ");
resultSelection();
System.out.println("");
}
public void saveDataInFile(String data, String fileName)
throws IOException {
RandomAccessFile stream = new RandomAccessFile(fileName, "rw");
FileChannel channel = stream.getChannel();
byte[] strBytes = data.getBytes();
ByteBuffer buffer = ByteBuffer.allocate(strBytes.length);
buffer.put(strBytes);
buffer.flip();
channel.write(buffer);
stream.close();
channel.close();
}
public static void resultArraysSort(){
int[] testData;
Random random = new Random();
for (int size = 50_000; size <= 200_000; size *= 2) {
System.out.printf("Size: %,d\n", size);
testData = new int[size];
System.out.println("Sorted test data");
for (int i = 0; i < size; i++) {
testData[i] = i;
}
timePerformanceOlogN(testData);
System.out.println("Random test data");
for (int i = 0; i < size; i++) {
testData[i] = random.nextInt();
}
timePerformanceOlogN(testData);
System.out.println("Reverse sorted test data");
for (int i = 0; i < size; i++) {
testData[i] = i-1;
}
timePerformanceOlogN(testData);
}
}
public static void resultInsertion(){
int[] testData;
Random random = new Random();
for (int size = 50_000; size <= 200_000; size *= 2) {
System.out.printf("Size: %,d\n", size);
testData = new int[size];
System.out.println("Sorted test data");
for (int i = 0; i < size; i++) {
testData[i] = i;
}
timePerformanceInsertion(testData);
System.out.println("Random test data");
for (int i = 0; i < size; i++) {
testData[i] = random.nextInt();
}
timePerformanceInsertion(testData);
System.out.println("Reverse sorted test data");
for (int i = 0; i < size; i++) {
testData[i] = i-1;
}
timePerformanceInsertion(testData);
}
}
public static void resultSelection(){
int[] testData;
Random random = new Random();
for (int size = 50_000; size <= 200_000; size *= 2) {
System.out.printf("Size: %,d\n", size);
testData = new int[size];
System.out.println("Sorted test data");
for (int i = 0; i < size; i++) {
testData[i] = i;
}
timePerformanceSelection(testData);
System.out.println("Random test data");
for (int i = 0; i < size; i++) {
testData[i] = random.nextInt();
}
timePerformanceSelection(testData);
System.out.println("Reverse sorted test data");
for (int i = 0; i < size; i++) {
testData[i] = i-1;
}
timePerformanceSelection(testData);
}
}
public static void resultBubble(){
int[] testData;
Random random = new Random();
for (int size = 50_000; size <= 200_000; size *= 2) {
System.out.printf("Size: %,d\n", size);
testData = new int[size];
System.out.println("Sorted test data");
for (int i = 0; i < size; i++) {
testData[i] = i;
}
timePerformanceBubble(testData);
System.out.println("Random test data");
for (int i = 0; i < size; i++) {
testData[i] = random.nextInt();
}
timePerformanceBubble(testData);
System.out.println("Reverse sorted test data");
for (int i = 0; i < size; i++) {
testData[i] = i-1;
}
timePerformanceBubble(testData);
}
}
public static void timePerformanceOlogN(int[] array){
long start, end, totalRuntime;
start = System.currentTimeMillis();
Arrays.sort(array);
end = System.currentTimeMillis();
totalRuntime = (end - start);
System.out.printf(" Lapse time: %d milliseconds\n", totalRuntime );
}
public static void timePerformanceInsertion(int[] array){
long start, end, totalRuntime;
start = System.currentTimeMillis();
Sort.insertionSort(array);
end = System.currentTimeMillis();
totalRuntime = (end - start);
System.out.printf(" Lapse time: %d seconds\n", totalRuntime/1000 );
}
public static void timePerformanceBubble(int[] array){
long start, end, totalRuntime;
start = System.currentTimeMillis();
Sort.bubbleSort(array);
end = System.currentTimeMillis();
totalRuntime = (end - start);
System.out.printf(" Lapse time: %d seconds\n", totalRuntime/1000 );
}
public static void timePerformanceSelection(int[] array){
long start, end, totalRuntime;
start = System.currentTimeMillis();
Sort.selectionSort(array);
end = System.currentTimeMillis();
totalRuntime = (end - start);
System.out.printf(" Lapse time: %d seconds\n", totalRuntime/1000 );
}
}