Sort () sorts the arrays without opening up new memory, swapping the original array elements//1, simple arrays simple sorting. Sort ()//var arrsimple = [1,8,7,6,11]//function order (value1,value2) {// Return value1-value2;//}//Console.log (Arrsimple.sort (order))
Bubble sort: 22 comparisons, in liters for example, have been compared to the last item//splice ()//parameter 1:index: Positive delete position, negative number from end count//Parameter 2: must, to delete the quantity, set to 0 does not delete item//Parameter 3: Optional, add new item to array// var arr = [1,8,9,2,7,6]//function Bubblesort (arr) {//for (Var i=arr.length-1;i>0;i--) {//for (Var j=0;j<i;j++) {/ /if (arr[j]>arr[j+1]) {//var temp = arr[j]//Arr.splice (j,1,arr[j+1])//Arr.splice (J+1,1,TEMP)//}//}//}/RET Urn arr; // }
Select sort: Remove Max and Min from the currently indeterminate module to the front or back//var arr = [1,8,9,11,2,7,6]//function Bubblesort (array) {//for (Var I=0;i<ar r.length;i++) {//var min = arr[i]//var minindex = I//for (var j = i+1;j<arr.length;j++) {//if (Min>arr[j]) {//M in = Array[j]; Minindex = j; }//}//Arr.splice (i,0,min); Arr.splice (minindex+1,1)//Console.log (ARR)//}//Return arr; // }
Insert Sort: Select an array that is already sorted, in contrast, until you find one that is less than his, insert it into this front/function Insersort (arr) {//var start = 1;//for (Var i=start;i<arr.le ngth;start++,i++) {//for (j=0;j<start;j++) {//if (Arr[i]<=arr[j]) {//Arr.splice (J,0,arr[i])//Arr.splice (i+1,1 )//break; // } // } // } // }
Quick Sort//var times=0; var arr = [1,3,9,2,5,8,13]//var quicksort=function (arr) {/////If the array length is less than or equal to 1 no need to determine the direct return can//if (arr.length<=1) {//return Arr }//var midindex=math.floor (ARR.LENGTH/2);//base point//Var midindexval=arr.splice (midindex,1);//Take datum point value, Splice (index, 1) The function can return the number of deleted in the array arr[index+1]//var left=[];//to hold a smaller array than the Datum//var right=[];//to hold an array larger than the datum point////Iterate the array, make a judgment assignment//for (Var i=0;i <arr.length;i++) {//if (Arr[i]<midindexval) {//Left.push (Arr[i]),/////Console.log (left + '-------- ---1111111111111 ')//}//else{//Right.push (Arr[i]);//The right-hand array is larger than the reference point//Console.log (right+ '---------------- 22222222222222 ')//}//Console.log ("First" + (++times) + "post-order:" +arr); }////recursively perform the above operation on the left and right two arrays until the array length is <=1;//return quickSort. Concat (Midindexval,quicksort (right));//Var ARR1 = QuickSort (left). Concat (Midindexval,quicksort);//Console.log (ARR1)//}; Console.log (QuickSort (arr));
for (i=0;i<5;i++) {setTimeout (function () {console.log (i)},1000)}//function Makefn () {//var num = 0;//function f () {// num++;//console.log (num);//}//return f;//}//var func = Makefn ();
How to sort arrays