Lookup algorithms: Binary lookup, sequential lookup

Source: Internet
Author: User

enrolled in September 08, graduated in July 12, and ended my happy and rich college life at the Software Academy. This series is a review of four-year professional course studies, indexed in: http://blog.csdn.net/xiaowei_cqu/article/details/7747205

Find algorithm

The lookup algorithm finds a specific target in the existing sequence (list), requiring that each record in the sequence must be associated with a keyword (key) in order to be searched. The lookup algorithm usually requires two inputs: 1, the searched sequence 2, the keyword to find the output parameters and the return value of the algorithm: 1, the return type of the value of Error_code to indicate whether to find success 2, if the lookup succeeds, return success, output parameters position Navigate to the target location 3, if the lookup fails, returns not present, the output parameter may be undefined or any value different from the existing position the idea of a sequential lookup algorithm is simple: from the first element of the table to a downward lookup, if there are elements consistent with the target, find success If there is still no target element to the last element, the lookup fails. "Experimental description" topic: Write a program for sequential table {3,6,2,10,1,8,5,7,4,9}, in order to find the process of keyword 5. Output Required:
1) The original order table, 2) Find the location of the keyword, 3) The number of times to compare.
1. First write the Class table list. The most basic operation needs to be inserted into insert (), get Retrieve (), and get the size sizes ().
2. We observe the topic requirements, although the table stores a simple integer, but if we use the List<int> object obviously cannot record the comparison number, so we write a class key ourselves through its internal int type data member to record the value stored in the table, And imitate the basic logical operation of int, that is, write overloaded logical operators, and increase the number of times that a static data member comparisons is used to record its comparison operation.
3. After the preparation is done, start to write the sequential lookup algorithm. The idea of the algorithm is simple and easy to implement, starting from the first element in the table, and discovering that the target returns the position of the element in the table, and if there is no target after the traversal, the lookup fails, and 1 indicates that there are no target elements in the table. 4. Write the final output function as required by the title. "Related code" function Sequential_search [CPP]View Plaincopy
  1. int Sequential_search (const list<int> &the_list,
  2. Const Key &target)
  3. /*post:if an entry in the_list was equal to target and then return the position
  4. of this entry.
  5. Otherwise return-1
  6. */
  7. {
  8. int position;
  9. int s=the_list.size ();
  10. For (position=0;position<s;position++) {
  11. int data;
  12. The_list.retrieve (Position,data);
  13. if (data==target) {
  14. return position;
  15. }
  16. }
  17. return-1;
  18. }

Binary lookup algorithm Two-point lookup presupposes that the table is a canonical table in ascending or descending order. In this experiment we used an increment table.
Binary lookup finds the target element from the middle of the table. If a consistent element is found, the lookup succeeds. If the intermediate element is smaller than the target element, the second half of the table is still found using the binary lookup method (the table is ascending), whereas the middle element is larger than the target element, and the first half of the table is looked up. "Experimental description" topic: Write a program for the ordered table {1,2,3,4,5,6,7,8,9,10}, using the binary search for the keyword 9 process. Output Required:
1) The original order table, 2) Find the location of the keyword, 3) The number of times to compare.
1. The two-point lookup algorithm is based on the premise that the table must be ordered, such as a table in which the topic is incrementally arranged. The implementation of the Order of the table is user specification input, on the other hand we can also write an ordered class to facilitate user input.
So derive the class oredered_list from list, rewrite the function error_code insert (int position,const Record &data) so that when the inserted position does not satisfy the ordered condition of the table, it cannot be inserted.
The Error_code Insert (const Record &data) is also written with an inserted overloaded function that can be inserted directly into the appropriate location for user input.
2. Still use key in Title 1 to indicate the target
3. Realize the binary search algorithm. By studying in the book, we use the binary search algorithm that adds equality judgments directly. That is, each time the middle element of the table is compared, if the target returns the position of the element in the table, and if the intermediate element is smaller than the target element, the second part of the right half is searched, whereas the first half of the table is searched by binary. If there is no target element at the end, return 1 to indicate that there are no target elements in the table. 4. The output function written in topic 1 is still used to output the result.
/* Note here that because ordered_list is derived from the list, although the first parameter type in the Print_out function is List<int&gt, you can still use it instead of writing the overloaded function */"Related code" function Binary_search [CPP]View Plaincopy
  1. int Binary_search (const ordered_list<int> &the_list,
  2. Const Key &target)
  3. /*post:if an entry in the_list was equal to target and then return the position
  4. of this entry.
  5. Otherwise return-1
  6. */
  7. {
  8. int position;
  9. int data;
  10. int Bottom=0,top=the_list.size ()-1;
  11. While (bottom<=top) {
  12. position= (bottom+top) >>1;
  13. The_list.retrieve (Position,data);
  14. if (data==target)
  15. return position;
  16. if (data<target) bottom=position+1;
  17. else top=position-1;
  18. }
  19. return-1;
  20. }
"Process recording" experiment: "Result analysis" A. Implementing sequential Lookup Algorithm 1. Sequential lookup algorithm The idea is very simple, is a kind of traversal thought, each one finds the target element, realizes is also very simple.
2. Order lookup for tables with n elements. Number of comparisons: unsuccessful: compare n times. Successful lookup: The best case is 1 times, that is, the first element is the target element, the worst case is n times, the average number of comparisons (n+1)/2 times.
So when the table is large, the cost of sequential lookups is significant.
3. The sequential lookup algorithm does not have a duplicate comparison, that is, once found to be successful, but at the same time the cost is that when there are duplicate target elements in the table (for example, there are multiple target elements) we can only get the position of the first element.
B. Implementing a binary search algorithm 1. Two-point lookup method: Increments the table, first from the middle element to find, if the element is smaller than the target element, then look for the second half of the table, reverse the first part of the table, and repeat the process. So we'll halve the length of the table in each search.
2. Two points in the implementation of the amount of bottom and top, the process of halving each time is reflected in the change of bottom and top, in the implementation of the code can use a simple loop or function recursive thinking.
Recursive thought is easier to understand, but after writing we find that the function is the tail recursion, the tail recursion can usually be implemented with a simple loop, the loop does not have the function call process in the operation, save time and space.
3. Code changes in small and medium-sized areas may have a significant impact on the program. Find the binary_search_1 (not equal to the case) binary_search_2 (add equals) as above two binary points
Experiment code Download: http://download.csdn.net/detail/xiaowei_cqu/4437702 (reprint please indicate author and source: Http://blog.csdn.net/xiaowei_cqu Do not use for commercial purposes without permission)

Lookup algorithms: Binary lookup, sequential lookup

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.