PHP實現冒泡排序、雙向冒泡排序演算法

來源:互聯網
上載者:User

冒泡排序(Bubble Sort),是一種較簡單的、穩定的排序演算法。冒泡排序演算法步驟:比較相鄰的元素,如果第一個比第二個大,就交換他們兩個的位置;對每對相鄰的元素執行同樣的操作,這樣一趟下來,最後的元素就是最大的;除了已得出來的最大元素,把剩餘的元素重複前面步驟,直到沒有元素再需要比較為止,這樣排序就完成了。冒泡演算法,在最好情況下,時間複雜度為O(n);在最壞情況下,時間複雜度為O(n2);平均時間複雜度為O(n2)。

PHP實現冒泡排序、雙向冒泡排序演算法
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 <?php/** * 資料結構與演算法(PHP實現) - 冒泡排序(Bubble Sort)。 *
 * @author 創想編程(TOPPHP.ORG) * @copyright Copyright (c) 2013 創想編程(TOPPHP.ORG) All Rights Reserved * @license http://www.opensource.org/licenses/mit-license.php MIT LICENSE * @version 1.0.0 - Build20130608 */class
BubbleSort {
  /**   * 冒泡排序。   *
   * @var integer   */  const
SORT_NORMAL = 1;   /**   * 雙向冒泡排序。   *
   * @var integer   */  const
SORT_DUPLEX = 2;   /**   * 需要排序的資料數組。   *
   * @var array   */  private
$data;   /**   * 資料數組的長度。   *
   * @var integer   */  private
$size;   /**   * 資料數組是否已排序。   *
   * @var boolean   */  private
$done;   /**   * 構造方法 - 初始化資料。   *
   * @param array $data 需要排序的資料數組。   */  public
function __construct(array
$data) {    $this->data =
$data;    $this->size =
count($this->data);    $this->done = FALSE;  }   /**   * 交換資料數組中兩個元素的位置。   *
   * @param integer $x 元素在數組中的索引。   * @param integer $y 元素在數組中的索引。   */  private
function swap($x,
$y) {    $temp
= $this->data[$x];    $this->data[$x] =
$this->data[$y];    $this->data[$y] =
$temp;     }   /**   * 冒泡排序。   */  private
function sort() {    $this->done = TRUE;     for
($i
= 1;
$i <
$this->size; ++$i) {      // 記錄交換資料的次數。      $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;        }      }       // 若交換資料的次數為0,說明資料數組已有序,不必再進行排序。      if
(0 === $swap) {        break
;      }    }  }   /**   * 雙向冒泡排序。   */  private
function duplexSort() {    $this->done = TRUE;     for
($i
= 1;
$i <=
floor($this->size / 2); ++$i) {      // 記錄交換資料的次數。      $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;        }      }       // 若交換資料的次數為0,說明資料數組已有序,不必再進行排序。      if
(0 === $swap) {        break;      }    }  }   /**   * 擷取排序後的資料數組。   *
   * @param integer $sort 排序演算法:SORT_NORMAL為冒泡排序;SORT_DUPLEX為雙向冒泡排序。   * @return array 返回排序後的資料數組。   */  public
function getResult($sort
= self::SORT_NORMAL) {    // 若已排序則無需再進行排序,直接返回排序好的資料數組。    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;  }}?>
範例程式碼
1234 <?php$bubble
=
new BubbleSort(array(35, 75, 92, 41, 27, 58));echo
'<pre>'
, print_r($bubble->getResult(BubbleSort::SORT_DUPLEX), TRUE),
'</pre>';?>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.