My friends told me that there are some error in the implementation of JavaSE6 sorting algorithm for int[], which is quicksort, I want to check the implementation, how can I do that? Where can I find the code?
Asked
Active
Viewed 136 times
-5
-
6You might not want to take programming advice from your friends. – Brian Roach Mar 07 '12 at 08:37
-
1ask your friend for the code. – Chandra Sekhar Mar 07 '12 at 08:37
-
1The source is in the JDK and your IDE should load it automatically. In my IDE, I can use `
+ – Peter Lawrey Mar 07 '12 at 08:46` on a method to see its source. -
To down voting person: What's wrong with careful checking? – safarisoul Mar 07 '12 at 08:47
-
ctrl+click only shows me the .Class file, I found medium of 3 is used. "invokestatic java.util.Arrays.med3(int[], int, int, int) : int [445]" – safarisoul Mar 07 '12 at 09:43
-
To curious soul: google median-of-3 killer – safarisoul Mar 07 '12 at 10:07
2 Answers
1
Most likely this one:
http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html
It was a general problem, that a crucial calculation was prone to integer overflow. Apparently your friends got it wrong.

Thorbjørn Ravn Andersen
- 73,784
- 33
- 194
- 347
-
-
@safarisoul no, it is the problem of averaging two limited representation integers. – Thorbjørn Ravn Andersen Mar 07 '12 at 10:21
-
-
@safarisoul, yes, QuickSort has an O(n^2) worst case. That is not a _bug_. Your point? – Thorbjørn Ravn Andersen Mar 08 '12 at 09:19
-
I don't care how you name it, bug or flower. And I know the worst case is rare, not likely to happen in real world. I'm just curious to the reason underneath. And thanks for the link you shared, a good point too. – safarisoul Mar 08 '12 at 23:42
-
@safarisoul you asked for an error in the implementation of quicksort which the incorrect calculation of the average of two integers is as it cause an exception. Calculating the pivot element as the median of three elements instead of just picking the highest one (as in the original implementation of QuickSort) is not an error - it just causes the worst case to go from O(n log n) to O(n^2). Vast difference. – Thorbjørn Ravn Andersen Mar 09 '12 at 02:57