# Quicksort implementation with pivotal calculated as middle element

I read quick sort algorhitm and implemented in like this:

public List<Integer> sort(List<Integer> list) {
if (list.size() <= 1) {
return list;
}

int pivotalValue = list.get(list.size() / 2);

List<Integer> left = new ArrayList<>();
List<Integer> pivotalValues = new ArrayList<>();
List<Integer> right = new ArrayList<>();
for (Integer element : list) {
if (element < pivotalValue) {
} else if (element > pivotalValue) {
} else {
}
}
List<Integer> sortedLeft = sort(left);
List<Integer> sortedRight = sort(right);
return sortedLeft;

}


What do you think about my implementation?

• I think the title fails to name the distinguishing feature of this implementation: 3-way distributing values to Lists. You present uncommented/undocumented code. You needlessly forsake genericity. (I was tempted to down-vote the question out of considering the code presented abysmal - the voting hints stress usefulness of the question.) – greybeard Nov 24 '18 at 18:27
• @greybeard I think code is self explained – gstackoverflow Nov 24 '18 at 20:21
• Try using your method in a separate source file. See what tool support you get from your IDE of choice with or without doc comments. Revisit your code a couple of years later. – greybeard Nov 24 '18 at 21:00
• @greybeard, could you provide concrete points you don't like? It is really unclear what you to say – gstackoverflow Nov 24 '18 at 21:17
• could you provide concrete points you [have an opinion on]? I try to refrain from getting into details of uncommented code. – greybeard Nov 24 '18 at 21:19

What do you think about my implementation?

To tell you the truth, I don't think much of your implementation. Instead of in-place swaps you're creating a bunch of temporary lists and combining them after. It probably more closely resembles a merge sort than a quick sort.

Creating all those lists and merging them together requires many more iterations than the usual quick sort.

Altogether, it seems to me, that both the time and space complexity is worse than the usual quick sort.

• I am agree about memory complexity, but I don't understand why time complexity worse than usual quick sort – gstackoverflow Oct 25 '18 at 17:22
• @gstackoverflow - merging the lists together requires extra iterations over the data. – tinstaafl Oct 25 '18 at 17:25
• Are you sure? lists are not arrays – gstackoverflow Oct 25 '18 at 17:33
• @gstackoverflow - a quote from the addAll doc page The new elements will appear in this list in the order that they are returned by the specified collection's iterator. – tinstaafl Oct 25 '18 at 17:41
• @gstackoverflow re. I don't understand why time complexity worse than usual quick sort Did you test it? What kind of run time to you get? How does that compare to Collections.sort()? – markspace Dec 20 '19 at 3:15