Home >Backend Development >PHP Tutorial >[Discussion] Which sorting algorithm is used in the C implementation of the PHP sorting series functions?
ext/standard/php_array.h https://github.com/php/php-src/blob/master/ext/standard/php_array.h
The sorting function defined above: arsort -- Sort an array in reverse order while maintaining index relationships asort – Sort an array while maintaining index relationships krsort -- Sort an array in reverse order by key name ksort -- Sort an array by key natcasesort -- Sort an array in a case-insensitive manner using the "natural sort" algorithm natsort -- Sort an array using the "natural sorting" algorithm rsort -- Sort an array in reverse order sort -- Sort the array uasort – Sort values in an array using a user-defined comparison function and maintain index association uksort -- Sort keys in an array using a user-defined comparison function usort – Sort values in an array using a user-defined comparison functionFor simplicity, only analyze the sort function: https://github.com/php/php-src/blob/master/ext/standard/array.c {/ *{{Proto Bool Sort (Array & Array_arg [, Int Sort_flags]) Sort an array */ High possibility of using quick sort. Php_function (sort) {
if (zend_hash_sort(Z_ARRVAL_P(array), zend_qsort, php_array_data_compare, ……
Continue to analyze.
Zend/zend_hash.c https://github.com/php/php-src/blob/master/Zend/zend_hash.c
(*sort_func)((void *) arTmp, i, sizeof(Bucket *), compar TSRMLS_CC); HANDLE_BLOCK_INTERRUPTIONS(); Exchange space for time ht->pListHead = arTmp[0]; hash bucket sort in the basic sort.
Bucket sorting is stable
Bucket sort is the fastest kind of common sort, faster than quick sort...in most cases
Bucket sorting is very fast, but it also consumes a lot of space (
) What kind of php was used |