Array, bubbling sort

Source: Internet
Author: User
Tags rounds

<!DOCTYPE HTML><HTML><HeadLang= "en">    <MetaCharSet= "UTF-8">    <title></title></Head><Body><Script>    //finding and averaging all the numbers in a set of numbers/*var arr = [10,20,30,10];    var len = arr.length;    var sum = arr[0];    var avg;    for (var i =1;i<len;i++) {sum + = Arr[i];    } avg = Sum/len;    Console.log (sum+ "/" +len); Console.log (avg);*//*var arr = [11,2,31,1,4,134,1,41,42,131,434];    var sum = arr[0];//Gets the value of the first index to the array var avg;        The first value in the for (Var i=1;i<arr.length;i++) {//Factor group has been taken out so that it starts directly from index 1 so i=1;    Sum + = arr[i];//of the elements in the array and avg = sum/arr.length;//divided by the length of the array to find the mean Console.log (sum+ "/" +arr.length); Console.log (avg);*/    //find the maximum and minimum values in a set of numbers, and where /*var arr = [1,2,4,5,6]; var max = arr[0];//First takes the value of index 0 and compares it to the following value var Maxindex = 0;//Because Max assigns a value of index 0 to the index defines the maximum worthwhile variable start value is also 0 var min = arr[0];//First FETCH index  A value of 0 is then used to compare with the following value var Minindex = 0;//The value index to index 0 is defined as the maximum worthwhile variable start value and 0 for (var i =0;i<arr.length;i++) {//Factor group The first value has been fetched            So starting from 1//(maximum) determine when Max value is less than arr[i], assign arr[i] to max index value equals i if (Max<arr[i]) {max = arr[i];        Maxindex = i;            }//(minimum value) if (Min>arr[i]) {min = arr[i];        Minindex = i;    }}/!* Console.log ("Maximum value" +max);    Console.log ("Max index" +maxindex);    Console.log (' minimum value ' +min);    Console.log (' minimum index ' +minindex); *!/console.log ("maximum" +max);    Console.log ("Max index" +maxindex);    Console.log ("Minimum Value" +min); Console.log ("minimum Index" +minindex);*/    //to split a string array with a | or other symbol/*var arr = ["Flowing Water", "Dragonfly", "floating clouds"];    var str = arr[0];    var part = ' | ';    for (var i =1;i<arr.length;i++) {str+=part+arr[i]; } console.log (str);*//*var arr = [' number ', ' moment is classic ', ' casually '];    var str = arr[0];    var part = '? ';    for (var i =1;i<arr.length;i++) {str + = part + arr[i]; } console.log (str);*/    //remove the value of 0 in the array, not 0 in the new array/*var arr = ["My", 0,0,0, "his", 1,2,3,0, "Who's"];    /!* alert (arr);    alert (arr.length); *!/var newArr = [];        for (Var i=0;i<arr.length;i++) {if (arr[i]!=0) {newarr[newarr.length] = Arr[i]; }} console.log (NEWARR);*//*var arr = ["My", 0,0,0, "his", 1,2,3,0, "Who's"];    var newArr = [];        for (Var i=0;i<arr.length;i++) {if (arr[i]!=0) {newarr[newarr.length] = Arr[i]; }} console.log (NEWARR);*/    /*var arr = [1,2,4,23,4,9,5,8,3,0,39,0];    var newArr = [];        for (var i =0;i<arr.length;i++) {if (arr[i]!=0) {newarr[newarr.length] = Arr[i]; }} console.log (NEWARR);*/    //flipping an array/*var arr = [1,2,4,23,4,9,5,8,3,0,39,0];    Console.log (arr);    var newArr = [];    for (var i = arr.length-1;i>=0;i--) {Newarr[newarr.length] = Arr[i]; } console.log (NEWARR);*/    //bubble Sort, small to large  /*var arr = [1,2,4,23,4,9,5,8,3,0,39,0];    Console.log (arr);    var m = 0;    var n = 0;        Compare the number of rounds for (Var i=0;i<arr.length-1;i++) {//control the switch after the end of the direct-hop number of switches var onOff = true; The number of times the comparison is cycled, and after each cycle there are elements that are no longer compared so minus I for (var j =0;j<arr.length-1-i;j++) {//Determine when the position is swapped if (Arr[j]                    >arr[j+1]) {var temp = arr[j];//Save the previous variable arr[j] = arr[j+1];                    ARR[J+1] = temp;            The default execution continues onOff = false;        } n++;        } m++;        if (ONOFF) {break; }} console.log (arr);*/    vararr= [1,2,345,3, -, About, the, the,990,0];//define an arrayConsole.log (arr); varm= 0;//number of rounds to compare    varN= 0;//Number of comparisons    //the first cycle controls the number of rounds compared to the second     for(varI=0; I<Arr.length-1; I++){        //defines a switch to control the number of comparisons when the judgment is no longer executed when the hop count comparison cycle        varOnOff= true; //cycle that defines the number of comparisons         for(varJ=0; J<Arr.length-1-i;j++){            //deciding when to place a position swap            if(Arr[j]<arr[j+1]){                varTemp=Arr[j]; ARR[J]=arr[j+1]; Arr[j+1] =temp; //switch is false compare continueOnOff= false; } N++;//number of comparisons self-increment} m++;//compare number of rounds self-increment        if(ONOFF) { Break; }} console.log ("Number of wheels"+m); Console.log ("number of"+N); Console.log (arr);</Script></Body></HTML>

Array, bubbling sort

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.