search
Homephp教程PHP开发PHP quick sort quicksort example detailed explanation

The example in this article describes PHP quick sort. Share it with everyone for your reference, the details are as follows:

quicksort

In the quick sort algorithm, the divide and conquer strategy is used. First, the sequence is divided into two subsequences, and the subsequences are sorted recursively until the entire sequence is sorted. (That is, the idea of ​​dividing it into two)

The steps are as follows:

Select a key element in the sequence as the axis;

Reorder the sequence, move elements smaller than the axis to the front of the axis, and larger than the axis The elements are moved behind the axis. After the division, the axis is at its final position;

recursively reorders two subsequences: the subsequence with smaller elements and the subsequence with larger elements.

For example, the sequence $arr:

5 3 0 11 44 ​​7 23 2 Set the first element $arr[0] = 5 as the axis and set the flag low... top represents the beginning and the end
2 3 0 11 44 ​​7 23 2 from the opposite Start comparing in the direction (right): 22 3 0 11 44 ​​7 23 11 Start comparing in the opposite direction (left) until: 5Repeat the above steps until low == top Replace the position with the axis element, which is 5
2 3 0 5 44 7 23 11
This way it can be divided into two parts 2 3 0 and 44 23 11
This way you can get Recursively continue with the starting steps

Algorithm implementation:

class quick_sort{
    function quicksort(&$arr,$low,$top){
      if($low < $top){
        $pivotpos = $this->partition($arr,$low,$top);
        $this->quicksort($arr,$low,$pivotpos-1);
        $this->quicksort($arr,$pivotpos+1,$top);
      }
    }
    function partition(&$arr, $low ,$top){
      if($low == $top){
        return;
      }
  //   设置初始数值
      $com = $arr[$low];
      while($low!=$top){
  //      将比初始数值小的替换到左边
        while($top&&$top!=$low){
          if($com > $arr[$top]){
          $arr[$low++] = $arr[$top];
          break;
          }else{
            $top--;
          }
        }
  //      将比初始数值大的替换到右边
        while($low&&$low!=$top){
          if($com < $arr[$low]){
            $arr[$top--] = $arr[$low];
            break;
          }else{
            $low++;
          }
        }
      }
      $arr[$low] = $com;
      return $low;
    }
}

I hope this article will be helpful to everyone in PHP programming.

For more detailed explanations of PHP quicksort quicksort examples and related articles, please pay attention to the PHP Chinese website!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools