JavaScript uses the binary method to search for data instance code.

Source: Internet
Author: User

JavaScript uses the binary method to search for data instance code.

Sort out the document, search for a JavaScript sample code that uses the binary method to search for data, and take a note.

// Query data using the binary method var arr = [, 43, 23]; var B = 44; var min = 0; var max = arr. length; for (var I = 1; I <arr. length; I ++) {// number of times the outer loop controls the sorting for (var j = 0; j <arr. length-I; j ++) {// Number of inner loop control loops if (arr [j] <arr [j + 1]) {z = arr [j]; arr [j] = arr [j + 1]; arr [j + 1] = z ;}}// alert (arr [3]) while (true) {// The number of binary queries is to first sort a group of data in order and then split the data into two parts from the middle to see the range in which the number is to be queried and then split it into two parts, until/* for (var I = 0; I <arr. length; I ++) * // is this incorrect? Var zjs = parseInt (min + max)/2) until this number; // because (min + max)/2 may be decimal, therefore, add an integer to forcibly convert if (zjs = min) {when the number array to be searched is an even number array, the remaining number is 2, you need to add a condition to judge if (B = arr [zjs + 1]) {alert (zjs + 1); break ;}} if (B = arr [zjs]) {alert (zjs) break;} else if (B> arr [zjs]) {min = zjs;} else {max = zjs }}

Calculate the total score for 10 scores, the highest score, and the lowest score.

Var arr = new Array (50,100, 100,); var sum = 0; var maxd = 0; var mind =; // If You Want to query the minimum number, it is best to use the maximum number of full marks as the base number that can be compared (var I = 0; I <arr. length; I ++) {sum = sum + arr [I]; if (arr [I]> maxd) {maxd = arr [I];} if (arr [I] <mind) {mind = arr [I] ;}} alert (sum); alert (maxd); alert (mind );

Add a non-repeated number to the array

// Var a = 7; var a = parseInt (prompt ("enter a number"); var x = 0; var arr = [1, 2, 4, 4, 5] for (var I = 0; I <arr. length; I ++) {if (a = arr [I]) {x = 1; break ;}} if (x = 0) {arr. push (a)} alert (arr. length)

I have never touched on similar questions before using the binary search data. I can't think of any ideas. After the teacher finishes talking about it, I still feel like I understand it. It's quite easy to find the bubble sort, but when I tried to knock on the code myself, I made a mistake. I couldn't always think about it better than Hulu. I should have my own understanding. Why do I need to think so much about the code, to avoid similar small errors in the future, remember to use the variables defined by yourself. They echo each other before and after. If they cannot be defined, remember to add them with semicolons, remember to add curly braces.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.