1. PHP
Copy Code code as follows:
<?php
$unsorted = Array (2,4,5,63,4,5,63,2,4,43);
function Quicksort ($array)
{
if (count ($array) = 0)
return Array ();
$pivot = $array [0];
$left = $right = Array ();
for ($i = 1; $i < count ($array); $i + +) {
if ($array [$i] < $pivot)
$left [] = $array [$i];
Else
$right [] = $array [$i];
}
Return Array_merge (Quicksort ($left), Array ($pivot), quicksort ($right));
}
$sorted = Quicksort ($unsorted);
Print_r ($sorted);
2. JavaScript
Copy Code code as follows:
var a = [2,4,5,63,4,5,63,2,4,43];
function Quicksort (arr)
{
if (arr.length = 0)
return [];
var left = new Array ();
var right = new Array ();
var pivot = arr[0];
for (var i = 1; i < arr.length; i++) {
if (Arr[i] < pivot) {
Left.push (Arr[i]);
} else {
Right.push (Arr[i]);
}
}
Return Quicksort (left). Concat (pivot, quicksort);
}
Console.log (Quicksort (a));
Note that the first conditional statement is very important! And in PHP the Count function returns a null value or an empty array or 0 instead, you can <2 things like count ($array) it
Copy Code code as follows:
if (count ($array) < 2)
return $array;
You can't use it in JavaScript, because there is an "empty" array to pass as a parameter for ' undefined ' values. Therefore, you need to include the above criteria:
Copy Code code as follows:
This'll result with the An error
if (Arr.length < 2)
return arr;