JIYIK CN >

Current Location:Home > Learning > ALGORITHM >

The road to learning sorting algorithms - quick sort

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

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 faster than other O(n log n) algorithms because its inner loop can be implemented efficiently on most architectures.

Quick sort uses a divide and conquer strategy to divide a list into two sub-lists.

The above part is quoted from the Internet. I can never remember these definitions clearly.

Let's look at the implementation steps directly below

1. Pick an element from the sequence, called the "baseline" (there are many ways to find this base, here we use the first element as the base by default)

2. Rearrange the sequence, placing all elements smaller than the base value in front of the base value, and all elements larger than the base value in the back of the base value (the same number can be on either side). After this partition is exited, the base value is in the middle of the sequence. This is called a partition operation.

3. Divide the array into two parts based on the middle position of the benchmark obtained in the second step, and recursively sort the subsequence of elements less than the benchmark value and the subsequence of elements greater than the benchmark value.

4. Repeat the second step until the number of values ​​in the subsequence is 1

One of the sorting processes can be referred to the following figure

Quick sort process

Above we have learned about the process of finding the reference position. Now we just need to recursively split the array according to the reference position and repeat the above process for each sub-array one by one.

Let’s look at the implementation process

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,$s,$e){
    if($s>=$e) return ;
    $nextP = FindPv($arr,$s,$e);  //找到下一个基准所在位置
    PvSort($arr,$s,$nextP-1);
    PvSort($arr,$nextP+1,$e);
}
$arr = array(10,6,8,23,4,1,17,56,32,50,11,9);
PvSort($arr);
print_r($arr);

The above is a quick sort implemented by recursion. Recursion is very convenient to use and allows us to grasp the overall architecture of the program. The system has already done the underlying details for us. In fact, the underlying recursion uses the stack mechanism. We can also use the stack mechanism to implement the quick sort algorithm without recursion. You can check the non-recursive implementation of quick sort . This will help us have a deeper understanding of the implementation mechanism of the algorithm.

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

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

Sorting Arrays in Bash

Publish Date:2025/03/23 Views:74 Category:OPERATING SYSTEM

Sorting an array is a very common task in any programming language. In Bash scripting, we can also accomplish this task in two different ways. The first one uses any sorting algorithm and the second one uses a built-in keyword in Bash scrip

Sort data based on the second column of a file in Bash

Publish Date:2025/03/22 Views:87 Category:OPERATING SYSTEM

This article explains how to sort data based on the second column of a file in bash. Overview of the Sort Command in Bash Sort files using the sort command, which places records in a specific order. By default, the sort command sorts files

Learning the Sorting Algorithm - Insertion Sort (Concepts)

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

What is "insertion sort"? The concept is as follows: each time a record to be sorted is inserted into the previously sorted sequence according to its key size, until all records are inserted. Concepts are always somewhat abstract, and can a

Learning path of sorting algorithm - direct insertion sort

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

This article follows up on Insertion Sort (Concepts) and presents the implementation steps and code for direct insertion sort. Since the Concepts section already has a large number of illustrations, it would be a bit long-winded to provide

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial