JavaScript code for Binary Search

Source: Internet
Author: User

Generally, binary data is used on Int ..... in JS, A-Z may be used more flexibly, or pinyin may be used... or ......

However, it is worth pondering whether the following process is worthwhile to achieve binary search such as pinyin:

1. Sorting pinyin, looks likeCodeA large volume.

2. Then, perform a secondary search. In this case, we need to identify the size of Pinyin. It seems that it is not too small.

The speed of finding the results is faster, but the speed of your JS files is much slower.

The following code can even be found as soon as 1 billion, but the array is created in the example mode... So stop trying. Just give me a thought. Next time I will send a JS solution to the eight queens problem.AlgorithmWonderful.

Copy codeThe Code is as follows: var array = [];
Var key = 482;
VaR number = 1000;

for (I = 0; I array. push (I);
}< br> // -->
var time = new date ();
var;
var left = 0;
var right = array. length;
while (left <= right) {
var center = math. floor (left + right)/2);
If (array [center] = Key) A = center;
If (Key right = center-1;
}else {
left = center + 1;
}< BR >}< br> alert ("binary search result:" + a);
alert (new date ()-Time) /1000);

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.