JavaScript Array Example

Source: Internet
Author: User
Tags array example rounds javascript array

To repeat when adding new elements to an array

1. General Ideas and principles

    vara =[3,5,8,9,2,10,4];//Original Array    varx = 4;//New Data    varCF =false;//set a value that determines whether duplicates are repeated     for(vari=0;i<a.length;i++)//Looping through    {        if(a[i]==x)//Data Duplication{CF=true;//change the value of a set variable        }    }    if(cf==true)//code for repeated data execution{alert ("This data already exists"); }    Else//data is not duplicated{a.push (x);//Add to Array    }    

2. A more convenient method, IndexOf () can return the corresponding data in the corresponding array of the index number, if there is no change in the array data, will return 1,

So you can use IndexOf () to determine if the data is duplicated,

    

    vara =[3,5,8,9,2,10,4];//Original Array    varx = 4;//New Data    varb = A.indexof (x);//define variable B to receive the return value of indexof ()    if(B==-1)//The return value is-1, the data is not in the array{a.push (x);//add data to an array    }    Else//a return value of not 1 indicates that the data is in the array{alert ("The data already exists");//code for repeated data execution}

Bubble sort

An unordered array of non-repeating in the order from the large to the smallest or from small to large to rearrange, here according to the order from large to small analysis, the principle is to compare the size of each two data, the small value back, that is, if the previous data is less than the next data, then the Exchange, if the previous data is greater than the next data

First define a primitive array

    var attr = [4,7,3,6,8,1,2,9,5]; // Original Array

Comparison and exchange of the first data and the second data

 var  i = 0; //     index is 0  var  a = attr[i]<attr[i+1]; //     if  (A==true ) //   { //  interchange  var  b = attr[i]; //  Sets an intermediate variable B to receive the value of the first Data  attr[i] = attr[i+1]; //  The first data becomes a larger value  attr[i+1] = b; //  The second data becomes a smaller value } 

If you want to compare, you need a for loop.

     for(vari=0;i<attr.length-1;i++)//22 The number of comparisons is reduced by one for the length of the array    {        if(Attr[i]<attr[i+1])//If the previous data is less than the last data        {            //Exchange            varb = Attr[i];//set an intermediate variable B to receive the value of the first dataAttr[i] = attr[i+1];//The first data becomes a larger valueATTR[I+1] = b;//The second data becomes a smaller value        }        //no change if previous data is greater than the last data}

The output array found the lowest value to the bottom, according to the maximum number of consecutive 8 rounds can be the data from the large to small arrangement, so you can apply a loop in the outer layer to control the comparison of the round, and the more the turn, the number of 22 comparisons per wheel will be less, because the minimum value has been ranked, The number of rows (x) is exactly the number of rounds (j) minus one, the number of 22 comparisons (z) is the number of rows of data (y) minus one, and the total number of data is length, so x+y=length;x=j-1;z=y-1 ==>y=z+1;==> (j-1) + (z+1) =length;==> z=length-j, which can eventually be written as:

     for(varj=1;j<attr.length;j++)//control the round of comparisons, starting from 1 so there are attr.length-1 wheels    {         for(vari=0;i<attr.length-j;i++)//22 The number of comparisons is reduced by the length of the array        {            if(Attr[i]<attr[i+1])//If the previous data is less than the last data            {                //Exchange                varb = Attr[i];//set an intermediate variable B to receive the value of the first dataAttr[i] = attr[i+1];//The first data becomes a larger valueATTR[I+1] = b;//The second data becomes a smaller value            }            //no change if previous data is greater than the last data        }    }

Output to

If you want to order from small to large, as long as the condition is changed to the current data is greater than the next data exchange can be, that is, if the less than the number is changed to greater than.

An easy way to sort data

Sort () method, which by default is sorted in ascending order by the value of the first digit of the data, and can be sorted in ascending order by adding a sort function

    function Sortnumber (A, b)    {        return a    -b}    Attr.sort (sortnumber);

Finding data by dichotomy

  The dichotomy method applies to the ordered array , the principle is the index takes the judgment, the gradual two-point discards reduces the comparison scope, finally finds the index value of the data, the advantage is the comparison few times, can save the computer resources when the big data searches

    varattr =[1,2,3,4,5,6,7,8,9,10];//Original Array    varsr = 11;//data that needs to be looked up    varMINXL = 0;//Minimum Index    varMAXXL =attr.length-1;//Maximum Index    varMIDXL = 0;//Fetch Medium Index     while(true)    {        //Index FetchMidxl=parseint ((MINXL+MAXXL)/2)//determine if the index number you want to find        if(attr[midxl]==SR) {             Break;//Yes, find and exit the Loop            }        //determine if there are only two values left        if(minxl==MIDXL) {//determine if the last index is the required index number            if(attr[midxl+1]==SR) {                //Yes, find and exit the LoopMidxl=midxl+1 Break; }            Else            {    //No, no appropriate value, exit loopMidxl=-1;//give a value that cannot be indexed                 Break; }            }        //change the scope of the search, that is, two points        if(attr[midxl]<SR) {MINXL=MIDXL; }        Else{MAXXL=MIDXL; }            }    if(Midxl==-1) {alert ("No This data"); }    Else{alert ("This data serial number is" +MIDXL); }

  

  

JavaScript Array Example

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.