Sort sort exactly how

Source: Internet
Author: User

Sort () Method

The sort () method sorts the elements of the array at the appropriate position and returns the Arrays.

<!DOCTYPE HTML><HTML>    <Head>        <MetaCharSet= "UTF-8">        <title></title>        <Script>            vararr=[1, one,2, a,5,4,0];            Arr.sort (); Alert (arr);//[0,1,11,2,22,4,5]</Script>    </Head>    <Body>    </Body></HTML>

It can be seen that the array is sorted by the Unicode of the characters (the array is treated as a string).

Sorted in ascending order:

<!DOCTYPE HTML><HTML>    <Head>        <MetaCharSet= "UTF-8">        <title></title>        <Script>            vararr=[1, one,2, a,5,4,0]; Arr.sort (function(n1,n2) {returnN1-n2;            }); Alert (arr);//[0,1,2,4,5,11,22]        </Script>    </Head>    <Body>    </Body></HTML>

Random sort:

<!DOCTYPE HTML><HTML>    <Head>        <MetaCharSet= "UTF-8">        <title></title>        <Script>            vararr=[1, one,2, a,5,4,0]; Arr.sort (function(n1,n2) {returnmath.random ()-0.5;            });        Alert (arr); </Script>    </Head>    <Body>    </Body></HTML>
How the sort () method implements sorting

?? How do I view the sort () method if the implementation is sorted? We can output a, B and array in the comparison function to see if we can see the sorting algorithm Used.

-1];function compare(a,b){    console.log(a,b,arr);    return a-b;}arr.sort(compare);/**控制台输出1 8 [1, 8, 3, 5, -1]8 3 [1, 8, 3, 5, -1]1 3 [1, 8, 8, 5, -1]8 5 [1, 3, 8, 5, -1]3 5 [1, 3, 8, 8, -1]8 -1 [1, 3, 5, 8, -1]5 -1 [1, 3, 5, 8, 8]3 -1 [1, 3, 5, 5, 8]1 -1 [1, 3, 3, 5, 8][-1,1, 3, 5, 8]*/

?? The first time 1 and 8 comparison, 1<8, do not need to adjust the position.?

The second 8 and 3 comparisons, 8>3, need to adjust the Position. But there is no swap position, just 8 covers the 3 Position. Here you can infer that the bubbling algorithm is not used Purely.
?? The third is the 1 and 3 comparisons, where the 1<3,3 replaces the 8 Position. What the hell, a few meanings??? See here I also say do not understand ah. Then keep looking down.??

The four are 8 and 5 compared, 8>5, and just cover, no swap Position. Still do not understand, continue down!
?? Five is 3 and 5 comparison, 3<5,5 replaced 8 of the position, do not understand, continue down!??

The sixth is 8 and 1 comparison, 8>-1, still just cover, continue down!
?? 第七、八、九次, 1 were compared with 5,3,1 in turn, and 5,3,1 moved one position at a Time.

We come to the conclusion that the sort() method is sorted using both bubbling and inserting methods.

Analog sequencing:

<!DOCTYPE HTML><HTML>    <Head>        <MetaCharSet= "UTF-8">        <title></title>        <Script>            vararr=[ +, a,4, -,1, wu, about,6, -]; functionfindminindex (arr,start) {varImin=arr[start]; varIminindex=start;  for(varI=Start;i<Arr.length;i++){                    if(imin>arr[i]) {imin=arr[i]; Iminindex=i; }                }                returniminindex; }             for(varI=0; I<Arr.length;i++){                varN=Findminindex (arr,i); vartem; TEM=arr[n]; arr[n]=arr[i]; arr[i]=tem;        } Alert (arr); </Script>    </Head>    <Body>    </Body></HTML>

Sort sort exactly how

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.