做法
將陣列分割後排序,再將兩個已排序的陣列合併成一個陣列。
實作
原始陣列。
1
| $items = [74, 52, 91, 13, 6, 69, 31, 24];
|
先試著把陣列分割成左陣列和右陣列。
1 2 3
| $half = (int) floor(count($items) / 2); $left = array_slice($items, 0, $half); $right = array_slice($items, $half);
|
輸出:
1 2
| Array ( [0] => 74 [1] => 52 [2] => 91 [3] => 13 ) Array ( [0] => 6 [1] => 69 [2] => 31 [3] => 24 )
|
使用遞迴函數。
1 2 3 4 5 6 7 8 9 10 11 12
| function mergeSort($items) { if (count($items) <= 1) { return $items; }
$half = (int) floor(count($items) / 2); $left = array_slice($items, 0, $half); $right = array_slice($items, $half); $left = mergeSort($left); $right = mergeSort($right); }
|
輸出:
1 2 3 4 5 6 7 8 9 10 11 12 13
| Array ( [0] => 74 [1] => 52 [2] => 91 [3] => 13 ) Array ( [0] => 6 [1] => 69 [2] => 31 [3] => 24 ) Array ( [0] => 74 [1] => 52 ) Array ( [0] => 91 [1] => 13 ) Array ( [0] => 74 ) Array ( [0] => 52 ) Array ( [0] => 91 ) Array ( [0] => 13 ) Array ( [0] => 6 [1] => 69 ) Array ( [0] => 31 [1] => 24 ) Array ( [0] => 6 ) Array ( [0] => 69 ) Array ( [0] => 31 )
|
設定另一個 merge()
函數,以進行比較合併。
1 2 3 4 5 6 7 8 9 10 11 12
| function merge($left, $right) { while (count($left) && count($right)) { if ($left[0] < $right[0]) { $reg[] = array_shift($left); } else { $reg[] = array_shift($right); } }
return array_merge($reg, $left, $right); }
|
因此寫為:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| function mergeSort($items) { if (count($items) <= 1) { return $items; }
$half = (int) floor(count($items) / 2); $left = mergeSort(array_slice($items, 0, $half)); $right = mergeSort(array_slice($items, $half));
return merge($left, $right); }
function merge($left, $right) { while (count($left) && count($right)) { if ($left[0] < $right[0]) { $reg[] = array_shift($left); } else { $reg[] = array_shift($right); } }
return array_merge($reg, $left, $right); }
|
輸出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| Array ( [0] => 52 ) Array ( [0] => 74 ) Array ( ) Array ( [0] => 13 ) Array ( [0] => 91 ) Array ( ) Array ( [0] => 13 ) Array ( [0] => 13 [1] => 52 ) Array ( [0] => 13 [1] => 52 [2] => 74 ) Array ( ) Array ( [0] => 91 ) Array ( [0] => 6 ) Array ( ) Array ( [0] => 69 ) Array ( [0] => 24 ) Array ( [0] => 31 ) Array ( ) Array ( [0] => 6 ) Array ( [0] => 6 [1] => 24 ) Array ( [0] => 6 [1] => 24 [2] => 31 ) Array ( [0] => 69 ) Array ( ) Array ( [0] => 6 ) Array ( [0] => 6 [1] => 13 ) Array ( [0] => 6 [1] => 13 [2] => 24 ) Array ( [0] => 6 [1] => 13 [2] => 24 [3] => 31 ) Array ( [0] => 6 [1] => 13 [2] => 24 [3] => 31 [4] => 52 ) Array ( [0] => 6 [1] => 13 [2] => 24 [3] => 31 [4] => 52 [5] => 69 ) Array ( [0] => 74 [1] => 91 ) Array ( )
|
最終可合併為:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| function mergeSort($items) { if (count($items) <= 1) { return $items; }
$half = (int) floor(count($items) / 2); $left = mergeSort(array_slice($items, 0, $half)); $right = mergeSort(array_slice($items, $half));
while (count($left) && count($right)) { if ($left[0] < $right[0]) { $reg[] = array_shift($left); } else { $reg[] = array_shift($right); } }
return array_merge($reg, $left, $right); }
|
程式碼