is there a way to get the same result when using LinkedList rather than an array? The following code works perfectly, only when Im using array in its generics form. I want to do the same by using LinkedList. so something like this :
public static <E extends Comparable<E>> LinkedList<E> qSort(LinkedList<E> list) {
return list;
}
this is what I already have..
public static <E extends Comparable<E>> E[] quickSort(E[] list) {
if (list.length <= 1) {
return list;
}
sort(list, 0, list.length - 1);
return list;
}
public static <E extends Comparable<E>> void sort(E[] list, int low, int high) {
if ((high - low) <= 0) {
return;
}
int splitPoint = split(list, low, high);
sort(list, low, splitPoint - 1);
sort(list, splitPoint + 1, high);
}
private static <E extends Comparable<E>> int split(E[] list, int low, int high) {
int left = low + 1;
int right = high;
E pivot = list[low];
while (left <= right) {
if (list[left].compareTo(pivot) < 0) {
left++;
} else {
break;
}
}
while (true) {
while (right > left) {
if (list[right].compareTo(pivot) < 0) {
break;
} else {
right--;
}
}
if (left >= right) {
break;
}
E temp = list[left];
list[left] = list[right];
list[right] = temp;
left++;
right--;
}
list[low] = list[left -1];
list[left-1] = pivot;
return left-1;
}