-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickSortTeams.java
38 lines (30 loc) · 1022 Bytes
/
quickSortTeams.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
package statCalculator;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class quickSortTeams extends Sorter{
@Override
public void sort(Division divisionToSort, Comparator<String> comp) {
quickSortRec(divisionToSort,comp,0,divisionToSort.size()-1);
}
private void quickSortRec(Division divisionToSort, Comparator<String> comp, int start, int end) {
if(start>=end) {
return;
}
int part = partition(divisionToSort,comp,start,end);
quickSortRec(divisionToSort,comp,start,part-1);
quickSortRec(divisionToSort,comp,part+1,end);
}
private int partition(Division divisionToSort, Comparator<String> comp, int start, int end) {
int i = start;
for(int j = start; j<end; j++) {
if(comp.compare(divisionToSort.get(j).getName(), divisionToSort.get(end).getName())<0) {
i++;
Team temp = divisionToSort.get(i);
Collections.swap(divisionToSort.teamList, i, j);
}
}
Collections.swap(divisionToSort.teamList, i+1, end);
return i+1;
}
}