= 0; $i--) { heapify($arr, $n, $i); } // Extract elements from the max heap and build the sorted array. for ($i = $n - 1; $i >= 0; $i--) { // Swap the root(maximum value) of the heap with the last element of the heap. [$arr[0], $arr[$i]] = [$arr[$i], $arr[0]]; // Heapify the reduced heap. heapify($arr, $i, 0); } // Return the sorted array. return $arr; } /** * Ensures that the array satisfies the heap property * * @param array $arr * @param int $n * @param int $i */ function heapify(array &$arr, int $n, int $i): void { $largest = $i; $left = 2 * $i + 1; $right = 2 * $i + 2; if ($left < $n && $arr[$left] > $arr[$largest]) { $largest = $left; } if ($right < $n && $arr[$right] > $arr[$largest]) { $largest = $right; } if ($largest !== $i) { [$arr[$i], $arr[$largest]] = [$arr[$largest], $arr[$i]]; heapify($arr, $n, $largest); } }