I have written a program to sort Linked Lists and I noticed that my insertion sort works much better than my quicksort algorithm. Does anyone have any idea why this is? Insertion sort has a complexity of $\Theta(n^2)$ and quicksort $O(n\log n)$ so therefore quicksort should be faster. I tried for random input size and it shows me the contrary. Strange...
Here the code in Java:
public static LinkedList qSort(LinkedList list) {
LinkedList x, y;
Node currentNode;
int size = list.getSize();
//Create new lists x smaller equal and y greater
x = new LinkedList();
y = new LinkedList();
if (size <= 1)
return list;
else {
Node pivot = getPivot(list);
// System.out.println("Pivot: " + pivot.value);
//We start from the head
currentNode = list.head;
for (int i = 0; i <= size - 1; i++) {
//Check that the currentNode is not our pivot
if (currentNode != pivot) {
//Nodes with values smaller equal than the pivot goes in x
if (currentNode.value <= pivot.value) {
{
x.addNode(currentNode.value);
// System.out.print("Elements in x:");
// x.printList();
}
}
//Nodes with values greater than the pivot goes in y
else if (currentNode.value > pivot.value) {
if (currentNode != pivot) {
y.addNode(currentNode.value);
// System.out.print("Elements in y:");
// y.printList();
}
}
}
//Set the pointer to the next node
currentNode = currentNode.next;
}
//Recursive calls and concatenation of the Lists and pivot
return concatenateList(qSort(x), pivot, qSort(y));
}
}