PHP Sort algorithm class instance _php skill

Source: Internet
Author: User

This example describes the PHP sort algorithm class. Share to everyone for your reference. Specifically as follows:

PHP implementation of the four sort algorithms:

1 The basic idea of the insertion sort (insertion sort) is:
Each time a record to be sorted is inserted at its key size to the appropriate location in the previously sorted child file until all records are inserted.

2 The basic idea of choosing sort (Selection sort) is:
Each trip selects the smallest key record from the record to be sorted, in the order that it is placed at the end of the sorted sub file until all the records have finished sorting.

3 The basic idea of bubble sort is:
22 Compare the keywords of the records to be sorted, and find that the order of the two records is reversed to exchange until there is no reverse sequence of records.

4 The quick sort is essentially the same as the bubble sort, which is an application of commutative ordering. So the basic idea is the same as the bubble sort above.

1. The sort.php document reads as follows:

<?php/** * * @author Quanshuidingdang/class Sort {Private $arr = array ();
  Private $sort = ' insert ';
  Private $marker = ' _sort ';
  Private $debug = TRUE; /** * constructor * @param array for example: $config = Array (' arr ' => Array (22,3,41,18),//array value to be sorted ' sort ' =& Gt ' Insert ',//possible value: Insert, select, Bubble, Quick ' debug ' => TRUE//Possible value: TRUE, FALSE) */Public function __con
    struct ($config = Array ()) {if (count ($config) > 0) {$this->_init ($config);
  /** * Get sort result/Public function display () {return $this->arr;
    /** * Initialization * @param array * @return BOOL/Private Function _init ($config = Array ()) {//Parameter judgment if (!is_array ($config) OR count ($config) = = 0) {if ($this->debug = = TRUE) {$this->_log ("Sort_i
      Nit_param_invaild ");
    return FALSE; }//Initialize member variable foreach ($config as $key => $val) {if (Isset ($this-> $key)) {$this-> $key = $val; }//Call the appropriate member method to complete the sort $method = $this->sort.
    $this->marker; if (! method_exists ($this, $method)) {if ($this->debug = = TRUE) {$this->_log ("Sort_method_invaild"
      );
    return FALSE;
    } if (false = = ($this->arr = $this-> $method ($this->arr)) return false;
  return TRUE; /** * Insert Sort * * @param array * @return BOOL/Private Function Insert_sort ($arr) {//Parameter judgment I F (! Is_array ($arr) OR count ($arr) = = 0) {if ($this->debug = = TRUE) {$this->_log ("Sort_array" (Inser
      T) _invaild ");
    return FALSE;
    }//Implementation $count = COUNT ($arr);
      for ($i = 1; $i < $count; $i + +) {$tmp = $arr [$i];
          for ($j = $i-1; $j >= 0; $j-) {if ($arr [$j] > $tmp) {$arr [$j +1] = $arr [$j];
        $arr [$j] = $tmp;
  }} return $arr; /** * Select sort * * @param arRay * @return BOOL/Private Function Select_sort ($arr) {//Parameter judgment if (! Is_array ($arr) OR count ($arr) = =
      0) {if ($this->debug = = TRUE) {$this->_log ("Sort_array (SELECT) _invaild");
    return FALSE;
    }//Implementation $count = COUNT ($arr);
      for ($i = 0; $i < $count-1; $i + +) {$min = $i;
      for ($j = $i +1; $j < $count; $j + +) {if ($arr [$min] > $arr [$j]) $min = $j;
        } if ($min!= $i) {$tmp = $arr [$min];
        $arr [$min] = $arr [$i];
      $arr [$i] = $tmp;
  } return $arr; /** * Bubble Sort * * @param array * @return BOOL/Private Function Bubble_sort ($arr) {//Parameter judgment I F (! Is_array ($arr) OR count ($arr) = = 0) {if ($this->debug = = TRUE) {$this->_log ("Sort_array" (bubbl
      e) _invaild ");
    return FALSE;
    }//Implementation $count = COUNT ($arr); for ($i = 0; $i < $count; $i + +) {for ($j = $count-1; $j > $i; $j-) {if ($arr [$j] < $arr [$j-1]) {$tmp = $arr [$j];
          $arr [$j] = $arr [$j-1];
        $arr [$j-1] = $tmp;  
  }} return $arr;  /** * Quick Sort * @param array * @return BOOL/Private Function Quick_sort ($arr) {//specific implementation if 
    (Count ($arr) <= 1) return $arr;
    $key = $arr [0];
    $left _arr = Array ();
    $right _arr = Array ();
      for ($i = 1; $i < count ($arr); $i + +) {if ($arr [$i] <= $key) $left _arr[] = $arr [$i];
    else $right _arr[] = $arr [$i];
    $left _arr = $this->quick_sort ($left _arr); 
 
    $right _arr = $this->quick_sort ($right _arr);
  Return Array_merge ($left _arr, Array ($key), $right _arr); /** * Log record/private function _log ($msg) {$msg = ' date['. Date (' y-m-d h:i:s '). '] ' . $msg.
    ' \ n ';
  Return @file_put_contents (' Sort_err.log ', $msg, file_append);

 }/*end of File sort.php*//*location htdocs/sort.php * *

2. The sort_demo.php document reads as follows:

<?php
require_once (' sort.php ');
$config = Array (
  ' arr ' => array (), 200303,2200,1192),
  //array value to be sorted '
  sort ' => ' Selec T ',
  //possible value: Insert, select, Bubble, Quick
  ' debug ' => TRUE
  //Possible value: True, FALSE
);
$sort = new sort ($config);
Var_dump ($config [' arr ']);
Var_dump ($sort->display ());
/*end of php*/

I hope this article will help you with your 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.