php實現一些快速排序演算法

來源:互聯網
上載者:User
   好久沒來逛部落格了,實在是因為項目太忙的緣故,抽不出時間來寫,對不住關注我部落格的同學了。最近複習了一下c語言,將C語言的一些排序演算法用php實現了一下,貼出來大家供大家學習指正。  php實現插入排序
/** * Description:php實現插入排序的類 * @author wzy */class insert_sort {public $arr;public $size;function __construct($arr) {$this->arr = $arr;$this->size = count ( $arr );}//引用傳遞,實現資料值的交換function swap(&$a, &$b) {list ( $a, $b ) = array ($b, $a );}//插入排序主邏輯函數function insert_sort_process() {for($j = 1; $j < $this->size; $j ++) {$key = $this->arr [$j];$i = $j - 1;while ( $i >= 0 && $this->arr [$i] > $key ) {$this->swap ( $this->arr [$i], $this->arr [$i + 1] );$i --;}//$i+1是因為退出while迴圈時進行了減一操作$this->arr [$i + 1] = $key;}return $this->arr;}}
php實現快速排序
/** * Description:php實現快速排序類 * * @author wzy *         */class quick_sort {public $arr;public $size;function __construct($array) {$this->arr = $array;$this->size = count ( $array );}function quick_swap(&$a, &$b) {list ( $a, $b ) = array ($b, $a );}//擷取分段資料位元置function quick_partiton($left, $right) {$key = $this->arr [$left];$i = $left + 1;$j = $right;while ( $i <= $j ) {while ( $i <= $j && $this->arr [$i] <= $key ) {$i ++;}while ( $i <= $j && $this->arr [$j] > $key ) {$j --;}if ($i < $j) {$this->quick_swap ( $this->arr [$i], $this->arr [$j] );}}$this->quick_swap ( $this->arr [$left], $this->arr [$j] );return $j;}//快速排序主邏輯函數function quick_sort_process($left = null, $right = null) {$left = (is_null ( $left )) ? 0 : $left;$right = (is_null ( $right )) ? $this->size - 1 : $right;if ($left < $right) {$middle = $this->quick_partiton ( $left, $right );$this->quick_sort_process ( $left, $middle - 1 );$this->quick_sort_process ( $middle + 1, $right );}}}
調用$this->arr即為排序好的數組
以後有時間會經常更新自己的部落格,貼出自己的原創性代碼供大家學習討論,感謝大家關注

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.