javascript overview of the "array" is a common method --- --- --- Notes and bubble sort

First, the array is a collection of data.

By the constructor (var arr = new Array ( ' 0', '1', 'hello')) and a literal (var arr = [ '0' , '1']) way to create,

Note that, when the constructor to create, if there is only one value, but also a number, it represents the length of the array (var arr = new Array (5), to create an array of length 5).

Array index starts from 0, length property to get the length of the array, but also can change the value of the array.

How to change the value of the array length property?

 When the array length is zero, or an array of [], an array of empty.

About storage array

- The problem is we need to pay special attention when using an array! -

Sometimes we will encounter such a problem, as when the value of the two arrays, arrays are not equal, when the value of an array is assigned to another array, another array change, the two arrays will change.

 Address of the array is a complex type (reference type) data, when we var a arr, in the stack memory, the heap memory, then to arr stack memory address, the address is the value of arr arr value stored in the variable storage (stack).

Every time we create a new array, a new array will give a new location in the heap memory. Although the same array of values, but they are different addresses , it is not equal.

And when we arr assigned to arr3 when, in fact, the address assigned to the arr3 array arr, arr and arr3 point to the same array, so when we change arr3, the corresponding original arr will change.

An array of common methods

 1. Array.isArray () --- detect whether the array

 2. push () --- to accept any number of parameters, and individually added to the end of the array

 3. pop () --- removes the last item from the end of the array

 4. unshift () --- add any entries in the array front

 5. shift () --- the first item in the array is removed

 Sequence 6. reverse () --- flip array item

 7. sort () --- array entries are arranged in ascending order according to the alphabet

 But when we sort numbers, there will be a problem

 If the digital ordering, we need the help of a function

1 function compare(a,b){
2      return a-b   
3 }

 Ascending to the above, if the descending order, simply return to the internal function b - a can.

8. concat () --- Clone current array entry, creating a new array of items parameter can be an array or array

 9. slice () --- a new array based on one or more current array entry is created. You can accept one or two parameters (index position)

 10. splic () --- array deletion, insertion, replacement, is the most powerful method for the array, the return value is an array, changing the original array

delete

 Parameter 1: starting from the starting position; Parameter 2: Remove length

insert

 Parameter 2 is 0, the parameter corresponding to the original array index position 1, add entries from the back

replace

 Delete, after the addition, addition is added back from a position corresponding to an index parameter.

11. join () --- corresponding to the array into a string, the parameter is a connector

Original array unchanged

 12. indexOf () --- return to find the position with the array, without looking -1

Two parameters, Reference 1: Find the item; Reference 2: Find the index start position (optional)

 13.lastIndexOf () --- seek from the forward end of the array and returns the index position or the number of front to back

 14. every () --- for each operation array given function, if the array each of which is true, returns true, false otherwise

There are two parameters: a parameter is a function (callback); reference 2 (optional) this point
the reference is a function of 1 (callback function), there are three index callback function parameter (value item in the array, the array entries position, the array itself)

 15. some () --- for each operation array given function, if the function returns true for any one of the array, the result returns true

There are two parameters: a parameter is a function (callback); reference 2 (optional) this point
the reference is a function of 1 (callback function), there are three index callback function parameter (value item in the array, the array entries position, the array itself)

16. filter () --- one array for each of the given operating function, the function returns a true result is an array of entries

There are two parameters: a parameter is a function (callback); reference 2 (optional) this point
the reference is a function of 1 (callback function), there are three index callback function parameter (value item in the array, the array entries position, the array itself)

 17. map () --- to run on each item in the array given function, each function call returns the result of an array, the array is returned, regardless of true or false-- map

There are two parameters: a parameter is a function (callback); reference 2 (optional) this point
the reference is a function of 1 (callback function), there are three index callback function parameter (value item in the array, the array entries position, the array itself)

 18. forEach () --- iterate from beginning to end, the given function calls for each array element, there is no return value

There are two parameters: a parameter is a function (callback); reference 2 (optional) this point
the reference is a function of 1 (callback function), there are three index callback function parameter (value item in the array, the array entries position, the array itself)

 19. reduce () and reduceRight () --- iteration of an array of all the items, and then build a final value returned. The result of each iteration to get back as the initial value for the next iteration of the next

The method has two parameters, one parameter: the function performed by each of the (four parameters (the previous value, the current value of the index entry array, the array itself)); Reference 2: Initial value iteration (optional)

 If the second argument is omitted, then that is used as the initial value of the first item in the array, which means that the first call to the function using the first and second terms as the first value and the second parameter of the function

reduceRight () and reduce () the same principle, but reduceRight () is processed one by one from the last item of the array to the first item (right to left)

 

Bubble Sort

Bubble sort is simply an array of values ​​is pairwise comparison, typically used to process the array scrambled ordered array.

For example, an array, which values ​​in ascending order

 However, this is not the best way that we can see, console.log (1) carried out 100 times.

Comparison of each round, the maximum value can be ranked in the final, so every round of the next round do not need to be compared with the maximum has been at the back

            The first round of comparisons 9
            5 8 3 2 15 67 98 12 3 25
            5 3 8 2 15 67 98 12 3 25
            5 3 2 8 15 67 98 12 3 25
            5 3 2 8 15 67 98 12 3 25
            5 3 2 8 15 67 98 12 3 25
            5 3 2 8 15 67 98 12 3 25
            5 3 2 8 15 67 12 98 3 25
            5 3 2 8 15 67 12 3 98 25
            532,815,671,232,598 selected maximum 98 -----

            8 second round of comparisons
            3 5 2 8 15 67 12 3 25
            3 2 5 8 15 67 12 3 25
            3 2 5 8 15 67 12 3 25
            3 2 5 8 15 67 12 3 25
            3 2 5 8 15 67 12 3 25
            3 2 5 8 15 12 67 3 25
            3 2 5 8 15 12 3 67 25
            3258151232567 98 ----- talk without comparing the maximum value of 67, 67, since the round has been compared, the maximum value has been chosen
        
            The third round of comparisons 7
            2 3 5 8 15 12 3 25
            2 3 5 8 15 12 3 25
            2 3 5 8 15 23 3 25
            2 3 5 8 15 23 3 25
            2 3 5 8 15 23 3 25
            2 3 5 8 15 3 23 25
            2 3 5 3 8 15 23 25 25 --- Similarly the maximum round and without comparison with 67,98, following the same token

            The fourth round six times compare
            2 3 5 8 15 3 23
            2 3 5 8 15 3 23 
            2 3 5 8 15 3 23
            2 3 5 8 15 3 23
            2 3 5 8 3 15 23

            5 fifth round of comparisons
            2 3 5 8 3 15
            2 3 5 8 3 15
            2 3 5 8 3 15
            2 3 5 3 8 15
            2 3 5 3 8 15

            The sixth round 4 comparisons
            2 3 5 3 8
            2 3 5 3 8
            2 3 3 5 8
            2 3 3 5 8

            第七轮             3次比较
            2 3 3 5 --2跟3比较
            2 3 3 5 --3跟3比较
            2 3 3 5 --3跟5比较

            第八轮              2次比较
            2 3 3 --2 3
            2 3 3 --3 3

            第九轮              1次比较
            2 3

总结:数组有10个数值,十个数值只需要进行9轮比较

轮数 = 数组length - 1

每一轮的比较次数 = 数组长度 - 当前轮数

 for循环i从零开始,所以当前轮数 = i + 1

 相同的结果,但这次console.log(1)只进行了45次。

 1         var a = [5, 8, 3, 2, 15, 67, 98, 12, 3, 25];
 2         for (let i = 0; i < a.length - 1; i++) {
 3             for (let j = 0; j < a.length - i - 1; j++) {
 4                 if (a[j] > a[j + 1]) {//如果从大到小,只需a[j]<a[j+1]
 5                     let temp = a[j];
 6                     a[j] = a[j + 1];
 7                     a[j + 1] = temp;
 8                 }
 9                 console.log(1);//45次
10             }
11         }
12         console.log(a);//Array(10) [ 2, 3, 3, 5, 8, 12, 15, 25, 67, 98 ]


Guess you like

Origin www.cnblogs.com/yznotes/p/12603067.html