5. Array (bottom)

Source: Internet
Author: User
Tags array length rounds

1. Array stored in arrays

            var num1 = 10;            var str = "hello";            var isYes = true;            var arr1 = [10, 20, 30,[50, 60]];            var arr = [num1, str, isYes, arr1];            // alert(arr[3][2]);            // alert(arr[3][3][1]);            // alert(arr); //10,hello,true            // alert(arr[3]);            /*var tmpArr = arr[3];            alert(tmpArr);            alert(tmpArr[1]);            alert(arr[3][1])*/

= = "Note" In the data element, you can store data of any data type. ==

2. Two-dimensional array exercises

?? The natural number of 1 to 25 is assigned to a 5x5 two-dimensional array a by looping in row order, and then the lower left half of the array is output. Test programming.

                1. Declare array arr Altogether five elements these five elements are array 2, subscript 0 of this element arr[0] = [1, 2, 3, 4, 5];                /* Arr[0] = [1, 2, 3, 4, 5];                ARR[1] = [6, 7, 8, 9, 10];                ARR[2] = [11, 12, 13, 14, 15];                ARR[3] = [16, 17, 18, 19, 20];            ARR[4] = [21, 22, 23, 24, 25];            */var arr = []; var tmp = 1;                For the cumulative number 1 for (var i = 0; i < 5; i++) {var newArr = [];                Fill the NEWARR with five data for (var j = 0; J < 5; J + +) {Newarr.push (tmp++);                } arr.push (NEWARR);            Arr[[], [], [], [], []];            }//Alert (arr[0]);                                for (var i = 0; i < arr.length; i++) {//1) outputs each of the ARR elements and wraps//document.write (Arr[i]);                    2. Output each number in the Tmparr for (var j = 0; J < Arr[i].length; J + +) { if (J <= i){document.write (Arr[i][j] + "&ensp;");            }} document.write ("<br/>"); }/* 1, find all the elements in the array and 2, let the elements in the array Exchange position (important) arr[1] A                RR[3] Exchange value;            3, the maximum number of arrays and the minimum number (important) Max min 4, the array of the minimum number of subscript (important) Index */var arr = [4, 5, 2, 3, 1];            var sum = 0;            for (var i = 0; i < arr.length; i++) {sum + = Arr[i];            }//alert (sum);            var tmp = arr[1];            ARR[1] = arr[3];            ARR[3] = tmp; Alert (arr); 1,2,5,4,3//MAX var max = arr[0]; Assuming the first element, the maximum var min = arr[0];            Assume the first element, the minimum var index = 0;                for (var i = 1; i < arr.length; i++) {/*if (Arr[i] > max) {max = arr[i];     }*/if (Arr[i] < min) {               min = Arr[i];                index = i;            }}//alert (max);            Alert (min); alert (index);
3. Sorting

/*
Reverse ()
Reverse the original array directly. The original array was modified directly.
Format: Array. reverse ();
*/

        /*var arr = [30, 20, 10, 5, 50, 100];        arr.reverse();        alert(arr); //100,50,5,10,20,30*/

/*
Sort ()
Sort
Array. sort (); Ascending.
"Note" is not good, the default is to use string sorting. So we don't have to.
*/

    /*      var arr = [5, 4, 3, 7, 8, 9];        arr.sort();        alert(arr); //3,4,5,7,8,9*/        var arr = [5, 15, 10, 1];        arr.sort();        alert(arr); //1,10,15,5
4. Bubbling method

/*
Data structure: sorting algorithm.
Bubble sort
Select sort (fixed notation)
*/

/*
The idea of bubbling sort:
Idea: Enter 6 unordered numbers, from beginning to end to compare the size of adjacent two digits, if the large number in front, decimal in the After, then the exchange of two positions, then compare, so that all the data in order from small to large.

            A total of 6 numbers, compared to 5 rounds. First round: 5 times 9, 8, 7, 6, 5, 4 8, 9, 7, 6, 5, 4 8, 7, 9, 6, 5, 4 8, 7, 6, 9, 5, 4 8, 7, 6, 5, 9, 4 8, 7, 6, 5, 4, 9 second round: 4 times 8, 7, 6, 5            , 4 7, 8, 6, 5, 4 7, 6, 8, 5, 4 7, 6, 5, 8, 4 7, 6, 5, 4, 8            Third round: 3 times 7, 6, 5, 4 6, 7, 5, 4 6, 5, 7, 4 6, 5, 4, 7 Fourth round: 2 times 6, 5, 4 5, 6, 4 5, 4, 6 fifth round: 1 times 5,        4 4, 5 */var arr = [9, 8, 7, 6, 5, 4]; Bubble sort//1, determine how many rounds to compare = array length-1; for (var i = 0; i < arr.length-1; i++) {//2, the number of times this round is compared = array length            -Current number of rounds (i + 1);            for (j = 0; J < arr.length-(i + 1); J + +) {if (Arr[j] > arr[j + 1]) {//Swap two number of positions        var tmp = arr[j];                    ARR[J] = arr[j + 1];                Arr[j + 1] = tmp; }}} alert (arr); 4,5,6,7,8,9
5. Select sort

/*
Select sort

            Daleitai method: Each ring is taken out, with all the elements behind, to compare, in line with the exchange conditions, exchange position.  First round: five times 9, 8, 7, 6, 5, 4 8, 9, 7, 6, 5, 4 7, 9, 8, 6, 5, 4 6, 9, 8, 7, 5, 4 5, 9, 8, 7, 6, 4 4, 9, 8, 7, 6, 5 second round: four times 9, 8, 7, 6, 5 8                   , 9, 7, 6, 5 7, 9, 8, 6, 5 6, 9, 8, 7, 5 5, 9, 8, 7, 6 third round: three times  9, 8, 7, 6 8, 9, 7, 6 7, 9, 8, 6 6, 9, 8, 7 fourth round:                         Two times 9, 8, 7 8, 9, 7 7, 9, 8 fifth round: one        9, 8 8, 9 "Note" Select, try writing yourself.        */var arr = [9, 8, 7, 6, 5, 4]; for (var i = 0, i < arr.length-1; i++) {for (var j = i + 1; j < Arr.length; J + +) {if (arr[i                    ] > Arr[j]) {var tmp = Arr[i];Arr[i] = Arr[j];                ARR[J] = tmp; }}} alert (arr);     4,5,6,7,8,9
6. Join

/*
Function: Array = = string
To stitch elements in an array into a string
Format: Array. Join ("string");
Value: A string that is stitched together
Do not change the original array
*/

        var arr = [1, 2, 3, 4, 5];        var str = arr.join("+");        // alert(str);//1+2+3+4+5        /*            1、随机给出一个五位以内的数,然后输出该数共有多少位,每位分别是什么        */        var num = 12345;        /*            将数字num的每一位取出。        */        var arr = []; //用来存储取下来的每一位。        while(1){            if(num == 0){                break;            }            //个位如何取            arr.push(num % 10);            num = parseInt(num / 10);        }        arr.reverse();        alert(arr.length);
7. Join Step

/*
Function: Concatenation of elements in an array into strings by the array = = string
Format: Array. Join ("string");
Value: A string that is stitched together
*/

        var arr = [1, 2, 3, 4, 5];        var str = arr.join("+");        // alert(str);//1+2+3+4+5        /*            1、随机给出一个五位以内的数,然后输出该数共有多少位,每位分别是什么        */        var num = 12345;        /*            将数字num的每一位取出。        */        var arr = []; //用来存储取下来的每一位。        while(num){            //个位如何取            arr.push(num % 10);            num = parseInt(num / 10);        }        arr.reverse();        alert(arr.length);

5. Array (bottom)

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.