[What sort algorithm is used to discuss the C implementation within the function of the]php sort series?]

Source: Internet
Author: User
Tags sorts

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 sort function defined above:

Arsort--Reverse-sort the array and keep the index relationship Asort--sort the array and keep the index relationship Krsort--the array is reversed by the key name Ksort--Sorts the array by key name Natcasesort--using the "natural sort" algorithm to Sort Natsort of case-sensitive letters--sort by the "natural sort" algorithm for arrays Rsort--reverse sorting of arrays sort--sort an arrayUasort--Use a user-defined comparison function to sort the values in the array and keep the index associated uksort--sorts the key names in the array using a user-defined comparison function Usort--sorts the values in the array using a user-defined comparison function

for simplicity, only the sort function is parsed : https://github.com/php/php-src/blob/master/ext/standard/array.c

    1. /* {{proto bool sort (array &array_arg [, int sort_flags])
    2. Sort an array */
    3. Php_function (sort)
    4. {
    5. Zval *array;
    6. Long sort_type = Php_sort_regular;
    7. if (Zend_parse_parameters (Zend_num_args () tsrmls_cc, "A|l", &array, &sort_type) = = FAILURE) {
    8. Return_false;
    9. }
    10. Php_set_compare_func (Sort_type tsrmls_cc);
    11. if (Zend_hash_sort (z_arrval_p (array), Zend_qsort, Php_array_data_compare, 1 tsrmls_cc) = = FAILURE) {
    12. Return_false;
    13. }
    14. Return_true;
    15. }
    16. /* }}} */
Copy Code

In the code, you see the

if (Zend_hash_sort (z_arrval_p (array), Zend_qsort, Php_array_data_compare, ...).

The possibility of using a quick sort is large.

Continue the analysis.

Zend/zend_hash.c

Https://github.com/php/php-src/blob/master/Zend/zend_hash.c

  1. (*sort_func) (void *) artmp, I, sizeof (Bucket *), compar tsrmls_cc);
  2. Handle_block_interruptions ();
  3. Ht->plisthead = artmp[0];
  4. Ht->plisttail = NULL;
  5. Ht->pinternalpointer = ht->plisthead;
  6. Artmp[0]->plistlast = NULL;
  7. if (i > 1) {
  8. Artmp[0]->plistnext = artmp[1];
  9. for (j = 1; j < I-1; J + +) {
  10. Artmp[j]->plistlast = Artmp[j-1];
  11. Artmp[j]->plistnext = artmp[j+1];
  12. }
  13. Artmp[j]->plistlast = Artmp[j-1];
  14. Artmp[j]->plistnext = NULL;
  15. } else {
  16. Artmp[0]->plistnext = NULL;
  17. }
  18. Ht->plisttail = Artmp[i-1];
  19. Pefree (artmp, ht->persistent);
  20. Handle_unblock_interruptions ();
  21. if (renumber) {
  22. p = ht->plisthead;
  23. i=0;
  24. while (P! = NULL) {
  25. p->nkeylength = 0;
  26. P->h = i++;
  27. p = p->plistnext;
  28. }
  29. Ht->nnextfreeelement = i;
  30. Zend_hash_rehash (HT);
  31. }
Copy Code

In the algorithm, faster than the fast sort is no doubt the Cardinal sort, a cursory look at the algorithm, may be the basis of sorting in the hash bucket .

Bucket sequencing is a stable bucket sort is the fastest one in a common sort, faster than a fast line ... In most cases, the buckets are sorted very quickly, but at the same time they are very space-consuming (change time with space
Used, which, PHP
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.