Javascript implements the binary search method to implement code _ javascript skills

Source: Internet
Author: User
Javascript implements the binary search method. Generally, binary code is used in 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, it seems that the amount of code is not small.

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. I just want to give a thought. Next time I will send a js solution to the eight queens problem. The algorithm is amazing.


The Code is as follows:


Var array = [];
Var key = 482;
Var number = 1000;

For (I = 0; I Array. push (I );
}
// -->
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 <array [center]) {
Right = center-1;
} Else {
Left = center + 1;
}
}
Alert ("result of the Binary Search Method:" + );
Alibaba (new Date ()-time)/1000 );

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.