/** * Heap sort: Uses a heap (implemented by the Containers module) to sort the collection. * Requirements: Needs to be able to compare elements with <=> * Time Complexity: О(n^2) * Space Complexity: О(n) total, O(1) auxiliary * Stable: Yes * @param array $array Array to be sorted * @return array */ public static function heap_sort(&$array) { //This will heapify the array $init = (int) floor((count($array) - 1) / 2); for ($i = $init; $i >= 0; $i--) { $count = count($array) - 1; Sort::build_heap($array, $i, $count); } //swaping of nodes for ($i = count($array) - 1; $i >= 1; $i--) { $tmp_var = $array[0]; $array[0] = $array[$i]; $array[$i] = $tmp_var; Sort::build_heap($array, 0, $i - 1); } return $array; }