JavaScript realizes the code _javascript skill by realizing the binary lookup method

Source: Internet
Author: User
The general two points are used to int[] type .... In JS may be more flexible to use a-Z, or use pinyin ... or use the ...

But one of the questions worth thinking about is if you're looking for a binary search for pinyin. And the following process is worth it:

1. On the phonetic sorting, it seems that the amount of code is not small.

2. And then two points to find. This need to recognize the size of pinyin, it seems not too small bar.

Find results faster, but others under your JS file speed is much slower, oh, in the end who to abandon.

The following code can even be found in 1 billion, as soon as possible, but use the example pattern to create that array ... So don't try. Just give a thought, next time I will send a JS eight queen problem solution, hehe algorithm is very wonderful OH


Copy Code code as follows:

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

for (i=0;i<number;i++) { 
 array.push (i ); 

//-->> 
var time = new date (); 
VAR&NBSP;A;&N Bsp
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 (Results of the binary lookup search: "+a); 
Alert (new date () &n Bsp;- 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.