JIYIK CN >

Current Location:Home > Learning > ALGORITHM >

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

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

In the article "Quick Sort", we introduced the principles and steps of quick sort, and implemented the algorithm using recursion. In the previous article, we also mentioned the use of non-recursive methods to implement the algorithm. In this article, we will use non-recursive methods to implement quick sort.

First, let's introduce the steps of the stack involved.

The first step is to apply for a stack to store the starting and ending positions of the sorted array.

Step 2: Push the starting position s and the end position e of the entire array into the stack

The third step is to pop the data from the stack, sort the data that has been popped, and find the final position p of the benchmark data.

Step 4: Determine whether the starting position s is less than the reference position p-1. If so, push the starting position and p-1 into the stack as the end position.

Step 5: Determine whether the reference position p+1 is less than the end position e. If so, push p+1 as the starting position and e as the end position into the stack.

Step 6: Determine whether the stack is empty. If it is not empty, repeat step 3, otherwise exit the operation.

The overall approach of using non-recursive methods is the above steps. Next, we will implement quick sorting through code.

The function for finding the base position can be the same as the one using recursion

function FindPv(&$arr, $s, $e)
{
    $p = $s;
    //基准起始位置
    $v = $arr[$p];
    //将数组的第一个值作为基准值
    while ($s < $e) {
        while ($arr[$e] > $v && $e > $p) {
            $e--;
        }
        $arr[$p] = $arr[$e];
        $p = $e;
        while ($arr[$s] < $v && $s < $p) {
            $s++;
        }
        $arr[$p] = $arr[$s];
        $p = $s;
    }
    $arr[$p] = $v;
    return $p;
}

function PvSort(&$arr)
{
    $stack = array();
    array_push($stack, array(0, count($arr) - 1));
    while (count($stack) > 0) {
        $temp = array_pop($stack);
        $p = FindPv($arr, $temp[0], $temp[1]);
        if ($p + 1 < $temp[1]) array_push($stack, array($p + 1, $temp[1]));
        if ($temp[0] < $p - 1) array_push($stack, array($temp[0], $p - 1));
    }
}

$arr = array(10, 6, 8, 23, 4, 1, 17, 56, 32, 50, 11, 9);
PvSort($arr);
print_r($arr);

The above is a non-recursive way to implement quick sort. In fact, the code is not complicated. Of course, PHP has more concise code to implement quick sort, but here we use this more primitive method, which will be more helpful for our understanding of quick sort.

I hope this article is helpful to you.

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

Grouping and Sorting in Pandas

Publish Date:2025/04/12 Views:90 Category:Python

This tutorial explored the concept of grouping data in a DataFrame and sorting it in Pandas. Grouping and Sorting DataFrame in Pandas As we know, Pandas is an advanced data analysis tool or package extension in Python. Most of the companies

Sort a collection by date in MongoDB

Publish Date:2025/04/11 Views:64 Category:MongoDB

In this MongoDB tutorial, the problem of sorting a collection in MongoDB is discussed. The different ways to sort a collection in the database are briefly explained. Using sort() function in MongoDB This problem is solved using the MongoDB

Sort by RAND in MySQL

Publish Date:2025/04/09 Views:176 Category:MySQL

Summary: in this tutorial, we will learn how to shuffle or sort the values ​​or records of a table in MySQL. Most businesses and organizations that use MySQL for data analysis or visualization need to sort different table values ​​o

Linux sort command sort

Publish Date:2025/04/08 Views:172 Category:OPERATING SYSTEM

The sort command is a commonly used sorting command in Linux , and it is also a pipeline command . In order to ensure that future instances can get the sorting results we want, we need to set the following # export LC_ALL=C Okay, next we wi

Recursively Deleting Files in Linux

Publish Date:2025/04/06 Views:153 Category:OPERATING SYSTEM

This article explains how to delete files in Linux. We will then elaborate on the following topics in detail. The example files and directories we will use throughout this article are as follows. After rm the command, type the name of the f

Copy Files Recursively in Linux

Publish Date:2025/04/06 Views:127 Category:OPERATING SYSTEM

The Linux terminal is a simple and quick way to copy files and directories. In this article, we will explain how to cp copy files in Linux using command. We will also use wildcards * to copy files with similar names and recursively copy mul

Sort Files by Size in Linux

Publish Date:2025/04/06 Views:191 Category:OPERATING SYSTEM

Sometimes you want to do some deep cleaning of your system by finding unnecessary large files and deleting them or removing files that are smaller than a predetermined size, such as logs. Linux provides various utilities that can help us fi

Recursively search for files in Linux

Publish Date:2025/04/06 Views:138 Category:OPERATING SYSTEM

In this Linux article, we will learn how to find files recursively in Linux. We will also see how to search files recursively in subdirectories in Linux systems. We will use different Linux commands in many ways. We will learn them one by o

Recursively add files and folders in Git

Publish Date:2025/03/27 Views:138 Category:Git

Sometimes, we come across a situation where we have to adjust some files, folders, and subfolders that already exist in Git. A part of a nested folder system has to be added remotely to Git. This article will discuss how to use commands to

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial