Skip to content

Instantly share code, notes, and snippets.

@liuxd
Created September 7, 2017 01:12
Show Gist options
  • Select an option

  • Save liuxd/bd6691879849c891c7285a6171fc41a6 to your computer and use it in GitHub Desktop.

Select an option

Save liuxd/bd6691879849c891c7285a6171fc41a6 to your computer and use it in GitHub Desktop.
HeapSort.php
<?php
/**
* Heap Sort.
*
* Keywords: heap
* Time Complexity: O(n log n)
* Space Complexity: O(1)
*/
class HeapSort extends SplHeap
{
public function compare($value1, $value2)
{
return ($value2 - $value1);
}
}
# Example.
$array = [24,2,20,12,33,10,11];
$obj = new HeapSort;
foreach ($array as $v) {
$obj->insert($v);
}
foreach ($obj as $num) {
echo $num, "\n";
}
# end of this file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment