JavaScript array and Bubble Sort method

Source: Internet
Author: User

Array

Arrays: Regardless of the type, can be stored. Storage is in a certain order.

Order: The index number starts at 0.

1, need to initialize the array first

var array//Array name = new Arrary ()//array Note case

Method of assigning values

array[0]= value 1

array[1]= Value 2

array[2]= Value 3

Array[3]= Value 4

Other usage

Sort, ascending

Array.Sort ();

Flip, want to arrange in descending order, you need to first sort ascending, then flip the whole array

Array.reverse ();

Finds the index number of the first occurrence of the value, and returns 1 if it cannot be found

Alert (Array.indexof (6));

Finds the index number of the last occurrence of this value, and returns 1 if it cannot be found

Alert (Array.lastindexof (2));

pops up the value on the last index, which is equivalent to removing the last

Alert (Array.pop ());

To push a value into an array, increase the length by 1, and increase the index number by 1

Array.push (4.4);

Iterate through the collection, replacing the index number with the newly defined variable name

If you want to print each element of an array, place the variable in the brackets after the name of the arrays as an index to use

for (var aa in array)

{document.write (array[aa]+ "<br/>")}

Bubble Contrast method

A[0]=5

A[1]=4

A[2]=7

A[3]=3

A[4]=1

Compare results

5 4 7) 3 1

4 5 7) 3 1

4 5 7) 3 1

3 5 7) 4 1

1 5 7) 4 3

1 5 7) 4 3

1 4 7) 5 3

1 3 7) 5 4

1 3 4) 5 7

1 3 4) 5 7

JavaScript array and Bubble Sort method

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.