IOS algorithm (5) half lookup

Source: Internet
Author: User
Binary search, also known as semi-query, has the advantage of a small number of times, fast query speed, and good average performance. Its disadvantage is that the table to be queried is an ordered table and it is difficult to insert or delete data. Therefore, the half-fold lookup method is suitable for searching frequently ordered lists without frequent changes.

Binary search, also known as semi-query, has the advantage of a small number of times, fast query speed, and good average performance. Its disadvantage is that the table to be queried is an ordered table and it is difficult to insert or delete data. Therefore, the half-fold lookup method is suitable for searching frequently ordered lists without frequent changes.

Two methods of half-lookup

Half-lookup method:

In an ordered table, you can compare the value of the data to be searched with the value of the element in the search range. There are three situations:

1) if the data value to be searched is exactly the same as the value of the intermediate element, the index of the intermediate element value is put back.

2) if the data value to be searched is smaller than the value of the intermediate element, the first half of the entire search range is used as the new search range. execute 1) until an equal value is found.

3) If the value of the data to be searched is greater than the value of the intermediate element, the second half of the entire search range is used as the new search range. execute 1) until an equal value is found.

4) if no equal value is found at the end, an error message is returned.

According to the binary tree, the middle value is the root of the binary tree. the first half is divided into the left subtree, and the second half is the right subtree. The number of searches by the half lookup method is exactly the number of layers where the value is located. In the case of equal probability, it is about log2 (n + 1)-1


Code implementation:

// Main. m

// Algorithm ---- half-lookup

// Copyright (c) 2014 summer2014mht@sina.com. All rights reserved.

# Import

Int main (int argc, const char * argv [])

{

Int array [] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

Int count = sizeof (array)/sizeof (array [0]);

Int target = 10;

Int start = 0, end = count-1, mid = 0;

While (start <= end ){

Mid = (start + end)/2;

If (array [mid]> target ){

End = mid-1;

} Else if (array [mid] <target ){

Start = mid + 1;

} Else {

Break;

}

}

If (start <= end ){

Printf ("[% d]: % d \ n", mid, array [mid]);

} Else {

Printf ("not found \ n ");

}

Return 0;

}

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.