JavaScript Basics When writing bubble sort: how Javascript's data types and variables are assigned

Source: Internet
Author: User

A problem was encountered while writing the bubbling Sort:

functionbubblesort (arr) {vartemp = 0; Console.log ("incoming array:");    Console.log (arr);  for(vari = 0;i<arr.length;i++) {//Cycle arr.length-1 TimesConsole.log ("outer section" +i+ "secondary cycle ===============start");  for(varj = 0;j<arr.length-1;j++) {//Cycle arr.length-1-1 Times            if(arr[j]<arr[j+1]) {//array of Flashbacks: if the next two, the front one is smaller than the back, swaptemp = arr[j];//-┐arr[j] = arr[j+1];//-├-two values in an array with intermediate variablesarr[j+1] = temp;//-┘} console.log ("inner layer" +j+ "secondary cycle");        Console.log (arr); } console.log ("outer section" +i+ "secondary cycle ===============end"); }    returnarr;}
var array = [2,3,9,5,7,1,8]; var sortarray = bubblesort (array); console.log ("array   " +array);   array   9,8,7,5,3,2,1console.log ("sortarray   " +sortarray);   sortarray   9,8,7,5,3,2,1// Curious Why is the array also sorted? 

Look at the following study:

The first thing I think about is whether to return the Question. Did I forget a Return's key role in my mind?

Test it First:

functionfoo (bar) {bar= 1; returnBar//return 3;}varA = 0;varb =foo (a); console.log (a)//0Console.log (b)//1//if return 3 gets 3//do not add return,functionFoo2 (bar2) {bar2= 1;}vara2 = 0;varB2 =foo2 (a2); console.log (a2)//0Console.log (b2)//undefined
Return is for returning the function value, it really has no special magical effect.
In fact, directly delete the return, try to know if it is, how can I doubt this.

wait, Why does the above a not be modified, is it related to a is a number?

The problem here should be on the array.

What's wrong with the array? This piece of knowledge about the data type and assignment principle of javascript is blurred in my mind.

Let's try the code first:

 functionfoo (bar) {bar= [1,2,3,4]; returnbar;} varA = [a.]; varb =foo (a); console.log (a)//[a]Console.log (b)//[1,2,3,4] functionFoo2 (bar2) {bar2[0] = 0; returnbar2;} varA2 = [a]; varB2 =foo (a2); console.log (a2)//[0,2,3]Console.log (b2)//[0,2,3]

Two functions are also arrays, why are the results different?

The reason for this is that the array is a reference data type


Research on the type of JS data
The data types in JS are divided into:
string,number,boolean,[],{},null,undefined
string, number, boolean, array, object, null,undefined

What is the difference between null and undefined???? -------------------------------------undefined: declaring a variable but not initialized, null, the object cannot be found
Null==undefined//true
Null===undefined//false

The above data types can be categorized
Primitive types and reference types
Original Type: undefined,null,boolean,number,string
Reference Type: [],{}

In many languages, string strings are treated as reference types, not primitive types, because the length of the string is variable, but JS breaks the Tradition.

however, JavaScript has a dynamic type, which means that a variable can be assigned to a different type.

So let's not think about what kind of data type the entangled variable is, and how each data type is, and we're going to think straight from the value of the Variable.

In js, A variable can have two types of values (refer to the data type Above)
Raw values and reference values
Raw values: simple data segments stored in stacks (stack), that is, their values are stored directly in the location of the variable access
Reference value: the object stored in the heap (heap), that is, the value stored at the variable is a pointer (point) to the memory of the storage Object.

therefore, when assigning a value to a variable, the JavaScript interpreter must first determine whether the value is a primitive type or a reference type.
To achieve this, the JavaScript interpreter tries to determine whether the value is one of the original types of Javascript. What the original type has said Above.
In the case of primitive types, because the space occupied by the original type is fixed, in memory they are stored in the stack, which makes it easy to query the value of the variable quickly. (about string length variable see Above)
If it is a reference type, its storage space is allocated from the HEAP. The reason is because the size of the reference value will change, so you can't put them in the stack, otherwise it will reduce the speed of the variable query. So we put the Object's address in the heap on the Stack. The size of the address is fixed, so there is no negative effect on the performance of the Variable.

------------------------------------------------the above related knowledge source: http://www.w3school.com.cn/js/pro_js_value.asp

------------------------------------------------the following knowledge source: https://www.zhihu.com/question/26042362/answer/31903017

So the above problem is good Explanation.
can be simplified into this:

var a = [4,5,6var b == [=]; alert (b);   // [a] var a = [a.popvar b = a; // built-in function, Delete the last alert (b) of the array;  // [up]

This is how it is illustrated:

Even if the function is called
var b = Foo (a)
As long as the operation inside the function directly operates on the incoming array object itself, It is the object in the Operation Heap.
will be modified to ITSELF.
As long as it does not operate itself, it creates a new object in the heap and then modifies the reference address of B in the stack to the Past.
Will not modify Itself.

Expand reading; https://zhuanlan.zhihu.com/p/24080761

JavaScript Basics When writing bubble sort: how Javascript's data types and variables are assigned

Related Article

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.