forked from dscmsit/Problem-Solving-in-any-Language
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubblesort.java
More file actions
34 lines (26 loc) · 794 Bytes
/
bubblesort.java
File metadata and controls
34 lines (26 loc) · 794 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
class bubbleSort {
public static void main(String[] args) {
int a[] = { 36, 19, 29, 12, 5 };
// make temporary space for sorting 1 number to another space
int temp;
// using flag for time complexity
int flag = 0;
for (var i = 0; i < a.length; i++) {
// put -i for time complexity
for (var j = 0; j < a.length - i - 1; j++) {
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
flag = 1;
}
}
if (flag == 0) {
break;
}
}
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}