PHP implements Bubble sorting and bidirectional bubble sorting algorithms

Source: Internet
Author: User

Bubble Sort is a simple and stable sorting algorithm. Bubble Sorting Algorithm step: Compare adjacent elements. If the first element is larger than the second one, the two of them are exchanged. perform the same operation on each adjacent element, the final element is the largest. In addition to the obtained largest element, repeat the remaining elements in the previous step until no elements need to be compared. This sorting is complete. In the best case, the time complexity of the bubble algorithm is O (n). In the worst case, the time complexity is O (n2), and the average time complexity is O (n2 ).

PHP implements Bubble sorting and bidirectional bubble Sorting Algorithm 1
 
/**
* Data Structure and algorithm (implemented in PHP)-Bubble Sort ).
*
* @ Author creative programming (TOPPHP. ORG)
* @ Copyright Copyright (c) 2013 creative programming (TOPPHP. ORG) All Rights Reserved
* @ License http://www.opensource.org/licenses/mit-license.php MIT LICENSE
* @ Version 1.0.0-Build20130608
*/
Class BubbleSort {
/**
* Bubble sorting.
*
* @ Var integer
*/
Const SORT_NORMAL = 1;
 
/**
* Bidirectional bubble sorting.
*
* @ Var integer
*/
Const SORT_DUPLEX = 2;
 
/**
* Array of data to be sorted.
*
* @ Var array
*/
Private $ data;
 
/**
* The length of the data array.
*
* @ Var integer
*/
Private $ size;
 
/**
* Whether the data array is sorted.
*
* @ Var boolean
*/
Private $ done;
 
/**
* Constructor-Initialize data.
*
* @ Param array $ array of data to be sorted.
*/
Public function _ construct (array $ data ){
$ This-> data = $ data;
$ This-> size = count ($ this-> data );
$ This-> done = FALSE;
}
 
/**
* Swap the positions of two elements in the data array.
*
* @ Param integer $ x index of the element in the array.
* @ Param integer $ y index of the element in the array.
*/
Private function swap ($ x, $ y ){
$ Temp = $ this-> data [$ x];
$ This-> data [$ x] = $ this-> data [$ y];
$ This-> data [$ y] = $ temp;
}
 
/**
* Bubble sorting.
*/
Private function sort (){
$ This-> done = TRUE;
 
For ($ I = 1; $ I <$ this-> size; ++ $ I ){
// Record the number of data exchanges.
$ Swap = 0;
 
For ($ j = $ this-> size-1; $ j> $ I-1; -- $ j ){
If ($ this-> data [$ j] <$ this-> data [$ j-1]) {
$ This-> swap ($ j-1, $ j );
+ + $ Swap;
}
}
 
// If the number of data exchanges is 0, it indicates that the data array is ordered and does not have to be sorted.
If (0 ===$ swap ){
Break;
}
}
}
 
/**
* Bidirectional bubble sorting.
*/
Private function duplexSort (){
$ This-> done = TRUE;
 
For ($ I = 1; $ I <= floor ($ this-> size/2); ++ $ I ){
// Record the number of data exchanges.
$ Swap = 0;
 
For ($ j = $ this-> size-1, $ k = $ I-1;
$ J> $ I-1 & $ k <$ this-> size-1; -- $ j, ++ $ k ){
If ($ this-> data [$ j] <$ this-> data [$ j-1]) {
$ This-> swap ($ j-1, $ j );
+ + $ Swap;
}
 
If ($ this-> data [$ k]> $ this-> data [$ k + 1]) {
$ This-> swap ($ k, $ k + 1 );
+ + $ Swap;
}
}
 
// If the number of data exchanges is 0, it indicates that the data array is ordered and does not have to be sorted.
If (0 ===$ swap ){
Break;
}
}
}
 
/**
* Obtain the sorted data array.
*
* @ Param integer $ sort Sorting Algorithm: SORT_NORMAL indicates Bubble Sorting; SORT_DUPLEX indicates bidirectional bubble sorting.
* @ Return array returns the sorted data array.
*/
Public function getResult ($ sort = self: SORT_NORMAL ){
// If the data has been sorted, no sort is required and the sorted data array is directly returned.
If ($ this-> done ){
Return $ this-> data;
}
 
Switch ($ sort ){
Case self: SORT_DUPLEX:
$ This-> duplexSort ();
Break;
 
Case self: SORT_NORMAL:
Default:
$ This-> sort ();
Break;
}
 
Return $ this-> data;
}
}
?>

Sample Code 1
2
3
4 <? Php
$ Bubble = new BubbleSort (array (35, 75, 92, 41, 27, 58 ));
Echo '<pre>', print_r ($ bubble-> getResult (BubbleSort: SORT_DUPLEX), TRUE), '</pre> ';
?>

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.