An introduction to how JavaScript uses the binary method to find data

黄舟
Release: 2017-06-18 11:50:47
Original
2490 people have browsed it

This article mainly introduces the example code ofJavaScriptusing binary method to find data. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor and take a look.

Organize the documents and search out an example code of JavaScript using binary method to find data. Take a note by the way


//二分法查数据 var arr=[41,43,45,53,44,95,23]; var b=44; var min=0; var max=arr.length; for(var i=1;iarr[zjs]){ min=zjs; } else{max=zjs}}
Copy after login

Find the total score of 10 scores, the highest score, the lowest score


var arr = new Array(80,70,86,58,90,35,89,67,50,100); var sum = 0; var maxd = 0; var mind = 100; //想要查最小数最好用满分最大数做可以比较的基数 for(var i=0;imaxd){ maxd = arr[i]; } if(arr[i]
        
Copy after login

Add a unique number to the array


//var a=7; var a=parseInt(prompt("请输入一个数")); var x=0; var arr=[1,2,3,4,5] for(var i=0;i
        
Copy after login

I have never been exposed to similar questions before using binary search data. I can’t think of any ideas. After the teacher finished speaking, I still seemed to understand but I didn’t understand until I typed it out.Bubble sortingIt looks quite easy, but when it comes to typing the code yourself, you make mistakes again. You can't always compare the gourds to the gourds, you should have your own understanding of why you type like this, and type more code to avoid similar mistakes in the future. Small mistakes, thevariablesyou define must be remembered, and they echo before and after. When you can't define it, use one, and when you use it, use another one. Remember to add semicolons and curly braces.

The above is the detailed content of An introduction to how JavaScript uses the binary method to find data. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!