Quicksort improvement.

Consider two improvement for Quicksort.

1. Chose a better pivot so that it never has just 1 key in a partition.

2. Only call the recursion when the array having length >16 (or greater).

The question is: do these improvement change the worst case and average-case complexities of the algorithm (n*n and n*logn) and why ?

Any idea ? ( I suppose those don't change the complexity (in average), but why ?)

[451 byte] By [csbham] at [2007-9-30 7:26:09]
# 1

The first improvement (better pivot) definitely changes the worst case O(n^2), which occurs if a sorted list is to be sorted and the pivot is simply chosen to be the first (0th) element.

The worst case is O(n^2) because the left partitions are always going to be of size 0, and everything will always be on the right (greater than) the pivot, which is the smallest element.

If a better pivot is chosen, the left partitions at each step in the quicksort will have some size greater than 0, coming closer to "chopping the problem in half" [O(n*log n)].

Hope this helps,

- D.t.O

DrinkHotJavaCoffee at 2007-7-2 0:05:45 > top of Java-index,Other Topics,Algorithms...
# 2
Yes, I have ideas. I know all about it. But then again, so does your teacher (hopefully). Why did your teacher ask you this question, when he knows the answer?
Ragnvald_id2 at 2007-7-2 0:05:45 > top of Java-index,Other Topics,Algorithms...
# 3
That's the case 1, so how about case 2 when you don't use recursion with (right-left)<=16.
csbham at 2007-7-2 0:05:45 > top of Java-index,Other Topics,Algorithms...
# 4
Thats part one of my homework, thanks for doing my homework for me, but you forgot to do part 2. I am to lazy to do it myself, so please do it for me.
Ragnvald_id2 at 2007-7-2 0:05:45 > top of Java-index,Other Topics,Algorithms...
# 5
Ragnvald_id2, if you know how to do, just do it. Please don't ask people do it for you. I asked for help just because I don't know, not because of my laziness.
csbham at 2007-7-2 0:05:45 > top of Java-index,Other Topics,Algorithms...
# 6

Well, your homework assignment asks you to consider.

What have you considered so far? You could implement the algorithm, with and without the purposed cut off at 16, and test the performance. You could do it without implementing too, just by visualizing (use pen and paper!) stop by step how the sorting progresses. Andyou could search the web. Try words like cutoff quicksort "big o" qsort in your search criteria.

Good luck with the assignment!

Ragnvald_id2 at 2007-7-2 0:05:45 > top of Java-index,Other Topics,Algorithms...
# 7

I came up with the following solution (not sure that correct)

If when reaching the length of 16, instead of calling recursion, you applied the insertion Sort. In this case, with the array almost sorted, the Insertion sort takes linear time.

Then with the worst case, the complexity should be n*(n-15) or n*n - 15*n.

How do you think ?

csbham at 2007-7-2 0:05:45 > top of Java-index,Other Topics,Algorithms...
# 8
> Then with the worst case, the complexity should be> n*(n-15) or n*n - 15*n.sounds correct to me :) > How do you think ?no comment
asjf at 2007-7-2 0:05:45 > top of Java-index,Other Topics,Algorithms...
# 9
> Then with the worst case, the complexity should be> n*(n-15) or n*n - 15*n.This is just O(n^2).
DrClap at 2007-7-2 0:05:45 > top of Java-index,Other Topics,Algorithms...
# 10
How about the average case then ? Any idea ?
csbham at 2007-7-2 0:05:45 > top of Java-index,Other Topics,Algorithms...
# 11

As always, one of the best places to look for code is in the Sun J2SE source code. The Arrays.sort() methods (except the one that takes an Object[] parameter) are all a tuned quicksort, which, according to the documentation, "offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance."

Here's the code that you are probably interested in:

// Insertion sort on smallest arrays

if (len < 7) {

for (int i=off; i<len+off; i++)

for (int j=i; j>off && x[j-1]>x[j]; j--)

swap(x, j, j-1);

return;

}

(The swap() method just swaps the two specified indices of the array.)

Apparently insertion sort, when used on arrays of length 0-6, has at least about n*log(n) performance.

Hope this helps,

- D.t.O

DrinkHotJavaCoffee at 2007-7-2 0:05:45 > top of Java-index,Other Topics,Algorithms...
# 12
to DrinkHotJavaCoffee, could you give me the link to that source code as well as all the source code in Java library. ?
csbham at 2007-7-2 0:05:45 > top of Java-index,Other Topics,Algorithms...
# 13

> Apparently insertion sort, when used on arrays of length 0-6, has at least about n*log(n) performance.

If you look at the definition of big-O notation, you'll find that if you restrict the range of n to be finite, any algorithm which is guaranteed to terminate will be O(n^0) (or O(1) - I just like to make the variable explicit).

YATArchivist at 2007-7-2 0:05:45 > top of Java-index,Other Topics,Algorithms...
# 14
> to DrinkHotJavaCoffee, could you give me the link to> that source code as well as all the source code in> Java library. ?It's in the %java_dir%/bin/src.zip file, where %java_dir% represents where you installed java.
DrinkHotJavaCoffee at 2007-7-2 0:05:45 > top of Java-index,Other Topics,Algorithms...
# 15

> If you look at the definition of big-O notation,

> you'll find that if you restrict the range of n to be

> finite, any algorithm which is guaranteed to terminate

> will be O(n^0) (or O(1) - I just like to make the

> variable explicit).

That's right, but although 2 operations and 2 million operations both reduce to O(1), they are different.

DrinkHotJavaCoffee at 2007-7-2 0:05:48 > top of Java-index,Other Topics,Algorithms...
# 16
http://www.cs.ubc.ca/spider/harrison/Java/sorting-demo.htmlthose have examples and demos
Backstab at 2007-7-2 0:05:48 > top of Java-index,Other Topics,Algorithms...