Created
June 27, 2017 12:34
-
-
Save iron-viper/c6b7effaf8d2a934e65a3d122497aaab to your computer and use it in GitHub Desktop.
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 quickSort($array) | |
{ | |
if (count($array) < 2) { | |
return $array; | |
} | |
$pivot = array_shift($array); | |
$less = []; | |
$greater = []; | |
foreach ($array as $element) { | |
if ($element <= $pivot) { | |
$less[] = $element; | |
} else { | |
$greater[] = $element; | |
} | |
} | |
return array_merge(quickSort($less), [$pivot], quicksort($greater)); | |
} | |
$test_array = []; | |
for ($i = 0; $i <= 100000; $i++) { | |
$test_array[] = mt_rand(1, 10000); | |
} | |
shuffle($test_array); | |
// ------------------------------ | |
$start = time(); | |
$res = quickSort($test_array); | |
$end = time(); | |
print_r($res); | |
echo date("s", $end - $start) . " sec"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment