Given a Generic type List (List),and two generic type objects: a,b:
-Design an algorithm that returns a List that contains every element from the original list that belongs to the range [a,b).
-The algorithm must include comparator E.
-The solution must be a "Divide and Conquer" algorithm.
This is what i came up with:
private static <E extends Comparable<E>> List <E> Dominio(List<E> lista, E a, E b){
return DominioAux(lista,a,b,0,lista.size()-1);
}
private static <E extends Comparable<E>> List <E> DominioAux(List<E> lista, E a, E b,Integer i, Integer j){
List<E> res = new ArrayList<>();
Integer m = (j-i)/2;
E pm = lista.get(m);
if (pm == a) {
DominioAux(lista,a,b,m,j);
} else if(pm==b) {
DominioAux(lista,a,b,i,m-1);
}
if (pm.compareTo(a)<0) {
res = DominioAux(lista,a,b,m,j);
}
if (pm.compareTo(a)>0) {
res = DominioAux(lista,a,b,i,m);
}
res = lista.subList(i, j);
return res;
}
The problem is I either get an "Index out of bounds exception" or an "Stack overflow error" when one of the ifs are executed.