Merge sort of PHP sorting algorithm (merging sort)

Source: Internet
Author: User
This article mainly introduced the PHP sorting algorithm merge sort (merging sort), combined with the example form detailed analysis of the PHP merge sort principle, the definition, the use method and the related operation attention matters, the need friend can refer to the next

In this paper, we describe the merging sort of PHP sorting algorithm (merging sort). Share to everyone for your reference, as follows:

Basic idea:

Merge sort: It is to use the idea of merging (merging) to realize the sorting method. Its principle is that the initial sequence contains n elements, it can be regarded as n ordered sub-sequence, each subsequence length is 1, and then 22 merges, to get ⌈n/2⌉ (⌈x⌉ means the smallest integer not less than x) of the length of 2 or 1 ordered sequence; then 22 merge, and so heavy Until a sequential sequence of length n is obtained, this sort method becomes a 2-way merge sort.

The process of merging:

A[i] Take the first part of a array (already sorted), A[j] take the back part of a array (already sorted)

R Arrays Store well-ordered a arrays

Compare the size of a[i] and a[j], and if A[I]≤A[J], copy the element A[i] from the first ordered table to R[k], and add 1 for I and K, or copy the elements from the second ordered table to A[j], and make J and K add 1 respectively , so loop until one of the ordered tables is finished, and then copy the remaining elements from the other ordered table to the cells in R from subscript K to subscript t. Merging sorting algorithm we usually use recursive implementation, first to sort the interval [s,t] to the midpoint of the two points, then the left sub-range, then the right sub-range is sorted, and finally the left and right intervals with a merge operation into an orderly interval [s,t].

Second, merge operation:

Merge operation (merge), also called merge algorithm, refers to the method of merging two sequential sequences into a sequential sequence.

If there is a series {6,202,100,301,38,8,1}

Initial state: 6, 202, 100, 301, 38, 8,1

After first merge: {6,202},{100,301},{8,38},{1}, number of comparisons: 3;

After the second merge: {6,100,202,301},{1,8,38}, number of comparisons: 4;

After the third merge: {1,6,8,38,100,202,301}, number of comparisons: 4;

The total number of comparisons is: 3+4+4=11,;

The number of reverse order is 14;

Third, the algorithm description:

The merge operation works as follows:

The first step: the space is applied to the sum of two sorted sequences, which is used to store the merged sequence

Step two: Set two pointers, the initial position is the starting position of two sorted series

Step three: Compare the elements pointed to by two pointers, select a relatively small element into the merge space, and move the pointer to the next position

Repeat step 3 until a pointer exceeds the end of the sequence

Copies all remaining elements of another sequence directly to the end of the merge sequence

Algorithm implementation:

Let's take a look at the main function section first:

Swap functions function Swap (array & $arr, $a, $b) {  $temp = $arr [$a];  $arr [$a] = $arr [$b];  $arr [$b] = $temp;} Merge algorithm total functions function mergesort (array & $arr) {  $start = 0;  $end = count ($arr)-1;  Msort ($arr, $start, $end);}

In the general function, we only call a Msort () function, because we want to use recursive calls, so we wrap the Msort ().

Now let's look at the MSort() function:

function Msort (array & $arr, $start, $end) {  //when the subsequence length is 1 o'clock, $start = = $end, no more grouping  if ($start < $end) {    $mid = Floor (($start + $end)/2); Divide the $arr into $arr [$start-$mid] and $arr [$mid +1-$end]    msort ($arr, $start, $mid);  Merge $arr [$start-$mid] into ordered $arr[$start-$mid]    msort ($arr, $mid + 1, $end);  Merges $arr [$mid +1-$end] into an orderly $arr [$mid +1-$end] Merge ($arr, $start, $mid, $end    );    Merge $arr[$start-$mid] part and $arr[$mid +1-$end] into an orderly $arr[$start-$end]  }}

The above MSort() function implementation will divide the array into half and half (until the subsequence length is 1), and then merge the subsequence together.

Now is our merge operation function Merge() :

Merging operation function Merge (array & $arr, $start, $mid, $end) {  $i = $start;  $j = $mid + 1;  $k = $start;  $temparr = Array ();  while ($i! = $mid +1 && $j! = $end + 1)  {    if ($arr [$i] >= $arr [$j]) {$temparr [$k      + +] = $arr [$j + +];    }    else{      $temparr [$k + +] = $arr [$i + +];    }  }  Add the remainder of the first subsequence to the already ordered $temparr array  while ($i! = $mid + 1) {    $temparr [$k + +] = $arr [$i + +];  }  Add the remainder of the second subsequence to the already ordered $temparr array  while ($j! = $end + 1) {    $temparr [$k + +] = $arr [$j + +];  }  for ($i = $start; $i <= $end; $i + +) {    $arr [$i] = $temparr [$i];  }}

When we get here, we're done with the merging algorithm. We call to try:

$arr = Array (9,1,5,8,3,7,4,6,2); MergeSort ($arr); Var_dump ($arr);

Operation Result:

Array (9) {[0]=> int (1) [1]=> int (2) [2]=> int (3) [3]=> int (4) [4]=> int (5) [5]=> int (6) [6]=> Int (7) [7]=> Int (8) [8]=> int (9)}

Analysis of Complexity:

Because the merging algorithm is grouped and compared whether the original sequence is ordered or not, its best, worst, and average time complexity is O (NLOGN).

The merging algorithm is a kind of stable sorting algorithm.

This article refers to from the "Big Talk Data Structure", in this only record, convenient to consult later, great God do not spray!

Related Article

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.