Skip to content

Instantly share code, notes, and snippets.

@iron-viper
Created June 27, 2017 12:34
Show Gist options
  • Save iron-viper/c6b7effaf8d2a934e65a3d122497aaab to your computer and use it in GitHub Desktop.
Save iron-viper/c6b7effaf8d2a934e65a3d122497aaab to your computer and use it in GitHub Desktop.
<?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