-
-
Notifications
You must be signed in to change notification settings - Fork 469
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #116 from salehhashemi1992/heap-sort
add heap sort to sort algorithms
- Loading branch information
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
/** | ||
* HeapSort Algorithm | ||
* | ||
* The HeapSort algorithm sorts an array by first transforming the array into a max heap and then | ||
* iteratively swapping the maximum element from the heap with the last unsorted element | ||
* and "heapifying" the heap again. | ||
* | ||
* @param array $arr | ||
* @return array | ||
* @throws \UnexpectedValueException | ||
*/ | ||
function heapSort(array $arr): array | ||
{ | ||
$n = count($arr); | ||
if ($n <= 0) { | ||
throw new \UnexpectedValueException('Input array must have at least one element.'); | ||
} | ||
|
||
// Build heap | ||
for ($i = $n / 2 - 1; $i >= 0; $i--) { | ||
heapify($arr, $n, $i); | ||
} | ||
|
||
// Extract elements from heap one by one | ||
for ($i = $n - 1; $i >= 0; $i--) { | ||
// Swap | ||
[$arr[0], $arr[$i]] = [$arr[$i], $arr[0]]; | ||
|
||
// Heapify the reduced heap | ||
heapify($arr, $i, 0); | ||
} | ||
|
||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters