f



Sorting algorithm(s) used by PHP's sort function

Does anyone know what sorting algorithm(s) -- quicksort, mergesort,
radix sort, etc. -- does PHP use internally in its sort function?
0
skashyap (1)
7/25/2003 1:33:09 AM
comp.lang.php 32646 articles. 0 followers. Post Follow

1 Replies
960 Views

Similar Articles

[PageSpeed] 23

"Shaunak Kashyap" <skashyap@intertechmedia.com> wrote in message
news:67746bb.0307241733.63a5649f@posting.google.com...
> Does anyone know what sorting algorithm(s) -- quicksort, mergesort,
> radix sort, etc. -- does PHP use internally in its sort function?

There's a lot of sorting algorithms used by PHP.  It looks like the primary
array sorting method is quicksort.  I also found some use of mergesort.
Looking at the source, I found this:

<builddirectory>/ext/standard/array.c

This sets up the "hooks" for most of the array functions for use in Zend.

It looks like all of the sort methods rely on a sort_type == zend_qsort.
Doing a quick search for this in the rest of the source, returns this:

<builddirectory>/Zend/zend_qsort.c

If you look at this file, you can see the good ol' quick sort algorithm in
action.  If you prefer a different algorithm, I'm sure you could replace the
quicksort algorithm, recompile PHP, and whammo!

If you're interested in developing extensions to PHP, the API is pretty well
documented in the manual and in "Programming PHP" by Rasmus Lerdorf.  I wish
I had the time to tinker with PHP at this level.

Take care,
Zac


0
news210 (43)
7/25/2003 2:53:47 AM
Reply: