快速排序因爲排序效率在同爲O(N*logN)的幾種排序方法中效率較高,所以常常被採用,再加上快速排序思想----分治法也確實實用,所以在不少筆試面試中出現的概率很高。php
直接默寫出快速排序仍是有必定難度的,因此必定要弄清楚原理再去記憶而不是去硬背。面試
快速排序是C.R.A.Hoare於1962年提出的一種劃分交換排序。它採用了一種分治的策略,一般稱其爲分治法(Divide-and-Conque)。ide
最多見的實現方法是"填坑法",它的實現步驟以下:ui
具體的代碼實現以下:this
<?php class QuickSortClass { public function partition(array &$arr, $startIndex, $endIndex) { // 取第一個元素爲基準值 $pivot = $arr[$startIndex]; $left = $startIndex; $right = $endIndex; // 坑的位置,出事等於基準值pivot的位置 $dataIndex = $startIndex; while ($right >= $left) { // right 指針從右向左進行移動,若是當前值小於基準值則將當前元素放到坑中,當前元素的位置變成新坑,left向右移動一個位置,切換到left進行比較,不然right往左移動一個位置繼續用新元素的值與基準值進行比較 while ($right >= $left) { if ($arr[$right] < $pivot) { $arr[$dataIndex] = $arr[$right]; $dataIndex = $right; $left++; break; } $right--; } // left 指針從左往右移動,若是當前值大於基準值則將當前元素放到坑中,當前元素變爲新坑,right向左移動一個位置,切換到right進行比較,不然left往右移動一個位置繼續與基準值進行比較 while($right >= $left) { if ($arr[$left] > $pivot) { $arr[$dataIndex] = $arr[$left]; $dataIndex = $left; $right --; break; } $left ++; } } $arr[$dataIndex] = $pivot; return $dataIndex; } public function quickSort(&$arr, $startIndex, $endIndex) { // startIndex大於等於endIndex的時候遞歸結束 if ($startIndex >= $endIndex) { return ; } $pivotIndex = $this->partition($arr, $startIndex, $endIndex); $this->quickSort($arr, $startIndex, $pivotIndex - 1); $this->quickSort($arr, $pivotIndex + 1, $endIndex); } } $quickSortClass = new quickSortClass(); $arr = [72, 6, 57, 88, 60, 42, 83, 73, 48, 85]; $quickSortClass->quickSort($arr, 0, count($arr) - 1); var_dump($arr);
填坑法的口訣以下指針
1.$left=strart; $right = end; $pivot=$arr[$left];
第一個坑爲$arr[$left]
code
2.$right--
由後向前找比$pivot
小的數,找到後挖出此數填到前一個坑$arr[$left]
中, $arr[$right]
變成新坑。排序
3.$left++
由前向後找比$pivot
大的數,找到後也挖出此數填到前一個坑$arr[$right]
中。遞歸
4.重複執行2,3二步,直到$left==$right
,將基準數$pivot
填入$arr[$left]
中。it