Created
December 1, 2019 17:47
-
-
Save adrianosferreira/d3e7d92937bbb88bcbde45f6fefd4ed6 to your computer and use it in GitHub Desktop.
Heap, heapsort, heapify with PHP
This file contains hidden or 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
<?php | |
function heapSort( $arr ) { | |
$arr = createHeap( $arr ); | |
$sorted = []; | |
$arrLength = count( $arr ); | |
for ( $i = 1; $i <= $arrLength; $i ++ ) { | |
$last = count( $arr ); | |
array_unshift( $sorted, $arr[1] ); | |
$arr[1] = $arr[ $last ]; | |
unset( $arr[ $last ] ); | |
if ( ! $arr ) { | |
return $sorted; | |
} | |
$current = 1; | |
while ( true ) { | |
$leftIndex = $current * 2; | |
$rightIndex = $current * 2 + 1; | |
$max = null; | |
if ( isset( $arr[ $leftIndex ], $arr[ $rightIndex ] ) ) { | |
if ( $arr[ $leftIndex ] > $arr[ $rightIndex ] && $arr[ $leftIndex ] > $arr[ $current ] ) { | |
$max = $leftIndex; | |
} elseif ( $arr[ $leftIndex ] < $arr[ $rightIndex ] && $arr[ $rightIndex ] > $arr[ $current ] ) { | |
$max = $rightIndex; | |
} | |
} elseif ( isset( $arr[ $leftIndex ] ) && $arr[ $leftIndex ] > $arr[ $current ] ) { | |
$max = $arr[ $leftIndex ]; | |
} elseif ( isset( $arr[ $rightIndex ] ) && $arr[ $rightIndex ] > $arr[ $current ] ) { | |
$max = $arr[ $rightIndex ]; | |
} | |
if ( ! $max ) { | |
break; | |
} | |
$temp = $arr[ $max ]; | |
$arr[ $max ] = $arr[ $current ]; | |
$arr[ $current ] = $temp; | |
$current = $max; | |
} | |
} | |
return $sorted; | |
} | |
function createHeap( $arr ) { | |
array_unshift( $arr, 'value' ); | |
unset( $arr[0] ); | |
$arrLength = count( $arr ); | |
for ( $i = 1; $i <= $arrLength; $i ++ ) { | |
$current = $i; | |
while ( true ) { | |
$updated = false; | |
$parent = ( int ) ( $current / 2 ); | |
if ( ! isset( $arr[ $parent ] ) ) { | |
break; | |
} | |
if ( $arr[ $current ] > $arr[ $parent ] ) { | |
$temp = $arr[ $current ]; | |
$arr[ $current ] = $arr[ $parent ]; | |
$arr[ $parent ] = $temp; | |
$updated = true; | |
} | |
$current = $parent; | |
if ( ! $parent || ! $updated ) { | |
break; | |
} | |
} | |
} | |
return $arr; | |
} | |
function heapify( $arr ) { | |
array_unshift( $arr, '1' ); | |
unset( $arr[0] ); | |
for ( $i = count( $arr ); $i > 0; $i -- ) { | |
$current = $i; | |
while( true ) { | |
$left = $current * 2; | |
$right = $current * 2 + 1; | |
$max = null; | |
if ( isset( $arr[ $left ], $arr[ $right ] ) ) { | |
if ( $arr[ $left ] > $arr[ $right ] && $arr[ $left ] > $arr[ $current ] ) { | |
$max = $left; | |
} elseif ( $arr[ $left ] < $arr[ $right ] && $arr[ $right ] > $arr[ $current ] ) { | |
$max = $right; | |
} | |
} elseif ( isset( $arr[ $left ] ) && $arr[ $left ] > $arr[ $current ] ) { | |
$max = $left; | |
} elseif ( isset( $arr[ $right ] ) && $arr[ $right ] > $arr[ $current ] ) { | |
$max = $right; | |
} | |
if ( ! $max ) { | |
break; | |
} | |
$temp = $arr[ $current ]; | |
$arr[ $current ] = $arr[ $max ]; | |
$arr[ $max ] = $temp; | |
$current = $max; | |
} | |
} | |
return $arr; | |
} | |
echo implode( ',', heapify( [ 10, 20, 15, 12, 40, 25, 18 ] ) ); | |
//echo implode( ',', heapSort( [ 10, 20, 15, 30, 40 ] ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment