Binary Search Algorithm (Review notes)

Source: Internet
Author: User

There is also a common search algorithm before the binary search algorithm: sequential search. the sequential search algorithm can be used in any array. it scans the entire array one by one. you can imagine the time spent. the sequential search algorithm is easy to understand. so I will not take notes. To put it bluntly, the binary search algorithm splits the array into two halves. then you specify the value of a target. then the algorithm searches by selecting the intermediate vertex in the table. if the number you give is smaller than the value of the intermediate point. search for the first half. if it is large, you can find it in the last half. in this way, the sub-ring will continue. until this is found. if it was just right at the beginning. you don't need to find it. take it home. Assume that the target value is MyNum. array is an Array of N elements. the center point is Mid. the First and Last parts of the array are: First Last. the task is to find MyNum in Array. First, calculate the intermediate point: Mid = (First + Last)/2; // the center point position. The subscript is obtained here. Mid_Value = Array [Mid]; // The value is assigned here. In this case, there are three possibilities: NO1. If you are lucky, maybe the number at the center of the first time is the number you are looking. If (Array [Mid] = MyNum) Return location; 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'height =" 158 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1911141917-0.jpg "width =" 490 "border =" 0 "/> Do you understand the difference between First and First-1.Last-1 and Last? You have to review the array knowledge. NO2. Everyone's luck is different. Not everyone's luck is the same as no1. When MyNum is smaller than Mid_Value, the center point is changed. The algorithm will search for the first half. The range is changed to [First, Mid].In this case Locate the Last and assign the position of the Mid to the Last. If (MyNum <Mid_Value) Find the sub-Table Array [First]-Array [Mid-1]; 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'alt =" "src =" http://img1.51cto.com/attachment/200810/200810111223717667999.bmp "border =" 0 "/> NO3. Another case is when MyNum> Mid_Value. In this case, the algorithm searches in the subtable. The range is changed to [Mid + 1, Last). Locate the subscript of First: Mid + 1. If (MyNum> Mid_Value) Locate First to Mid + 1; Find the sub-Table Array [Mid + 1]-Array [Last-1]; 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'alt =" "src =" http://img1.51cto.com/attachment/200810/200810111223717714749.bmp "border =" 0 "/> last, Note two casesQuery for interruptions: 1. When First and Last are crossed (First> = Last). The sub-table is empty. 2. No matching value is found. This is the case with the binary search algorithm. If you need code, click Baidu. There will be a lot of information, mainly understanding, and more training. How do I calculate the time spent? Many friends do not know how to calculate the time spent by algorithms. Of course, there is no way to compare the two algorithms. Here I will give you a timer API: Timer (); // constructor, huh. Does the constructor understand it? If you don't understand it, go to my previous article <constructor> Void start ();//... Void stop ();//... Double time () const; // time difference in seconds. The usage is as follows: ... Timer t; Double ResuleTime; T. start (); ... // Put the code of the time you want to test here. For example, if you write a Search () function, you can put it here. T. stop (); ResuleTime = t. time (); ... ^ Easy to use...... you don't need to use other tools.  

This article is from the "egg" blog and cannot be reproduced!

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.