-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
45 lines (37 loc) · 1.08 KB
/
Copy pathBubbleSort.java
File metadata and controls
45 lines (37 loc) · 1.08 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
/**
* BubbleSort Algorithm is implemented thanks to the
* following tutorial:
* https://www.geeksforgeeks.org/bubble-sort/
* */
import java.util.ArrayList;
public class BubbleSort implements SortingAlgorithm {
long start,end;
@Override
public ArrayList<Integer> sort(ArrayList<Integer> input) {
start = System.nanoTime();
int inputLength = input.size();
/**
* Check if the number adjacent to
* @param current is lower, if so -> swap
* */
for (int i = 0; i< inputLength-1; i++){
for (int j = 0; j < inputLength - i - 1; j++){
if(input.get(j).compareTo(input.get(j+1)) > 0){
int current = input.get(j);
input.set(j, input.get(j+1));
input.set(j+1, current);
}
}
}
end = System.nanoTime();
return input;
}
@Override
public long executionTime() {
return end-start;
}
@Override
public String getAlgorithmName() {
return "Bubble Sort";
}
}