Skip to content

Commit 810f8fc

Browse files
authored
Merge pull request #116 from salehhashemi1992/heap-sort
add heap sort to sort algorithms
2 parents e967070 + e061398 commit 810f8fc

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

Sorting/HeapSort.php

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* HeapSort Algorithm
4+
*
5+
* The HeapSort algorithm sorts an array by first transforming the array into a max heap and then
6+
* iteratively swapping the maximum element from the heap with the last unsorted element
7+
* and "heapifying" the heap again.
8+
*
9+
* @param array $arr
10+
* @return array
11+
* @throws \UnexpectedValueException
12+
*/
13+
function heapSort(array $arr): array
14+
{
15+
$n = count($arr);
16+
if ($n <= 0) {
17+
throw new \UnexpectedValueException('Input array must have at least one element.');
18+
}
19+
20+
// Build heap
21+
for ($i = $n / 2 - 1; $i >= 0; $i--) {
22+
heapify($arr, $n, $i);
23+
}
24+
25+
// Extract elements from heap one by one
26+
for ($i = $n - 1; $i >= 0; $i--) {
27+
// Swap
28+
[$arr[0], $arr[$i]] = [$arr[$i], $arr[0]];
29+
30+
// Heapify the reduced heap
31+
heapify($arr, $i, 0);
32+
}
33+
34+
return $arr;
35+
}
36+
37+
/**
38+
* Ensures that the array satisfies the heap property
39+
*
40+
* @param array $arr
41+
* @param int $n
42+
* @param int $i
43+
*/
44+
function heapify(array &$arr, int $n, int $i): void
45+
{
46+
$largest = $i;
47+
$left = 2 * $i + 1;
48+
$right = 2 * $i + 2;
49+
50+
if ($left < $n && $arr[$left] > $arr[$largest]) {
51+
$largest = $left;
52+
}
53+
54+
if ($right < $n && $arr[$right] > $arr[$largest]) {
55+
$largest = $right;
56+
}
57+
58+
if ($largest !== $i) {
59+
[$arr[$i], $arr[$largest]] = [$arr[$largest], $arr[$i]];
60+
heapify($arr, $n, $largest);
61+
}
62+
}

tests/Sorting/SortingTests.php

+26
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require_once __DIR__ . '/../../Sorting/BubbleSort.php';
77
require_once __DIR__ . '/../../Sorting/BubbleSort2.php';
88
require_once __DIR__ . '/../../Sorting/CountSort.php';
9+
require_once __DIR__ . '/../../Sorting/HeapSort.php';
910
require_once __DIR__ . '/../../Sorting/InsertionSort.php';
1011
require_once __DIR__ . '/../../Sorting/MergeSort.php';
1112
require_once __DIR__ . '/../../Sorting/QuickSort.php';
@@ -172,4 +173,29 @@ public function testQuickSortCipher()
172173
$this->assertEquals($result1, $test1);
173174
$this->assertEquals($result2, $test2);
174175
}
176+
177+
public function testHeapSortPerformance()
178+
{
179+
$array = range(1, 1000000);
180+
shuffle($array); // Randomize the order
181+
$start = microtime(true);
182+
heapSort($array);
183+
$end = microtime(true);
184+
$this->assertLessThan(1, $end - $start);
185+
}
186+
187+
public function testHeapSortCipher()
188+
{
189+
$firstArray = [20, 16, -5, -8, 6, 12, 2, 4, -3, 9];
190+
$expectedResultOne = [-8, -5, -3, 2, 4, 6, 9, 12, 16, 20];
191+
192+
$secondArray = [-6, 12, 14, 17, 5, 4, -9, 15, 0, -8];
193+
$expectedResultTwo = [-9, -8, -6, 0, 4, 5, 12, 14, 15, 17];
194+
195+
$resultOne = heapSort($firstArray);
196+
$resultTwo = heapSort($secondArray);
197+
198+
$this->assertEquals($expectedResultOne, $resultOne);
199+
$this->assertEquals($expectedResultTwo, $resultTwo);
200+
}
175201
}

0 commit comments

Comments
 (0)