39

Internally speaking, which algorithm(s) does PHP use to implement the various sort functions it offers? It seems like the usort variants might use a different algorithm than the built in sorts, but I wanted to know.

Where would I even find this information?

Thanks!

Joe Mastey
  • 26,809
  • 13
  • 80
  • 104

3 Answers3

46

You could find the information by looking at the php manual. http://php.net/sort says PHP uses an implementation of Quicksort. Failing that, you could always trudge through the PHP source code itself.

alex
  • 479,566
  • 201
  • 878
  • 984
Timothy
  • 4,630
  • 8
  • 40
  • 68
21

For sorting, PHP uses an implementation of quicksort that can be found in Zend/zend_sort.c, which takes a comparison function and an array of elements. The default comparison function for sort() is defined in ext/standard/array.c and is called php_array_data_compare(). So basically, it's the same algorithm for all sorting functions, except that they take different comparison functions.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Daniel Egeberg
  • 8,359
  • 31
  • 44
0

IIRC, PHP uses quick sort

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • 3
    Since Timothy "ninja'ed" you in the FGITW game, and because this 5-word answer isn't found to be helpful by any researchers in over a decade, you might entertain the idea of dropping this post from the page. Your call. – mickmackusa Sep 11 '20 at 02:46