PHP Sorting algorithm _php tutorial for PHP sort Classic algorithm

Source: Internet
Author: User
This article to sum up a few good php sorting algorithm, I hope these algorithms for you to help the reunion Oh.
The code is as follows Copy Code


1. Bubble algorithm, sorting algorithm, because in the sorting process is always a decimal place, the large number of backward, the equivalent of bubbles upward, so called bubble sort

$array = Array (a,f,c,b,e,h,j,i,g);

function Maopao_fun ($array) {

if ($len <= 1) {

return $arr;

}

$count = count ($array);

for ($i =0; $i < $count; $i + +) {

for ($j = $count-1; $j > $i; $j-) {

if ($array [$j] > $array [$j-1]) {

$tmp = $array [$j];

$array [$j] = $array [$j-1];

$array [$j-1] = $tmp;

}

}

}

return $array;

}

2. Quick Sort,
Quick Sort (Quicksort) is an improvement to the bubbling sort.
by C. A. R. Hoare was introduced in 1962. The basic idea is to divide the sorted data into two separate parts by a single trip,
All of the data in one part is smaller than any other part of the data, and then the two parts of the data are quickly sorted by this method,
The entire sorting process can be recursive, in order to achieve the entire data into an ordered sequence.

function QuickSort ($arr) {

$len = count ($arr);

if ($len <= 1) {

return $arr;

}

$key = $arr [0];

$left _arr = Array ();

$right _arr = Array ();

for ($i =1; $i < $len; $i + +) {

if ($arr [$i] <= $key) {

$left _arr[] = $arr [$i];

} else {

$right _arr[] = $arr [$i];

}

}

$left _arr = QuickSort ($left _arr);

$right _arr = QuickSort ($right _arr);

Return Array_merge ($left _arr, Array ($key), $right _arr);

}

3. Select sort
Each trip selects the smallest (or largest) element from the data element to be sorted,
The order is placed at the end of the ordered sequence, until all the data elements are sorted out. Select Sort is an unstable sort method

The code is as follows Copy Code

function Select_sort ($arr) {

$count = count ($arr);

for ($i =0; $i < $count; $i + +) {

for ($j = $i +1; $j < $count; $j + +) {

if ($arr [$i] > $arr [$j]) {

$tmp = $arr [$i];

$arr [$i] = $arr [$j];

$arr [$j] = $tmp;

}

}

}

return $arr;

}

4. Insert sort
Starting with the first element, the element can be thought to have been sorted
take out the next element, scan from backward forward in the ordered sequence of elements
if the element (sorted) is greater than the new element, move the element to the next position
Repeat step 3, Until the sorted element is found to be less than or equal to the position of the new element
Insert new element into next position
Repeat step 2

The code is as follows Copy Code

function Insert_sort ($arr) {

$count = count ($arr);

for ($i =1; $i < $count; $i + +) {

$tmp = $arr [$i];

$j = $i-1;

while ($arr [$j] > $tmp) {

$arr [$j +1] = $arr [$j];

$arr [$j] = $tmp;

$j--;

}

}

return $arr;

}

$arr = Array (49,38,65,97,76,13,27);

Print_r (Insert_sort ($arr));


PHP implementation of 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.

The code is as follows Copy Code

 /**
* Four kinds of sorting algorithm design (PHP)
*
* 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.
*
* @author Quanshuidingdang
*/
Class Sort {
Private $arr = Array ();
Private $sort = ' insert ';
Private $marker = ' _sort ';

Private $debug = TRUE;

/**
* Constructor function
*
* @param array For example: $config = Array (
' arr ' = = Array (22,3,41,18),//arrays value to sort
' Sort ' = ' insert ',//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 sort results
*/
Public Function display () {
return $this->arr;
}

/**
* Initialization
*
* @param array
* @return BOOL
*/
Private Function _init ($config = Array ()) {
Parameter determination
if (!is_array ($config) OR count ($config) = = 0) {
if ($this->debug = = = TRUE) {
$this->_log ("Sort_init_param_invaild");
}
return FALSE;
}

Initializing member variables
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 determination
if (! Is_array ($arr) OR count ($arr) = = 0) {
if ($this->debug = = = TRUE) {
$this->_log ("Sort_array (insert) _invaild");
}
return FALSE;
}

Specific 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 determination
if (! Is_array ($arr) OR count ($arr) = = 0) {
if ($this->debug = = = TRUE) {
$this->_log ("Sort_array (SELECT) _invaild");
}
return FALSE;
}

Specific 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 determination
if (! Is_array ($arr) OR count ($arr) = = 0) {
if ($this->debug = = = TRUE) {
$this->_log ("Sort_array (Bubble) _invaild");
}
return FALSE;
}

Specific 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 records
*/
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 */

http://www.bkjia.com/PHPjc/632898.html www.bkjia.com true http://www.bkjia.com/PHPjc/632898.html techarticle This article to sum up a few good php sorting algorithm, I hope these algorithms for you to help the reunion Oh. The code below duplicates Code 1. Bubbling algorithm, sorting algorithm, due to in 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.