Bubble sort, array de-weight, determine the number of occurrences of each value in the array ...

Source: Internet
Author: User

Don't worry about the road without bosom friend, the world who does not know June---Don Gao Shi "don't Dongda two first"

In this article, array sorting, array de-weight, judging the number of occurrences, do not use the method of the array.

The specific process thought in the code comment

First, bubble sort

//Bubble Sort:vararr = [1,2,3,5,4]; varJ,temp,flag; varCount =0;//number of wheels used to record comparisons//the number of times the outer loop control compares//Use assumption Method: (How to tag with variables)//1 Find an indeterminate operation in the program, set the assumptions//2 validation of assumptions (setting a condition that allows assumptions to fail)//3 Test The final value of the hypothetical condition, set the operation for(vari =0; I < arr.length-1; i++) {Count++; Flag=true;//Let's say you can sort of jump out of this round for(j =0; J < Arr.length-1I J + +){        if(Arr[j] > arr[j+1]) {temp=Arr[j]; ARR[J]= arr[j+1]; Arr[j+1] =temp; //as soon as you enter the IF, the ordering may not be complete, set the assumed condition to false//Stop JumpingFlag =false; }      }        if(flag) { Break; }} console.log (Arr,count);

First, the array to weight

//array de-weight:vararr = [5,1,2,1,2,2,1,2,1,2,3,4,3,4,5,3,4,5,4,3,2,1]; //the desired result is [1,2,3,4,5]//Console.log (Resultarr);//[1,2,3,4,5]//1 Creating an array of results varResultarr = [arr[0]]; varFlag; //2 times the enumeration group for(vari =1; i < arr.length; i++){      //the right way to achieveFlag =true;//assuming that the current arr[i] can be put into Resultarr for(j =0; J < Resultarr.length; J + +){        //set conditions that allow assumptions to fail: if you encounter any one of the elements equal, that means you cannot putif(Arr[i] = = =Resultarr[j]) {Flag=false; }      }      //set the final action based on the value of flagif(flag) {Resultarr.push (arr[i]); }} console.log (Resultarr);

简单介绍一下Set:Setis a new object in the ES6, which is especially convenient for the array. Only two lines of code are required

 //first defines a repeating array 
var arr = [1,1 , " Span style= "color: #800080;" >2 , 2,2 , 3,3 , 4,4 ] var set = new Set (arr) // {1,2,3,4} var newArr = Array. from (set ) // console.log (NEWARR) // [1,2,3,4]

The principle of implementation: the difference is that Set Array Array duplicate elements are allowed in, for example, [1,2,2,3] and Set all elements in are unique and can only be {1,2,3} . With this feature, we can quickly remove duplicate elements from the array.

Iii. Number of occurrences

The first of these methods
The first type:

/*var arr = [ 5,1,2,1,2,2,1,2,1,2,3,2,2,1,2,1,2,3,2,2,1,2,1,2,3,2,2,1,2,1,2,3,2,2,1,2,1,2,3,2,2,1,2,1,2,3,2,2,1,2,1,2,3,2,2,1,2,1,2,3,4    , 5,3,4,5,4,3,2,1];    Want to know how many times the array of arr 1 appears?    var count = 0;      for (var i = 0; i < arr.length; i++) {if (arr[i] = = 1) {count++; }    }*/    //want to detect five of the data, the number of results obtained is also fivevarCountarr =[],count; //take out each data to be counted Arr[i] for(i =0; i < resultarr.length; i++) {Count=0;//Initialize the Count of Arr[i]//use Resultarr[i] to compare counts with each element in the original array arr for(j =0; j<arr.length;j++){        if(Resultarr[i] = = =Arr[j]) {Count++; }      }      //Save the value of Count to CountarrCountarr.push (count); } console.log (Countarr);

The second method of

Bubble sort, array de-weight, determine the number of occurrences of each value in the array ...

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.