Ext/standard/php_array.h Https://github.com/php/php-src/blob/master/ext/standard/php_array.h
- #ifndef Php_array_h
- #define Php_array_h
- Php_minit_function (array);
- Php_mshutdown_function (array);
- Php_function (Ksort);
- Php_function (Krsort);
- Php_function (Natsort);
- Php_function (Natcasesort);
- Php_function (Asort);
- Php_function (Arsort);
- Php_function (sort);
- Php_function (Rsort);
- Php_function (Usort);
- Php_function (Uasort);
- Php_function (Uksort);
- ......
Copy CodeThe 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 functionfor simplicity, only the sort function is parsed : 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;
- Long sort_type = Php_sort_regular;
- if (Zend_parse_parameters (Zend_num_args () tsrmls_cc, "A|l", &array, &sort_type) = = FAILURE) {
- Return_false;
- }
- Php_set_compare_func (Sort_type tsrmls_cc);
- if (Zend_hash_sort (z_arrval_p (array), Zend_qsort, Php_array_data_compare, 1 tsrmls_cc) = = FAILURE) {
- Return_false;
- }
- Return_true;
- }
- /* }}} */
Copy CodeIn 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
- (*sort_func) (void *) artmp, I, sizeof (Bucket *), compar tsrmls_cc);
- Handle_block_interruptions ();
- Ht->plisthead = artmp[0];
- Ht->plisttail = NULL;
- Ht->pinternalpointer = ht->plisthead;
- Artmp[0]->plistlast = NULL;
- if (i > 1) {
- Artmp[0]->plistnext = artmp[1];
- for (j = 1; j < I-1; J + +) {
- Artmp[j]->plistlast = Artmp[j-1];
- Artmp[j]->plistnext = artmp[j+1];
- }
- Artmp[j]->plistlast = Artmp[j-1];
- Artmp[j]->plistnext = NULL;
- } else {
- Artmp[0]->plistnext = NULL;
- }
- Ht->plisttail = Artmp[i-1];
- Pefree (artmp, ht->persistent);
- Handle_unblock_interruptions ();
- if (renumber) {
- p = ht->plisthead;
- i=0;
- while (P! = NULL) {
- p->nkeylength = 0;
- P->h = i++;
- p = p->plistnext;
- }
- Ht->nnextfreeelement = i;
- Zend_hash_rehash (HT);
- }
Copy CodeIn 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) |