JIYIK CN >

Current Location:Home > Learning > ALGORITHM >

The road to learning sorting algorithms - merge sort (non-recursive implementation)

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

In the article "Merge Sort", we introduced the principles and operation steps of merge sort, and finally implemented the sorting algorithm using PHP code. In the program, we used the principle of recursion to implement the algorithm.

In fact, the underlying mechanism of recursive function is nothing more than the stack mechanism. In this article, we will not use recursive function, but directly use the stack mechanism to implement merge sort.

First, let's briefly introduce the basic principles of non-recursive implementation:

First we need to apply for two stacks - stack, stack1;

The first step is to push the starting position s, the end position e and the middle position m of the sequence to be sorted into stack1.

Step 2: Pop the data from stack1 and push the popped data into stack. Then determine whether s is less than m. If s is less than m, put s as the starting position, m as the end position, and (s+m)/2 as the middle position into stack1. Then determine whether m+1 is less than e. If m+1 is less than e, put m+1 as the starting position, e as the end position, and (m+1+e)/2 as the middle position into stack1.

Step 3: Check whether stack1 is empty. If it is empty, proceed to step 4. If it is not empty, repeat step 2.

Step 4: Pop the data from the stack and merge them according to the starting position, end position and middle position of the popped data (the merging process is the same as in the previous article).

Step 5: Check whether the stack is empty. If it is not empty, repeat step 4. If it is empty, the program ends.

Let's look at the implementation code

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){
    $stack = array();
    $stack1 = array();
    $temp = array(0,count($arr)-1,floor((0+count($arr)-1)/2));
    array_push($stack1,$temp);
    while(count($stack1)>0){
        $temp = array_pop($stack);
        array_push($stack,$temp);
        if($temp[0]<$temp[2]){
            array_push($stack1,array($temp[0],$temp[2],floor(($temp[0]+$temp[2])/2)));
        }
        if($temp[2]+1<$temp[1]){
            array_push($stack1,array($temp[2]+1,$temp[1],floor(($temp[2]+1+$temp[1])/2)));
        }
    }
    while(count($stack)>0){
        $temp = array_pop($stack);
        $arr = Merge($arr,$temp[0], $temp[2], $temp[1]);
    }
}
$arr = array(10,6,8,23,4,1,17,56,32,50,11,9);
MergeSort($arr);
print_r($arr);

Compared with the recursive method, this method has more code. However, considering the principle of merge sort, I personally think this method is easier to understand than the recursive method. After all, the implementation of recursion also relies on the stack mechanism. By directly operating the stack to implement the merge algorithm, we will have a deeper understanding of merge sort.

I have written another article about merge sort to give you a comprehensive understanding of the merge sort algorithm . It explains the splitting and merging process of merge sort in more detail, and the code includes both recursive and non-recursive implementations.

I hope this helps.

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:

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

The road to learning sorting algorithms - Radix Sort (MSD)

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

In the article "Radix Sort (LSD)" , we explained the concept and efficiency analysis of radix sort. We will not repeat it in this article. You can refer to that article to get a general understanding of the idea of ​​radix sort. Next, w

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial