-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
118 lines (64 loc) · 1.59 KB
/
BubbleSort.java
File metadata and controls
118 lines (64 loc) · 1.59 KB
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
// Jain Aarush
// April 20th, 2020
// Sorting Algorithms
// ICS3U Mrs.Strelkovska
public class BubbleSort {
public static void main(String[] args) {
int list1[] = new int[1000];
int random;
long start = 0L, end = 0L;
int microseconds = 0;
for (int i = 0; i < list1.length; i++) {
random = (int)(Math.random() * 500) + 1;
list1[i] = random;
}
start = System.nanoTime();
int j, key, temp;
for (int i = 1; i < list1.length; i++) {
key = list1[i];
j = i - 1;
while (j >= 0 && key < list1[j]) {
temp = list1[j];
list1[j] = list1[j + 1];
list1[j + 1] = temp;
j--;
}
}
end = System.nanoTime();
microseconds = (int)((end - start) / 1000);
for (int i = 0; i < 100; i++) {
System.out.println(list1[i]);
}
System.out.println();
System.out.println(microseconds + "\n");
int list2[] = new int[1000];
for (int a = 0; a < list2.length; a++) {
random = (int)(Math.random() * 500) + 1;
list2[a] = random;
}
start = System.nanoTime();
int y, z, smallv, smalli, temp1 = 0;
for (y = 0; y < list2.length; y++) {
smallv = list2[y];
smalli = y;
for (z = y; z < list2.length; z++) {
if (list2[z] < smallv) {
smallv = list2[z];
smalli = z;
}
}
if (smallv < list2[y]) {
temp1 = list2[y];
list2[y] = list2[smalli];
list2[smalli] = temp1;
}
}
end = System.nanoTime();
microseconds = (int)((end - start) / 1000);
for (int x = 0; x < 100; x++) {
System.out.println(list2[x]);
}
System.out.println();
System.out.println(microseconds);
}
}