Quick sorting of differences between php and javascript

Source: Internet
Author: User

1. PHP
Copy codeThe Code is as follows: <? Php
$ 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
Copy codeThe 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.Copy codeThe 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:Copy codeThe Code is as follows: // this will result with an error
If (arr. length <2)
Return arr;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.