Learn JS Third Day summary

Source: Internet
Author: User

1. Bubble Sort Analysis:

Example: the array [9,8,7,6,5,4,3,2,1,0] in the order of the small size of the bubble sort.

Evolution Process:

First trip:

8,7,6,5,4,3,2,1,0,9 was compared 9 times;

Second trip:

7,6,5,4,3,2,1,0,8,9 was compared 8 times;

Third trip:

6,5,4,3,2,1,0,7,8,9 was compared 7 times;

Four trips:

5,4,3,2,1,0,6,7,8,9 was compared 6 times;

Five trips:

4,3,2,1,0,5,6,7,8,9 was compared 5 times;

Six trips:

3,2,1,0,4,5,6,7,8,9 was compared 4 times;

Seventh trip:

2,1,0,3,4,5,6,7,8,9 was compared 3 times;

Eighth Trip:

1,0,2,3,4,5,6,7,8,9 was compared 2 times;

Nineth Trip:

0,1,2,3,4,5,6,7,8,9 was compared 1 times;

The outer loop loops 1 times and the inner layer loops 9 times.

The outer number is the array length (ARRAY.LENGTH-1), set the outer loop for (Var i=0;i<array.length-1;i++)

The number of internal comparisons decreases with the increase of the number of outer crossings, but the number of comparisons with the number of array.length of the outer layer is 10, which is the array length. So the inner loop is the for (Var j=0;j<array.length-1-i)---- Minus I is to improve the efficiency no longer need to compare (each time the number of comparisons is decreasing).

Instance:

Bubble sort, small to large [65,97,76,13,27,49,58]
var array=[65,97,76,13,27,49,58];
For (Var i=0;i<array.length-1;i++) {
For (Var j=0;j<array.length-1-i;j++) {
if (Array[j]>array[j+1]) {
var temp=array[j];
ARRAY[J]=ARRAY[J+1];
Array[j+1]=temp;
}
}
}
Console. log (array);
Sort from big to small
var array=[65,97,76,13,27,49,58];
For (Var i=0;i<array.length-1;i++) {
For (Var j=0;j<array.length-1-i;j++) {
if (Array[j]<array[j+1]) {
var temp=array[j];
ARRAY[J]=ARRAY[J+1];
Array[j+1]=temp;
}
}
}
Console. log (array);

2. Functions

function: A mechanism of code reuse or a code snippet that encapsulates a function.

Definition of the function:

function name ([parameter list]) {

function body (both executable or code to execute);

}

Function Name: General is a verb, representing a certain function, the name of the best also follow the hump naming law.

function invocation: function name ();

Benefits of a function: one declaration or definition, and multiple invocations.

The composition of the function----three elements: the three elements of the function: function name (function), parameter, return value

Each function has a return value, and if there is no definite return value, it is generally returned undefined.

The shape of the function participates in the argument:

When a function is called, a copy of the function's arguments is copied to the formal parameter, and the number of the function's shape participation arguments can be different.

var i= 0;
function Getsum (a,b,c) {///in the definition of functions, the parameter used is called the formal parameter, the role of the position to occupy
Arguments
A = a | | 0;
b = B | | 0;
c = C | | 0;
console. log (A+b+c);
}

Getsum (10,20);//When the function is actually called, the incoming data is the actual argument

The use of return in a function:

Return generally returns the data in the function, and if followed by the data, then the data is the last return value of the entire function. When the program runs, it returns the value first, then jumps out of the current function, and the subsequent code is no longer executed. If there is no data after the return, the program will immediately jump out of the current function after it has been run, and the code will no longer execute.

function Getsum (A, b) {
A = 100;
return a + B; Return the calculated result with the keyword return
console. log ("See if I have performed ....) "); The code after return is no longer executed
}
var num = Getsum (A, b);
console. log (num*2);

Nesting of functions: a function is called in a function.

Cases:

Find the maximum value in 3 numbers
function Max (A, b) {
if (a>b) {
return A;
}else{
return b;
}

}
function Max2 (a,b,c) {
var D=max (A, b);
var E=max (d,c);
var F=max (Max (A, B), c); function MAX2 called Max's letter//number method
return e;
return F;
}
Console. log (Max2 (4,8,10));

Learn JS Third Day summary

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.