Last active
May 26, 2017 07:31
-
-
Save ValeriiVasyliev/53a1809e1dfc22268c0f1b0674712608 to your computer and use it in GitHub Desktop.
Merge two arrays and sort (HackerRank Task)
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
<?php | |
$a = [4, 1, 5, 7, 7, 4, 0, 1, 2, 3]; | |
$b = [5, 2, 4, 5, 9, 9, 5, 0, 1, 2, 3, 4]; | |
$result = $a; | |
foreach ($b as $el) { | |
$result[] = $el; | |
} | |
quickSort($result, 0, count($result) - 1); | |
function quickSort(&$arr, $low, $high) { | |
$i = $low; | |
$j = $high; | |
$middle = $arr[ ( $low + $high ) / 2 ]; | |
do { | |
while($arr[$i] < $middle) ++$i; | |
while($arr[$j] > $middle) --$j; | |
if($i <= $j){ | |
$temp = $arr[$i]; | |
$arr[$i] = $arr[$j]; | |
$arr[$j] = $temp; | |
$i++; $j--; | |
} | |
} | |
while($i < $j); | |
if($low < $j){ | |
quickSort($arr, $low, $j); | |
} | |
if($i < $high){ | |
quickSort($arr, $i, $high); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment