forked from Lilliklave/Hundregistret
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDogSorter.java
51 lines (30 loc) · 1.22 KB
/
DogSorter.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
//Liliana Klavebäck Martinez [email protected]
import java.util.ArrayList;
import java.util.Comparator;
public class DogSorter {
private static void swapDogs(ArrayList<Dog> doglist, int indexOne, int indexTwo ){
Dog temp = doglist.get(indexOne);
doglist.set(indexOne, doglist.get(indexTwo));
doglist.set(indexTwo, temp);
}
private static int nextDog(Comparator<Dog> comparator, ArrayList<Dog> doglist, int start){
int min = start;
for( int j = start + 1; j < doglist.size(); j++){
if(comparator.compare(doglist.get(j), doglist.get(min)) < 0){
min = j;
}
}
return min;
}
public static int sortDogs(Comparator<Dog> comparator, ArrayList<Dog> doglist){
int counter = 0;
for(int i = 0; i < doglist.size() - 1; i++){
int min = nextDog(comparator, doglist, i);
if(min != i){
swapDogs(doglist, i, min);
counter++;
}
}
return counter;
}
}