Tell you a sorting algorithm, perhaps the most important thing they know-fast sorting, whether implemented in PHP or JavaScript. Although the Code between the two languages looks similar, there are also some differences, which shows the importance of syntax knowledge!
1. PHP
The Code is as follows:
$ Unsorted = array );
Function quicksort ($ array)
{
If (count ($ array) = 0)
Return array ();
$ Dependencies = $ array [0];
$ Left = $ right = array ();
For ($ I = 1; $ I <count ($ array); $ I ++ ){
If ($ array [$ I] <$ struct)
$ Left [] = $ array [$ I];
Else
$ Right [] = $ array [$ I];
}
Return array_merge (quicksort ($ left), array ($ rows), quicksort ($ right ));
}
$ Sorted = quicksort ($ unsorted );
Print_r ($ sorted );
2. JavaScript
The Code is as follows:
Var a = [,];
Function quicksort (arr)
{
If (arr. length = 0)
Return [];
Var left = new Array ();
Var right = new Array ();
Var callback = arr [0];
For (var I = 1; I <arr. length; I ++ ){
If (arr [I] <strong ){
Left. push (arr [I]);
} Else {
Right. push (arr [I]);
}
}
Return quicksort (left). concat (rows, quicksort (right ));
}
Console. log (quicksort ());
Note that the first condition statement is very important! In PHP, the COUNT function returns a NULL value, an empty array, or 0 instead. You can use it like count ($ array) <2.
The Code is as follows:
If (count ($ array) <2)
Return $ array;
You cannot use it in JavaScript because an "empty" array is passed as a parameter for the 'undefined' value. Therefore, the preceding conditions must be included:
The Code is as follows:
// This will result with an error
If (arr. length <2)
Return arr;