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

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

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial