From c978b7f8feb1ea438e7d38dfd42dc35e0df0be3c Mon Sep 17 00:00:00 2001 From: Shaminda Rathnayaka Date: Mon, 29 Oct 2018 13:08:10 +0530 Subject: [PATCH] add heap sort using php --- .../Algorithms/Heap Sort/heap_sort.php.txt | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 Algorithms/Algorithms/Heap Sort/heap_sort.php.txt diff --git a/Algorithms/Algorithms/Heap Sort/heap_sort.php.txt b/Algorithms/Algorithms/Heap Sort/heap_sort.php.txt new file mode 100644 index 0000000..261a2eb --- /dev/null +++ b/Algorithms/Algorithms/Heap Sort/heap_sort.php.txt @@ -0,0 +1,125 @@ +_i = $key; + } + + public function getKey() + { + return $this->_i; + } + } + + class Heap + { + private $heap_Array; + private $_current_Size; + + public function __construct() + { + $heap_Array = array(); + $this->_current_Size = 0; + } + + // Remove item with max key + public function remove() + { + $root = $this->heap_Array[0]; + // put last element into root + $this->heap_Array[0] = $this->heap_Array[--$this->_current_Size]; + $this->bubbleDown(0); + return $root; + } + + // Shift process + public function bubbleDown($index) + { + $larger_Child = null; + $top = $this->heap_Array[$index]; // save root + while ($index < (int)($this->_current_Size/2)) { // not on bottom row + $leftChild = 2 * $index + 1; + $rightChild = $leftChild + 1; + + // find larger child + if ($rightChild < $this->_current_Size + && $this->heap_Array[$leftChild] < $this->heap_Array[$rightChild]) // right child exists? + { + $larger_Child = $rightChild; + } else { + $larger_Child = $leftChild; + } + + if ($top->getKey() >= $this->heap_Array[$larger_Child]->getKey()) { + break; + } + + // shift child up + $this->heap_Array[$index] = $this->heap_Array[$larger_Child]; + $index = $larger_Child; // go down + } + + $this->heap_Array[$index] = $top; // root to index + } + + public function insertAt($index, Node $newNode) + { + $this->heap_Array[$index] = $newNode; + } + + public function incrementSize() + { + $this->_current_Size++; + } + + public function getSize() + { + return $this->_current_Size; + } + + public function asArray() + { + $arr = array(); + for ($j = 0; $j < sizeof($this->heap_Array); $j++) { + $arr[] = $this->heap_Array[$j]->getKey(); + } + + return $arr; + } + } + + function heapsort(Heap $Heap) + { + $size = $Heap->getSize(); + // "sift" all nodes, except lowest level as it has no children + for ($j = (int)($size/2) - 1; $j >= 0; $j--) + { + $Heap->bubbleDown($j); + } + // sort the heap + for ($j = $size-1; $j >= 0; $j--) { + $BiggestNode = $Heap->remove(); + // use same nodes array for sorted elements + $Heap->insertAt($j, $BiggestNode); + } + + return $Heap->asArray(); + } + + $arr = array(3, 0, 2, 5, -1, 4, 1); + echo "Original Array : "; + echo implode(', ',$arr ); + $Heap = new Heap(); + foreach ($arr as $key => $val) { + $Node = new Node($val); + $Heap->insertAt($key, $Node); + $Heap->incrementSize(); + } + $result = heapsort($Heap); + echo "\nSorted Array : "; + echo implode(', ',$result)."\n"; + ?> + \ No newline at end of file