Php sorting algorithm? Classic php Sorting Algorithm

Source: Internet
Author: User

This article will summarize several good php sorting algorithms. I hope these algorithms will be helpful to you.

The Code is as follows: Copy code


1. the bubble algorithm and sorting algorithm are called Bubble Sorting because they always place decimals forward and large numbers backward in the sorting process.

$ 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 sorting,
Quicksort is an improvement in Bubble sorting.
Proposed by C. A. R. Hoare in 1962. The basic idea is: sort the data to be sorted into two separate parts,
All the data in one part is smaller than all the data in the other part, and then sort the data in this way,
The entire sorting process can be performed recursively to convert 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 elements to be sorted,
The sequence is placed at the end of the sorted series until all the data elements to be sorted are arranged. Selecting sorting is an unstable sorting 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 sorting
Starting from the first element, this element can be considered to have been sorted
Extracts the next element and scans it forward from the back in the sorted element sequence.
If the element (sorted) is greater than the new element, move the element to the next position.
Repeat Step 3 until you find the position where the sorted elements are smaller than or equal to the new elements.
Insert new elements to the 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 sort Algorithms

1) the basic idea of Insertion Sort is:
Each time you insert a record to be sorted, insert it to the appropriate position in the subfile according to its keyword size until all records are inserted.
2) the basic idea of Selection Sort is:
Select the record with the smallest keyword from the record to be sorted, and put it at the end of the sorted subfile until all records are sorted.
3) the basic idea of Bubble Sorting is:
Compare the keywords of the records to be sorted. If the order of the two records is the opposite, the records are exchanged until there is no reverse order.
4) Fast sorting is essentially the same as Bubble sorting. It is an application of exchange sorting. So the basic idea is the same as the above Bubble sorting.

The Code is as follows: Copy code

Signature <? Php
/**
* Four sorting algorithm designs (PHP)
*
* 1) the basic idea of Insertion Sort is:
Each time you insert a record to be sorted, insert it to the appropriate position in the subfile according to its keyword size until all records are inserted.
2) the basic idea of Selection Sort is:
Select the record with the smallest keyword from the record to be sorted, and put it at the end of the sorted subfile until all records are sorted.
3) the basic idea of Bubble Sorting is:
Compare the keywords of the records to be sorted. If the order of the two records is the opposite, the records are exchanged until there is no reverse order.
4) Fast sorting is essentially the same as Bubble sorting. It is an application of exchange sorting. So the basic idea is the same as the above Bubble sorting.
*
* @ Author quanshuidingdang
*/
Class Sort {
Private $ arr = array ();
Private $ sort = 'insert ';
Private $ marker = '_ sort ';
 
Private $ debug = TRUE;
 
/**
* Constructor
*
* @ Param array example: $ config = array (
'Arr' => array (22,3, 41,18), // array value to be sorted
'Sort '=> 'insert', // possible values: insert, select, bubble, and quick
'Debug' => TRUE // possible values: TRUE, FALSE
)
*/
Public function _ construct ($ config = array ()){
If (count ($ config)> 0 ){
$ This-> _ init ($ config );
}
}
 
/**
* Obtain the sorting result
*/
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;
}

// Initialize the member variable
Foreach ($ config as $ key => $ val ){
If (isset ($ this-> $ key )){
$ This-> $ key = $ val;
}
}

// Call the corresponding member method to complete sorting
$ 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 sorting
*
* @ 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 sorting
*
* @ 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 Sorting
*
* @ 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;
}
 
/**
* Fast sorting
*
* @ 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 );
}
 
/**
* Logging
*/
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 */

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.