PHP implementation Bubble sort, two-way bubble sorting algorithm _php tutorial

Source: Internet
Author: User
Bubble sort (Bubble sort) is a simpler, more stable sort algorithm. Bubble sort Algorithm steps: Compare adjacent elements, if the first one is larger than the second, swap them both, and perform the same operation on each pair of adjacent elements, so that the last element is the largest, and in addition to the largest element that has been made, the remaining elements are repeated in the previous steps. This sort is done until there are no more elements to compare. The bubbling algorithm, in the best case, has a time complexity of O (n), and in the worst case, the time complexity is O (N2); The average time complexity is O (N2).

PHP implementation Bubble sort, two-way bubble sorting algorithm 1

/**
* Data structure and algorithm (PHP implementation)-bubble sort (Bubble sort).
*
* @author Gen Want programming (topphp.org)
* @copyright Copyright (c) 2013 Gen want 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 sort.
*
* @var Integer
*/
Const SORT_NORMAL = 1;

/**
* Bidirectional bubble sort.
*
* @var Integer
*/
Const SORT_DUPLEX = 2;

/**
* Array of data that needs to be sorted.
*
* @var Array
*/
Private $data;

/**
* The length of the data array.
*
* @var Integer
*/
Private $size;

/**
* The data array is sorted.
*
* @var Boolean
*/
Private $done;

/**
* Construction Method-initializes the data.
*
* @param array $data The arrays of data that need to be sorted.
*/
Public function __construct (array $data) {
$this->data = $data;
$this->size = count ($this->data);
$this->done = FALSE;
}

/**
* Swap the position of two elements in the data array.
*
* @param an integer $x the index of the element in the array.
* @param an integer $y the 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 sort.
*/
Private Function sort () {
$this->done = TRUE;

for ($i = 1; $i < $this->size; + + $i) {
Record the number of times the data was exchanged.
$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 exchanged is 0, the data array is ordered and no more sorting is required.
if (0 = = = $swap) {
break;
}
}
}

/**
* Bidirectional bubble sort.
*/
Private Function Duplexsort () {
$this->done = TRUE;

for ($i = 1, $i <= floor ($this->size/2), + + $i) {
Record the number of times the data was exchanged.
$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 exchanged is 0, the data array is ordered and no more sorting is required.
if (0 = = = $swap) {
Break
}
}
}

/**
* Gets an array of sorted data.
*
* @param integer $sort sorting algorithm: Sort_normal to bubble sort; Sort_duplex to two-way bubble sort.
* @return Array returns the sorted arrays of data.
*/
Public Function GetResult ($sort = self::sort_normal) {
If they are sorted, they do not need to be sorted and return a sorted array of data directly.
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;
}
}
?>

Example code 1
2
3
4 $bubble = new Bubblesort (Array (35, 75, 92, 41, 27, 58));
Echo '

', Print_r ($bubble->getresult (Bubblesort::sort_duplex), TRUE), '
';
?>

http://www.bkjia.com/PHPjc/477265.html www.bkjia.com true http://www.bkjia.com/PHPjc/477265.html techarticle Bubble sort (Bubble sort) is a simpler, more stable sort algorithm. Bubble Sort Algorithm Step: compare adjacent elements, if the first one is larger than the second, swap them two ...

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