-
Notifications
You must be signed in to change notification settings - Fork 0
/
BubbleSort.java
36 lines (30 loc) · 928 Bytes
/
BubbleSort.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
package kb.sort;
import kb.sort.api.Sortable;
public class BubbleSort implements Sortable {
/**
* Bubble Sort, not accepted (Time Limit Exceeded).
* Moreover the same time complexity as Insertion Sort or Selection Sort
* this algorithm seems slower in practice.
* O(n^2) time, O(1) space.
*/
@Override
public int[] sort(int[] nums) {
for (int i = nums.length - 1; i >= 1; i--) {
boolean sorted = true;
for (int j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) {
sorted = false;
int tmp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = tmp;
}
}
if (sorted)
break;
}
return nums;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}