PHP Quick Sort Quicksort instance detailed _php skill

Source: Internet
Author: User
Tags php programming php regular expression

This example describes the PHP quick sort quicksort. Share to everyone for your reference, specific as follows:

Quicksort

In the fast sorting algorithm, the divide-and-conquer strategy is used. First, the sequence is divided into two sub sequences, recursively sorted, until the whole sequence is sorted. (That is, the thought in Split)

The steps are as follows:

Select a key element in the sequence as the axis;

The sequence is reordered to move elements smaller than the axis to the front of the axis, and the elements larger than the axis move to the back of the axis. After dividing, the shaft is in its final position;

Recursively reorder two subsequence: A subsequence containing a smaller element and a subsequence containing a larger element.

such as sequence $arr:

5 3 0 11 44 7 23 2 The first element $arr[0] = 5 as the axis setting flag bit low ... top stands for both end and end
2 3 0 11 44 7 23 2 starting from the opposite direction (right) comparison: 2<5 replaces the first position with the 2,low++
2 3 0 11 44 7 23 The comparison begins in the opposite direction (left) until: 5<11 replaces the last position with the 11,top–
Repeat the above steps until Low = top replaces the position with an axis element that is 5
2 3 0 5 44 7 23 11
This can be divided into two parts 2 3 0 and 44 23 11
So that we can get the recursion to proceed with the 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;
      //Set the initial numeric $com = $arr [$low];
          while ($low!= $top) {//will replace the initial value to the left while ($top && $top!= $low) {if ($com > $arr [$top]) {
          $arr [$low + +] = $arr [$top];
          Break
          }else{$top--;
            }//will replace the initial value to the right while ($low && $low!= $top) {if ($com < $arr [$low]) {
            $arr [$top--] = $arr [$low];
          Break
          }else{$low + +;
      $arr [$low] = $com;
    return $low; }
}

More about PHP Interested readers can view the site topics: "PHP Sorting algorithm Summary", "PHP object-oriented Program Design Introductory Course", "PHP Mathematical Operation Skills Summary", "PHP Array" operation Skills Encyclopedia, "PHP Data structure and algorithm tutorial", " PHP Programming algorithm Summary, "PHP Regular Expression Usage Summary", and "PHP Common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.