PHP merge sorting (merge sorting)-algorithm principle analysis

Source: Internet
Author: User
: This article mainly introduces PHP merge sorting (merge sorting)-algorithm principle analysis. if you are interested in PHP tutorials, please refer to it. Merge sorting: The time complexity is ~ O (nlogn) -- also known as merge sorting

The Merge sorting method combines two (or more) ordered tables into a new ordered table,

That is, the sequence to be sorted is divided into several ordered subsequences, and then the ordered subsequences are merged into the overall ordered sequence.

 StableSort ($ arrStoreList, function ($ a, $ B) {// function ($ a, $ B) anonymous function return $ a <$ B ;}); // the static call method is also optional./* Merge_sort: stableSort ($ arrStoreList, function ($ a, $ B) {return $ a <$ B ;}); */print_r ($ arrStoreList); class Merge_sort {public static function stableSort (& $ array, $ cmp_function = 'strcmp ') {// use the merge sort self :: mergeSort ($ array, $ cmp_function); return;} public static function mergeSort (& $ array, $ cmp_function = 'strcmp ') {// Arrays of size <2RequireNo action. if (count ($ array) <2) {return;} // Split the array in half $ halfway = count ($ array)/2; $ array1 = array_slice ($ array, 0, $ halfway); $ array2 = array_slice ($ array, $ halfway); // Recurse to sort the two halves self: mergeSort ($ array1, $ cmp_function); self:: mergeSort ($ array2, $ cmp_function); // If all of $ array1 is <= all of $ array2, just append them. // array1 and array2 are in different order. arra needs to be compared The size of the last element of y1 and the first element of array2 if (call_user_func ($ cmp_function, end ($ array1), $ array2 [0]) <1) {$ array = array_merge ($ array1, $ array2); return;} // combine two ordered arrays into an ordered array: merge the two sorted arrays into a single sorted array $ array = array (); $ ptr1 = $ ptr2 = 0; while ($ ptr1 <count ($ array1) & $ ptr2 <count ($ array2) {if (call_user_func ($ cmp_function, $ array1 [$ ptr1], $ array2 [$ ptr2]) <1) {$ array [] = $ ar Ray1 [$ ptr1 ++];} else {$ array [] = $ array2 [$ ptr2 ++];} // Merge the remainder while ($ ptr1 <count ($ array1) {$ array [] = $ array1 [$ ptr1 ++];} while ($ ptr2 <count ($ array2) {$ array [] = $ array2 [$ ptr2 ++];} return ;}}?>

Output result: Array ([0] => 5 [1] => 4 [2] => 3 [3] => 2 [4] => 1)

Algorithm principle analysis: The key is to understand the principle of recursive calls and return functions.


The above introduces PHP merge sorting (merge sorting)-algorithm principle analysis, including The require content, hope to be helpful to friends who are interested in PHP tutorials.

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.