What sorting algorithms do PHP usort use?

I want to sort files by modification time in ascending and descending order.

According to this answer It seems that the best way is to achieve a definition of the sort callback function and use usort / uasort.

However, due to the nature of my application, I will most likely come across some worst-case scenarios for some sorting algorithms (for example, an almost inverse ordered input sequence).

Since each comparison uses two file system accesses that are partially associated with network drives, the number of comparisons is critical and should be kept to a minimum. Other types of iterations may be more.

So what sorting algorithms use the PHP array sorting functions? Quicksort? Multisort? Is there any way to configure this?

Should I shuffle the array before sorting?

Or do I need to write my own implementation?

Do you know some good libraries that provide sorting functions with custom algorithms?

What algorithm or methods to solve this problem of minimizing comparisons would you recommend?

+6
sorting algorithm php
source share
2 answers

In php.net/sort I found this:

Note. Like most PHP sort functions, sort () uses the "Quicksort" implementation.

I believe that it uses randomized quicksort , so there is no need to shuffle the array.

I did some tests , and quicksort PHP is not randomized, so shuffle your input array !!

+14
source share

I did some searches using PHP OpenGrok , and only based on some look at function names and scroll code, it seems like usort is implemented with quick sort.

To minimize file system calls, do them once for each element in your array and save the results in another array. Use this second array in your comparator function instead of repeated calls.

+6
source share

All Articles