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)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor