Home  >  Article  >  Backend Development  >  [Discussion] Which sorting algorithm is used in the C implementation of the PHP sorting series functions?

[Discussion] Which sorting algorithm is used in the C implementation of the PHP sorting series functions?

WBOY
WBOYOriginal
2016-07-25 08:46:311079browse

ext/standard/php_array.h

https://github.com/php/php-src/blob/master/ext/standard/php_array.h

  1. #ifndef PHP_ARRAY_H
  2. #define PHP_ARRAY_H
  3. PHP_MINIT_FUNCTION(array);
  4. PHP_MSHUTDOWN_FUNCTION(array);
  5. PHP_FUNCTION(ksort);
  6. PHP_FUNCTION(krsort);
  7. PHP_FUNCTION(natsort);
  8. PHP_FUNCTION(natcasesort);
  9. PHP_FUNCTION(asort);
  10. PHP_FUNCTION(arsort);
  11. PHP_FUNCTION(sort);
  12. PHP_FUNCTION(rsort);
  13. PHP_FUNCTION(usort);
  14. PHP_FUNCTION(uasort);
  15. PHP_FUNCTION(uksort);
  16. ...
Copy code

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 function

For 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 */
Php_function (sort) {
    zval *array;
  1. long sort_type = p Hp_sort_regular;
  2. if (zend_parse_parameters (ZEND_NUM_ARGS() TSRMLS_CC, "a|l", &array, &sort_type) == FAILURE) {
  3. RETURN_FALSE;
  4. }
  5. php_set_compare_func(sort_type TSRMLS_CC);
  6. if (zend_hash_sort(Z_ARRVAL_P(array), zend_q sort, php_array_data_compare, 1 TSRMLS_CC) == FAILURE) {
  7. RETURN_FALSE;
  8. }
  9. RETURN_TRUE;
  10. }
  11. /* }}} */
  12. Copy code
In the code, I saw
if (zend_hash_sort(Z_ARRVAL_P(array), zend_qsort, php_array_data_compare, ……
High possibility of using quick sort.

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();
ht->pListHead = arTmp[0];
    ht-> pListTail = NULL;
  1. ht->pInternalPointer = ht->pListHead;
  2. arTmp[0]->pListLast = NULL;
  3. if (i > 1) {
  4. arTmp[0]->pListNext = arTmp [1];
  5. for (j = 1; j < i-1; j++) {
  6. arTmp[j]->pListLast = arTmp[j-1];
  7. arTmp[j]->pListNext = arTmp[ j+1];
  8. }
  9. arTmp[j]->pListLast = arTmp[j-1];
  10. arTmp[j]->pListNext = NULL;
  11. } else {
  12. arTmp[0]->pListNext = NULL;
  13. }
  14. ht->pListTail = arTmp[i-1];
  15. pefree(arTmp, ht->persistent);
  16. HANDLE_UNBLOCK_INTERRUPTIONS();
  17. if (renumber) {
  18. p = ht-> pListHead;
  19. i=0;
  20. while (p != NULL) {
  21. p->nKeyLength = 0;
  22. p->h = i++;
  23. p = p->pListNext;
  24. }
  25. ht-> nNextFreeElement = i;
  26. zend_hash_rehash(ht);
  27. }
  28. Copy code
Among the algorithms, the one that is faster than quick sort is undoubtedly the radix sort. After a cursory look at the algorithm, it may be the
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 (
Exchange space for time

)

What kind of php was used
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn