PHP sorting algorithm series of the merge sort detailed

Source: Internet
Author: User
This article explains the collation of the PHP sorting algorithm series.

Merge sort

Merge sort (Merge-sort) is an efficient sorting algorithm based on merging operation, which is a very typical application of divide-and-conquer method (Divide and Conquer). The ordered Subsequence is merged to obtain a fully ordered sequence, i.e., the order of each subsequence is ordered, and then the sequence of sub-sequences is ordered. If two ordered tables are combined into an ordered table, they are called two-way merging.

Merge process

The core of the merge sort is how to merge two ordered sequences, assuming there are two ordered arrays, comparing the first element of two ordered arrays, who takes the small one, and puts the element in the third array, takes the element in the corresponding array, and so on, and so on, when taking an array that has no elements, You can add the remaining elements of another array directly to the third array.

Principle

1, the sequence of each adjacent two numbers are merged to form a ceil (N/2) sequence, sorted after each sequence contains two elements, the last sequence may have only one element.

2, the above sequence is merged again, form Ceil (N/4) sequence, each sequence contains four elements, the last sequence may have only three and the following elements.

3. Repeat step 2 until all the elements have been sorted.

Example

Sorting an array [53,89,12,6,98,25,37,92,5]

After the first merge

(53,89), 12, (6,98), (25,37), (5,92)

After the second merge

(12,53,89), (6,25,37,98), (5,92)

After the third merge

(6,12,25,37,53,89,98), (5,92)

After the fourth time merge

5,6,12,25,37,53,89,92,98

PHP Code implementation

function Merge_sort ($arr) {$length =count ($arr), if ($length <=1) {return $arr;}//decomposition array, recursive sort $half =ceil ($length/2); $ Arr2=array_chunk ($arr, $half); $left =merge_sort ($arr 2[0]); $right =merge_sort ($arr 2[1]); while (count ($left) &&count ($right)) {if ($left [0]< $right [0]) {$reg []=array_shift ($left);} else{$reg []=array_shift ($right);}} Return Array_merge ($reg, $left, $right); }

This article explains the PHP sorting algorithm series of merge sort to explain, plowing related content please concern PHP Chinese net.

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.