-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectionSort.java
55 lines (44 loc) · 1.27 KB
/
SelectionSort.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
/**
* SelectionSort Algorithm is implemented thanks to the
* following tutorial:
* https://www.geeksforgeeks.org/selection-sort/
* */
import java.util.ArrayList;
public class SelectionSort implements SortingAlgorithm {
private long start,end;
@Override
public ArrayList<Integer> sort(ArrayList<Integer> input) {
start = System.nanoTime();
int inputSize = input.size();
/**
* Go through the input size to find the lowest number
* */
for (int i = 0; i < inputSize - 1; i++){
int current = i;
/**
* find the lowest number
* */
for (int j = i+1; j < inputSize; j++){
if (input.get(j).compareTo(input.get(current)) < 0) {
current = j;
}
}
/**
* swap the lowest number with the first element
* */
int temp = input.get(current);
input.set(current,input.get(i));
input.set(i, temp);
}
end = System.nanoTime();
return input;
}
@Override
public long executionTime() {
return end-start;
}
@Override
public String getAlgorithmName() {
return "Selection Sort";
}
}