JIYIK CN >

Current Location:Home > Learning > ALGORITHM >

The road to learning sorting algorithms - merge sort

Author:JIYIK Last Updated:2025/03/19 Views:

Let's first look at the definition of merge sort

Merge sort is an effective sorting algorithm based on the merge operation. This algorithm is a very typical application of the Divide and Conquer method. Merge the ordered subsequences to obtain a completely ordered sequence; that is, first make each subsequence ordered, and then make the subsequence segments ordered. If two ordered lists are merged into one ordered list, it is called a two-way merge.

Simply put, it merges two ordered lists into one ordered list.

Let's first understand the process of merge sort through the following figure.

Merge sort process

Let's look at the steps of how to decompose and then merge

  1. Apply for space whose size is the sum of the two sorted sequences. This space is used to store the merged sequence.
  2. Set two pointers, the initial positions of which are the starting positions of the two sorted sequences.
  3. Compare the elements pointed to by the two pointers, select the relatively small element and put it into the merge space, and move the pointer to the next position
  4. Repeat step 3 until a pointer reaches the end of the sequence
  5. Copy all remaining elements of the other sequence directly to the end of the merged sequence

Finally, we implemented the merge sort algorithm using PHP

function Merge($arr,$l,$m,$r){
    $t = $arr;
    $start = $l;
    $end = $m+1;
    while($l<=$r){
        if($l>$m||$end>$r) break;
        if($arr[$l]<$arr[$end]){
            $t[$start++] = $arr[$l++];
        }else{
            $t[$start++] = $arr[$end++];
        }
    }
    if($l<=$m){
        $s = $l;
        $e = $m;
    }elseif($r>=$end){
        $s = $end;
        $e = $r;
    }
    while($s<=$e){
        $t[$start++] = $arr[$s++];
    }
    $arr = $t;
    return $arr;
}
function MergeSort(&$arr,$l,$r){
   if($r <= $l) return ;
   $m = floor(($l+$r)/2);
   MergeSort($arr,$l,$m);
   MergeSort($arr,$m+1,$r);
   $arr = Merge($arr,$l,$m,$r);
}
$arr = array(10,6,8,23,4,1,17,56,32,50,11,9);
MergeSort($arr,0,count($arr)-1);
print_r($arr);

The above code is implemented using a recursive mechanism. Of course, it can also be implemented in a non-recursive form. You can refer to the article "Merge Sort (Non-recursive Implementation)" .

Using PHP to implement merge sort, according to the above code, the code is relatively cumbersome. PHP has its own characteristics, and can use less code to implement merge sort. However, the above code can better reflect the entire decomposition and merging process. So if you are learning the merge sort algorithm, it is recommended to use the above code. Although the code is cumbersome, it is very helpful for understanding the merge sort process.

For a more detailed merge sort algorithm, I have written a new article to fully understand the merge sort algorithm , which you can refer to.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:https://www.jiyik.com/en/xwzj/algorithm_9885.html

Related Articles

The road to learning sorting algorithms - quick sort

Publish Date:2025/03/19 Views:121 Category:ALGORITHM

Quick sort is a sorting algorithm developed by Tony Hall. On average, sorting n items requires O(n log n) comparisons. In the worst case, O(n2) comparisons are required, but this is uncommon. In fact, quick sort is often significantly faste

The road to learning sorting algorithms - heap sort

Publish Date:2025/03/19 Views:145 Category:ALGORITHM

Like other sorting algorithms, let's first look at the definition of heap sort. Heapsort : refers to a sorting algorithm designed using the heap data structure. A heap is a structure that approximates a complete binary tree and satisfies th

Common sorting algorithms

Publish Date:2025/03/19 Views:66 Category:ALGORITHM

This article introduces several commonly used sorting algorithms in software engineering. The core codes of all sorting algorithms are introduced in "Core Codes of Common Sorting Algorithms" 1. Insertion Sort The basic idea of ​​inserti

The road to learning sorting algorithms - selection sort

Publish Date:2025/03/19 Views:160 Category:ALGORITHM

Selection sort is a simple and intuitive sorting algorithm. Its basic idea is to select a maximum (or minimum) element in an unsorted sequence and put it at the end (note: this is the end of the unsorted sequence, which can be considered as

The road to learning sorting algorithms - bubble sort

Publish Date:2025/03/19 Views:80 Category:ALGORITHM

Bubble sort is also a simple and intuitive sorting algorithm. The idea is that it repeatedly visits the sequence to be sorted, compares two elements at a time, and swaps them if they are in the wrong order. The work of visiting the sequence

Core code of commonly used sorting algorithms

Publish Date:2025/03/19 Views:122 Category:ALGORITHM

In the article "Common Sorting Algorithms", we briefly introduced the ideas and implementation steps of various sorting algorithms. In this article, I will share the core codes of these sorting algorithms with you. The complete code of all

Learning the Sorting Algorithm - Radix Sorting (LSD)

Publish Date:2025/03/19 Views:177 Category:ALGORITHM

Radix Sort : It is a non-comparative integer sorting algorithm. The basic principle of radix sorting is to group integers according to the number of digits in the integer. In the grouping process, the missing digits are filled with 0. Accor

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial