PHP Sorting algorithm class instance, sorting algorithm example _php tutorial

Source: Internet
Author: User

PHP Sort algorithm class instance, sort algorithm instance


The example in this paper describes the PHP sorting algorithm class. Share to everyone for your reference. Specific as follows:

PHP implementations of the four sorting algorithms:

1) The basic idea of inserting a sort (insertion sort) is:
Insert a record to be sorted each time, by its keyword size, into the appropriate location in the previously sorted sub-file until all records are inserted.

2) The basic idea of selecting sort (Selection sort) is:
Each trip selects the smallest record of the keyword from the record to be sorted, placing the order at the end of the ordered sub-file until all records have been sorted.

3) The basic idea of bubble sorting is:
22 Compare the keywords for the records to be sorted, and find that the two records are exchanged in reverse order until there are no reversed records.

4) Fast sorting is essentially the same as bubbling sort, which is an application that belongs to the Exchange sort. So the basic idea is the same as the bubble sort above.

1. sort.php files are 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),//' sort ' = ' sort ' = ' I ') Nsert ',//Possible values: Insert, select, Bubble, quick ' debug ' = TRUE//Possible value: TRUE, FALSE) */Public function __construct ($config = Array ())    {if (count ($config) > 0) {$this->_init ($config);  }}/** * Get sorting results */Public Function display () {return $this->arr; }/** * Initialize * * @param array * @return BOOL */Private Function _init ($config = Array ()) {//parameter to determine if ( !is_array ($config) OR count ($config) = = 0) {if ($this->debug = = = TRUE) {$this->_log ("Sort_init_param_i      Nvaild ");    } return FALSE;      }//Initialize member variable foreach ($config as $key + $val) {if (Isset ($this-$key)) {$this, $key = $val;    }    }Call the corresponding 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 to determine if (! is_ Array ($arr) OR count ($arr) = = 0) {if ($this->debug = = = TRUE) {$this->_log ("Sort_array (insert) _invaild"      );    } return FALSE;    }//Concrete 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 to determine if (! is_ Array ($ARR) OR count ($arr) = = 0) {if ($this->debug = = = TRUE) {$this->_log ("Sort_array (SELECT) _invaild");    } return FALSE;    }//Concrete 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 to determine if (! is_ Array ($arr) OR count ($arr) = = 0) {if ($this->debug = = = TRUE) {$this->_log ("Sort_array (bubble) _invaild"      );    } return FALSE;    }//Concrete 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) {//Implement 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. sort_demo.php files are as follows:

<?phprequire_once (' sort.php '); $config = Array (  ' arr ' = = Array (all, $, $, (), (), (), 200303,2200,1192),  Array values to sort '  sort ' = ' select ',  //Possible values: 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 is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/1018523.html www.bkjia.com true http://www.bkjia.com/PHPjc/1018523.html techarticle PHP Sorting algorithm class instances, sorting algorithm Examples This article describes the PHP sorting algorithm class. Share to everyone for your reference. The following: Four kinds of sorting algorithm PHP implementation: 1) Insert Row ...

  • 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.