I'm trying to implement the quicksort algorithm with ArrayList but can't seem to get this working. It gives me an error for my recursive return call in the sort() function. Please help!
This is the error I get: Exception in thread "main" java.lang.StackOverflowError
public static void main(String[] args) {
List<Integer> l = new ArrayList<Integer>();
l.add(10); l.add(7); l.add(13); l.add(22); l.add(9);
//l.get(2);
System.out.println(sort(l));
}
public static List<Integer> sort(List<Integer> l) {
if (l.size() <= 1) {
return l;
}
int pivot = l.get(0);
List<Integer> left = new ArrayList<Integer>();
List<Integer> right = new ArrayList<Integer>();
for (int i = 0; i < l.size(); i++) {
if (l.get(i) < pivot) {
left.add(l.get(i));
//System.out.println(left);
} else {
right.add(l.get(i));
}
}
return join(sort(left), sort(right), pivot);
}
public static List<Integer> join(List<Integer> left, List<Integer> right, int pivot) {
List<Integer> l = new ArrayList<Integer>();
for (int i = 0; i < left.size(); i++) {
l.add(left.get(i));
}
l.add(pivot);
for (int i = l.size(); i < right.size(); i++) {
l.add(right.get(i));
}
return l;
}
}
sort()method with the same arguments repeatedly, often when you get towards a "leaf" of the recursion.