Talk:Algorithm Implementation/Sorting/Quicksort

=Transwiki=

Untitled
This article was created from Transwiki:Quicksort implementations. This is the content of the talk page:

Quicksort implementations edit history

 * 05:15, 23 February 2006 Freakofnurture (transwiki-ing)
 * 22:55, 16 February 2006 198.135.203.9
 * 15:59, 16 February 2006 Mike Rosoft (Reverted edits by 216.79.182.236 (talk) to last version by Mike Rosoft)
 * 15:53, 16 February 2006 216.79.182.236
 * 15:47, 16 February 2006 Mike Rosoft (Reverted edits by 216.79.182.236 (talk) to last version by Donhalcon)
 * 15:43, 16 February 2006 216.79.182.236
 * 15:16, 15 February 2006 Donhalcon (Deletion)
 * 15:16, 15 February 2006 Donhalcon (Deletion)
 * 23:37, 12 February 2006 Rebooted ( →Ruby )
 * 23:37, 12 February 2006 Rebooted ( →Ruby )
 * 16:10, 10 February 2006 Allan McInnes (rv vandalism by User:216.79.174.154 to last version by User:61.213.69.15)
 * 15:58, 10 February 2006 216.79.174.154
 * 15:55, 10 February 2006 216.79.174.154
 * 15:49, 10 February 2006 216.79.174.154
 * 14:47, 8 February 2006 61.213.69.15 ( →C++ )
 * 06:22, 3 February 2006 Dcoetzee (Revert my own removal of stuff removed from core impls, vandalism (please contest copyvio on the talk page), and not terribly useful links)
 * 00:36, 3 February 2006 168.209.98.35 ( →C )
 * 00:34, 3 February 2006 168.209.98.35 ( →Zilog z80000 assembly language )
 * 00:33, 3 February 2006 168.209.98.35 ( →Ruby )
 * 00:29, 3 February 2006 168.209.98.35 ( →Ruby )
 * 12:17, 30 January 2006 213.228.191.42 ( →Python )
 * 12:16, 30 January 2006 213.228.191.42 ( →Ruby )
 * 09:09, 28 January 2006 Dcoetzee (Revert due to revert in core implementations; please discuss on Talk:Quicksort first)
 * 23:30, 27 January 2006 R.Koot ( →C++ - from core impementation)
 * 10:26, 26 January 2006 62.3.255.109
 * 06:51, 23 January 2006 Doug Bell (Formatting, wfy)
 * 07:14, 14 January 2006 151.37.145.68 ( →Ruby )
 * 22:16, 12 January 2006 24.7.222.105 ( →OCaml )
 * 23:49, 6 January 2006 196.202.27.161 ( →Ruby )
 * 23:48, 6 January 2006 196.202.27.161 ( →Ruby )
 * 23:46, 6 January 2006 196.202.27.161 ( →Ruby )
 * 23:42, 6 January 2006 196.202.27.161 ( →Ruby - Changed it because it doesn't work on the recent Ruby 1.8.2)
 * 19:26, 25 December 2005 Dcoetzee ( →Haskell - Yes, yes, we have an example using list comprehensions in the core implementations section)
 * 14:25, 25 December 2005 RAFPeterM ( →Haskell )
 * 15:45, 16 December 2005 Ahoerstemeier (Reverted edits by 65.125.82.250 (talk) to last version by 195.228.78.189)
 * 14:36, 16 December 2005 65.125.82.250 ( →Java )
 * 09:29, 14 December 2005 195.228.78.189 (Another Delphi implementation with comments)
 * 02:08, 6 December 2005 67.165.76.139 ( →Additional implementations )
 * 20:49, 30 November 2005 Dcoetzee (Delphi: Conventionally we put implementations in the same language in the same subsection)
 * 20:26, 30 November 2005 205.244.116.139
 * 20:25, 30 November 2005 205.244.116.139 (added another possible delphi quicksort)
 * 05:42, 29 November 2005 Dcoetzee ( →J - space)
 * 05:42, 29 November 2005 Dcoetzee (A less obtuse J example)
 * 03:25, 29 November 2005 Dcoetzee ( →Mathematica - A test driver)
 * 03:12, 29 November 2005 Jdh30 (Mathematica implementation)
 * 00:24, 29 November 2005 Dcoetzee ( →Core implementations - HTML comment on editing this section)
 * 00:23, 29 November 2005 Dcoetzee (Add core implementations (with template), additional implementations section, remove stuff covered in core implementations)
 * 18:27, 28 November 2005 Jdh30 (Tidied up the OCaml implementation.)
 * 23:29, 9 November 2005 Bluebot (Bringing &quot;External links&quot; and &quot;See also&quot; sections in line with the Manual of Style.)
 * 13:01, 25 October 2005 Mga ( →Prolog - _ is not necessary (links))
 * 15:18, 24 October 2005 Roland2 ( →Perl 6 - fixed visualisation of $_)
 * 15:17, 24 October 2005 Roland2 ( →Perl - fixed visualisation of @_)
 * 14:41, 24 October 2005 Jogloran ( →Perl )
 * 21:17, 23 October 2005 82.83.233.107 ( →Perl - fixed missing _)
 * 16:46, 21 October 2005 Roland2 ( →Perl - kuerzer)
 * 01:35, 21 October 2005 24.182.23.95 ( →Haskell - Moved the concise version before the optimized version to avoid confusion.)
 * 01:28, 21 October 2005 24.182.23.95 ( →Haskell - Added a concise version of the Haskell quicksort)
 * 19:38, 20 October 2005 Roland2 ( →Perl - Perl 6)
 * 07:09, 15 October 2005 Roger Hui (word fiddling)
 * 07:08, 15 October 2005 Roger Hui (list no longer sorted by no. of lines)
 * 11:24, 11 October 2005 T4bits (Changed the order of ARM assembly language with AppleScript)
 * 11:19, 11 October 2005 T4bits (Sorted the programming languages alphabetically.)
 * 20:55, 1 October 2005 NevilleDNZ (move to end)
 * 20:54, 1 October 2005 NevilleDNZ ( →Algol 68 )
 * 00:21, 22 September 2005 GarethM ( →Delphi )
 * 00:15, 22 September 2005 GarethM (Fixed bug)
 * 00:05, 22 September 2005 GarethM (Replaced Delphi sort with a more general example)
 * 23:48, 21 September 2005 GarethM
 * 16:31, 21 September 2005 Roger Hui (slightly shorter version)
 * 16:16, 21 September 2005 Roger Hui ( →J )
 * 16:14, 21 September 2005 Roger Hui (added one-line J solution)
 * 07:02, 31 July 2005 141.154.236.32 ( →Scheme )
 * 19:30, 8 July 2005 84.184.217.163
 * 19:06, 6 July 2005 Macrakis (remove NGL (non-notable))
 * 13:13, 19 June 2005 Jenblower (Spelling correction)
 * 15:55, 13 June 2005 66.63.147.21 ( →Matlab )
 * 15:08, 11 June 2005 66.27.115.141 ( →Matlab )
 * 14:54, 11 June 2005 66.27.115.141 ( →Matlab )
 * 14:52, 11 June 2005 66.27.115.141 ( →Matlab )
 * 14:52, 11 June 2005 66.27.115.141 ( →Matlab )
 * 21:14, 3 June 2005 Yfrank (added zilog z80 implementation)
 * 19:59, 24 May 2005 MattWright
 * 15:06, 17 May 2005 208.226.132.3 ( →Nuphanation )
 * 15:03, 17 May 2005 208.226.132.3 ( →J )
 * 05:39, 29 April 2005 69.81.62.118 (Added PHP code from quicksort page)
 * 04:07, 31 March 2005 Dcoetzee ( →C - Add updates from main quicksort article)
 * 07:42, 30 March 2005 ShadowLord ( →C - Fixed calling of sort function by qsort function for datatype implementation (cmp was in wrong place))
 * 09:49, 21 March 2005 Fredrik ( →C )
 * 20:09, 13 March 2005 Dcoetzee ( →C - Merged yet-another-C-quicksort from quicksort)
 * 20:08, 13 March 2005 Dcoetzee ( →C - Merged yet-another-C-quicksort from quicksort)
 * 23:10, 12 March 2005 Dcoetzee (Weaken intro wording a bit)
 * 22:26, 12 March 2005 Dcoetzee ( →Java - Merge Java 5 example from quicksort)
 * 18:30, 6 March 2005 Vexis (Fixed link to SML page.)
 * 23:56, 4 March 2005 4.26.108.34 ( →SML )
 * 23:28, 4 March 2005 4.26.108.34 ( →SML )
 * 23:25, 4 March 2005 4.26.108.34 ( →SML )
 * 23:24, 4 March 2005 4.26.108.34 ( →SML )
 * 23:20, 4 March 2005 4.26.108.34 ( →Prolog )
 * 23:14, 4 March 2005 4.26.108.34 ( →SML )
 * 20:53, 2 March 2005 128.151.142.9 (Add a C implementation that isn't int-specific.)
 * 22:01, 19 February 2005 Panzi (added Visual Basic source)
 * 20:42, 16 February 2005 66.54.249.44
 * 05:17, 4 January 2005 66.92.29.144 (Moved scheme immediately after Lisp, and C++ after C)
 * 14:50, 21 December 2004 213.65.121.68
 * 20:25, 9 December 2004 132.161.33.188 ( →Haskell )
 * 21:12, 27 November 2004 Solipsist ( →ARM assembly language - fixing ARM link after move)
 * 00:43, 21 November 2004 Dcoetzee (Moved all from quicksort, added intro)

Talk:Quicksort implementations edit history

 * 15:13, 15 February 2006 Donhalcon ( →Encyclopedic content? )
 * 06:44, 15 February 2006 Dcoetzee ( →Encyclopedic content? )
 * 05:17, 15 February 2006 Donhalcon (Encyclopedic content?)
 * 10:21, 28 January 2006 Allan McInnes ( →Wikisource - compromises are good)
 * 08:36, 28 January 2006 Dcoetzee ( →Bad History? )
 * 08:33, 28 January 2006 Dcoetzee ( →Wikisource )
 * 08:02, 28 January 2006 Roger Hui
 * 08:01, 28 January 2006 Roger Hui
 * 07:59, 28 January 2006 Roger Hui
 * 06:08, 28 January 2006 Allan McInnes ( →Wikisource - sample implementations)
 * 04:25, 28 January 2006 Dcoetzee ( →Wikisource )
 * 03:16, 28 January 2006 Allan McInnes (Wikisource)
 * 21:23, 9 January 2006 86.145.195.148 ( →I've discovered an optimization )
 * 03:17, 6 December 2005 128.2.164.232
 * 16:12, 29 November 2005 128.2.164.232
 * 15:15, 9 June 2005 CarlosHoyos ( →I've discovered an optimization )
 * 11:02, 9 June 2005 Comocomocomocomo ( →Comprehensible, iterative, stack-safe version (though longish) - O(N) -&gt; O(n))
 * 10:56, 9 June 2005 Comocomocomocomo ( →Comprehensible, iterative, stack-safe version (though longish) )
 * 19:49, 8 June 2005 Dcoetzee ( →Comprehensible, iterative, stack-safe version (though longish) )
 * 16:37, 8 June 2005 Comocomocomocomo (Comprehensible, iterative, stack-safe version (though longish))
 * 16:15, 8 June 2005 Comocomocomocomo ( →I've discovered an optimization )
 * 21:06, 3 June 2005 Dcoetzee ( →I've discovered an optimization )
 * 12:41, 22 May 2005 CarlosHoyos ( →I've discovered an optimization )
 * 09:55, 22 May 2005 CarlosHoyos
 * 23:46, 21 May 2005 Dcoetzee ( →I've discovered an optimization )
 * 21:48, 20 May 2005 CarlosHoyos ( →I've discovered an optimization )
 * 13:10, 20 May 2005 CarlosHoyos ( →I've discovered an optimization )
 * 08:57, 19 May 2005 Dcoetzee ( →I've discovered an optimization )
 * 22:59, 16 May 2005 CarlosHoyos (I've discovered an optimization)
 * 02:39, 7 May 2005 Dcoetzee ( →C: Built-in Quicksort function )
 * 08:07, 4 May 2005 210.6.198.187 (C: Built-in Quicksort function)
 * 01:33, 12 April 2005 129.206.107.43
 * 01:06, 21 February 2005 Dcoetzee ( →Sorted? )
 * 22:04, 19 February 2005 Panzi (is code sorted?)
 * 05:09, 2 February 2005 64.168.148.158 (C example incorrect.)

Talk:Quicksort implementations text

 * The following text is from a Wikipedia talk page. Remember that Wikibooks is not Wikipedia, so the current module at Wikibooks does not necessarily need to be encyclopedia content.

Add? DualPivotQuickSort Sep.2009
Should the DualPivotQuicksort algorithm invented by Vladimir Yaroslavskiy (Jon Bentley, and Joshua Bloch) listed here too? It became part of oracles java SE 7 as the default sort algorithm of arrays

See  

Sorted?
"... sorted by number of non-comment lines of code. ..." Is that true? Not for the last two i gues. -- Panzi 22:04, 19 Feb 2005 (UTC)
 * People add things without sorting them, as one might expect. Feel free to either reorder them or remove the statement about how they're sorted. Deco 01:06, 21 Feb 2005 (UTC)

Also, the C++ example won't work. It needs stable_partition or something. See 

C: Built-in Quicksort function
In C (and thus also C++) there is a built-in Quicksort implementation called " ", as from. Should this be mentioned? --210.6.198.187 08:07, 4 May 2005 (UTC)
 * I don't think the qsort function is technically required by the standard to use quicksort, despite its name, but sure, maybe. Deco 02:39, 7 May 2005 (UTC)

I've discovered an optimization
I've discovered an optimization for the standard quicksort algorithm that could be useful in many of these implementations. I have tested it to be sure it works (other optimizations I thought about were in fact slower, because they added cache faults). I didn't got the idea from any other site, it's just my personal work, and I would want to release the information as GNU, appearing as a contributor.

It's very late at night, so I can't talk more about it. I'll recheck this page in a few days to see if there's someone interested. It's my first time here, so if someone could help me explaining what steps I should take, it would be great. I don't know HTML (yet), if that's important. If there's something I should learn first, please tell me so.

Thank you!
 * Well, this page is for discussing this article, but if you have a good idea that's great. I would be advised though that a lot of really smart people have put time into engineering fast quicksort implementations and you're quite possibly rediscovering something or missing something. You're also a little confused about software licenses. The GNU software license is called the GPL, and you can only release documents such as source code under it, not algorithms or ideas (these can be patented, but this isn't usually worthwhile). If you want to tell everybody about it, though, you could try a newsgroup like comp.programming. Deco 08:57, 19 May 2005 (UTC)


 * Maybe someone else discovered it before me, but the optimization doesn't appear on this article, so I'm free to post it as mine, and allow anyone to modify it. I'm aware of the GPL, and I was referring to the source code, and the text I posted on the other article explaining how it worked.

Here you have a simple C source code, which only sorts arrays of integers with the 'less than' comparison:

//Quicksort Implementation without swap by DrJones void quicksort(int *start, int *end) { if (start<end) { MEDIAN(*start, start[1], *end); register int pivot=start[1]; register int *i=start+1; register int *j=end; for { while (pivot<*--j) ; if (j<=i) break; //j may be lower than i                *i=*j; while (*++i<pivot) ; if (j<=i) { i=j; break; //i may be lower than j, as the test comparison comes after the 'while' }                *j=*i; }           *i=pivote; quicksort(start, i-1); quicksort(i+1, end); } //end of 'if(start<end)' } //end of method

Some points:


 * MEDIAN(a,b,c) is not described here, it's a function that put on position 'a' the lowest of the three elements, then put on position 'c' the highest of the elements, and the remaining element on position b.
 * This code can be improved, but I chose not to do it, so I could focus on the replacement for the swap method. Feel free to post a code which uses all optimizations available.
 * The code with the swap method could be as follows:

//Quicksort Implementation with swap by DrJones void quicksort(int *start, int *end) { if (start<end) { register int pivot=start[1]; //though it could be any, I suggest (start+end)/2 register int *i=start; register int *j=end; while (i<j) { while (pivot<*j) --j; while (*i<pivot) ++i; swap(i, j); i++; j--; }           quicksort(start, i-1); quicksort(i+1, end); } //end of if } //end of method


 * I'd say you have an infinite loop there. It will happen when the pivot value appears more than once. When i and j reach those elements, the conditions of the inner loops won't be met any more; i and j will not change, and the two pivots will be swapped once and again... forever --Comocomocomocomo 16:15, 8 Jun 2005 (UTC)


 * Yes, you are right. I made a quick fix, but I haven't tried if it works yet. DrJones 15:15, 9 Jun 2005 (UTC)

I wrote back the code. I hope you can understand it, I'm not used to write these things. DrJones 09:55, 22 May 2005 (UTC)


 * You don't have to remove anything from here if you don't want to. Most of our policies don't apply to talk pages. Deco 23:46, 21 May 2005 (UTC)

I finally found a webpage that could be interesting: http://linux.wku.edu/~lamonml/algor/sort/quick.html

At this time, it's down, so I had to read the page on Google's Cache. It uses the method I found instead of swapping, but it has to do a lot more comparisons in its inner loop than mine, to prevent one index surpassing the other.

It seems my code could be included on the main page, afterall.

Here you got a macro function for the MEDIAN function, if you want.

#define SWAP(x,y) register int temp = (x); (x) = (y); (y) = temp #define MEDIAN(a, b, c) \ if (c<b) { SWAP(b, c); } \ if (b<a) { SWAP(a, b); \ if (c<b) { SWAP(c, b); } \ }

This MEDIAN macro works exactly as the insertion algorithm for 3 elements. I wrote it using a SWAP macro for clarity, but I feel it's weird using the SWAP function here, when I'm not using it on the Inner loop. Other thing I don't know if would be an enhacement, is using for instead of while(1). I'm going to write it on the code anyways. DrJones 12:41, 22 May 2005 (UTC)
 * There are very no modern compilers which would compile for differently from while(1). Deco 21:06, 3 Jun 2005 (UTC)


 * That converging pointer method of partitioning was first invented by R Sedgewick, according to "The Art of Computer Programming". -BadJim

Comprehensible, iterative, stack-safe version (though longish)
What do you think about this one?

Most implementations use function recursion. In the worst case (where the chosen pivot value allways happens to be the minimum or the maximun) the maximun recursion level will be O(n). I propose using a stack array in order to apply tail recursion in the critical recursive call. --Comocomocomocomo 16:37, 8 Jun 2005 (UTC)
 * Explicit stacks are not necessary to implement the partially tail recursive variant. It suffices to use a loop and only make a recursive call on the smaller of the two sublists, as the pseudocode here shows. On the other hand, explicit stacks can save a significant amount of time/space overhead because of all the other junk that standard calling conventions tend to throw on the stack; it's a good idea to use one in an efficient implementation, but (no offense) not particularly innovative. Deco 19:49, 8 Jun 2005 (UTC)


 * Don't worry, I'm not trying to invent anything new. I just want to write a fast implementation (pivot taken from the middle or from a pos. at random, minimun number of swap operations...) and make it comprehensible. I intentionally discarded other refinements (general implementation with pointers to functions or C++ templates, saving push/pop operations...). So, my question is: do you find it easy to understand? (Not only for expert programmers, but for beginners too) If not, what would you change? --Como 10:56, 9 Jun 2005 (UTC)

Wikisource
Wouldn't this article be better placed at Wikisource? Wikipedia already has a Quicksort article, and unlike Wikipedia, Wikisource is intended to be a repository of things like sample implementations. They already have a Wikisource:Quicksort page - it could easily be expanded with the content of this article. --Allan McInnes 03:16, 28 January 2006 (UTC)
 * That's a possibility, but it would make it impossible to do the transclusion trick where we include a few of the implementations in both quicksort and this page. I've proposed elsewhere that there should be a project for code with a suitable license, but that got nowhere. Deco 04:25, 28 January 2006 (UTC)


 * Ah. Then maybe the question I should have asked was whether or not Wikipedia should include sample implementations of algorithms at all. IMHO it would be more appropriate to point to external sites that carry sample implementations, instead of having (unverified, potentially buggy) sample implementations of every algorithm, in every language. Unfortunately, I suspect that trying to achieve a consensus answer to the question of sample implementations is likely to lead into the same kind of interminable debate that the Wikipedia:Wikicode proposal provoked... --Allan McInnes 06:08, 28 January 2006 (UTC)


 * I moved these to here to provide a way of removing large amounts of content from the Quicksort article without upsetting the innumerable contributors who inexplicably enjoy adding sample implementations. I think it's alright as long as it's separated from the main content and its purpose is clear. It might be compared to lists of minor characters from a TV series or video game or Notable lines in the Star Wars series. Incidentally, I'm glad you're familiar with my ill-fated proposal. 08:33, 28 January 2006 (UTC)


 * Oh, I agree with your approach. It seems like a reasonable compromise, given the small likelihood that I could ever get consensus for my own personal views on the matter. And I certainly think it's better to have these things on a separate page from the main article. I just wanted to get my opinion "on record" so to speak :-)
 * Going slightly off on a tangent: I thought wikicode seemed like a reasonable proposal, and I may well have supported if I'd been a contributor when the debates were going on. Much like this article, it's not what I would consider ideal (the curly braces bug me a bit for starters), but after reading through some of the debate it seems like the best compromise between the various viewpoints. I applaud your ability to find workable solutions! I'll most likely end up using wikicode or some close derivative of it for any pseudocoding I need to do here. I may even see if I can get Wikipedia:WikiProject Computer science to adopt it as part of some kind of style guideline. --Allan McInnes 10:21, 28 January 2006 (UTC)

Bad History?
I am puzzled that the history of the Quicksort Implementations page seems to be corrupted. For example, compare the following two versions:


 * 2005-11-18 21.42.19 (PST) DCoetzee - A less obtuse J example
 * 2005-11-28 19.25.16 (PST) DCoetzee - Mathematica - a test driver

I am pretty sure that at that time the "Core implementations" contains a "obtuse J example". The comment in the J example (the comment is still there in the current revision) also indicates that at that time, there was a J example in the "Core implementations". Yet I can not find any revision of the page that has the J example in the Core implementations. Roger Hui 08:02, 28 January 2006 (UTC)
 * You're looking at the wrong page. Mere hours ago, editor R. Koot removed swathes of content from the core implementations template. Here's how it looked before. I'm reverting it. Deco 08:36, 28 January 2006 (UTC)

Encyclopedic content?
WP:WWIN#Wikipedia is not a mirror or a repository of links, images, or media files says, explicitly, that wikipedia is not:


 * "Mere collections of public domain or other source material such as entire books or source code..."

This page looks an awful lot like a mere collection of source code; is it really encyclopedic in nature? If so, what is its importance? It seems to me that quicksort implementations belong somewhere else, like the Great Compiler Shootout or Sourceforge. --bmills 05:17, 15 February 2006 (UTC)


 * Or Wikisource. The only problem is that it makes it harder to share the implementations at the top with the quicksort page if they're on another site. I created the article originally to get implementations out of the quicksort article, which people feel compelled to add implementations to. Deco 06:44, 15 February 2006 (UTC)


 * Isn't that the point? An encyclopedia article on Quicksort shouldn't contain any more implementations than required to describe the algorithm.  If people feel compelled to add them, they should add them to a code repository, not Wikipedia.  --bmills 15:13, 15 February 2006 (UTC)

Why is this here?
@The first C implementation above does not sort the list properly if the initial input is a reverse sorted list, or any time in which the pivot turns out be the largest element in the list:

If it's not a proper quicksort, then why is it here? It should be fixed or removed. 82.139.85.118 01:21, 8 May 2006 (UTC)


 * Agreed, if it is failed, why is it here? Rursus (discuss • contribs) 18:54, 3 December 2011 (UTC)

what on earth??
why do you choose really random pivot.. choose the first one.

Python 3K
I'm just wondering if the Python version runs under Py3K. I haven't got Py3K up and running on any boxes yet, and I haven't worked with Python for a while, so I was wondering if anyone knew? Tinned Tuna (talk)

Bug in C implementation
The above code from the first C implementation doesn't work. The rightmost element is (end-size), not end.

A simple fix is to change

sort((char*)array, size, cmp, 0, nitems*size); to sort((char*)array, size, cmp, 0, (nitems-1)*size);