SOLVED: posted in the end of THIS comment.
I keep getting this error and I can't find any explanation to why it occurs.
Exception in thread "main" java.lang.StackOverflowError
at java.util.Random.nextDouble(Random.java:444)
at java.lang.Math.random(Math.java:716)
at assignment6quickSort.M6.qsAlgorithm(M6.java:50)
at assignment6quickSort.M6.qsAlgorithm(M6.java:60)
at assignment6quickSort.M6.qsAlgorithm(M6.java:60)
at assignment6quickSort.M6.qsAlgorithm(M6.java:60)
I've been googling like crazy and it seems no one have had the same problem OR I'm to stupid to search for the right thing (utterly possible).
Anyway, I'm creating random numbers to find a pivot number for a generic quickSort and a couple of hours ago it worked some times but now I get this error every single time.
Please, I'm so frustrated... Hehe! What the h*ck am I doing wrong? How can this cause an overflow?
Here comes my code...
package assignment6quickSort;
import java.util.ArrayList;
import java.util.List;
import java.util.Comparator;
public class M6 {
static M6Comparator<Integer> comp = new M6Comparator<Integer>();
static Integer[] array = new Integer[20];
static ArrayList qsSorted = new ArrayList();
public static void main (String[] args) {
for (int i = 0; i < array.length; i++) {
array[i] = (int)(50 * Math.random());
}
for (int i: array) {
System.out.print(i + ", ");
}
quickSort(array, comp);
System.out.println("\n");
for (Object i: qsSorted) {
System.out.print(i + ", ");
}
}
static <T> void quickSort(T[] a, Comparator<? super T> comp) {
ArrayList<T> temp = new ArrayList<T>();
for (int i = 0; i < a.length; i++) {
temp.add(a[i]);
}
qsSorted = qsAlgorithm(temp, comp);
}
static <T> ArrayList<T> qsAlgorithm(ArrayList<T> a, Comparator<? super T> comp) {
ArrayList<T> L = new ArrayList<T>();
ArrayList<T> G = new ArrayList<T>();
if (a.size() <= 1)
return a;
int pivot = (int)Math.random() * a.size();
T pivotValue = a.get(pivot);
for (int i = 0; i < a.size(); i++) {
if (comp.compare(a.get(i), pivotValue) == -1 || comp.compare(a.get(i), pivotValue) == 0) {
L.add(a.get(i));
} else {
G.add(a.get(i));
}
}
L = qsAlgorithm(L, comp);
G = qsAlgorithm(G, comp);
L.addAll(G);
return L;
}
}
Additionally, here is my Comparator:
package assignment6quickSort;
import java.util.Comparator;
public class M6Comparator<E> implements Comparator<E> {
public int compare(E original, E other) {
return((Comparable<E>)original).compareTo(other);
}
}
### SOLUTION ###
Apparently a classic recursive overflow error. Thanks a bunch to @pst and @Marcin for the help! Here is the revision of qsAlgorithm() method:
static <T> ArrayList<T> qsAlgorithm(ArrayList<T> a, Comparator<? super T> comp) {
ArrayList<T> L = new ArrayList<T>();
ArrayList<T> P = new ArrayList<T>();
ArrayList<T> G = new ArrayList<T>();
if (a.size() <= 1)
return a;
int pivot = (int)Math.random() * a.size();
T pivotValue = a.get(pivot);
for (int i = 0; i < a.size(); i++) {
int v = comp.compare(a.get(i), pivotValue);
if (v == -1) {
L.add(a.get(i));
} else if (v == 0) {
P.add(a.get(i));
} else {
G.add(a.get(i));
}
}
return concatenate(qsAlgorithm(L, comp), P, qsAlgorithm(G, comp));
}
static <T> ArrayList<T> concatenate(ArrayList<T> a, ArrayList<T> p, ArrayList<T> b) {
a.addAll(p);
a.addAll(b);
return a;
}
((Comparable<E>)original).compareTo(other);is not calling itself?