The road to learning sorting algorithms - quick sort (non-recursive implementation)
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.
Related Articles
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
Linked List Merge Sort
Publish Date:2025/03/18 Views:138 Category:ALGORITHM
-
In this article, we will learn how to sort a linked list using merge sort algorithm. It is one of the most preferred algorithms to sort a linked list because slow pointer random access makes the performance of other algorithms worse (for ex
Sorting an array of objects in React
Publish Date:2025/03/13 Views:79 Category:React
-
To sort an array of objects in React: Create a shallow copy of the array. Call the array's `sort()` method, passing it a function. The function is used to define the sort order.