Algorithms: Sequential lookups and binary lookups

Source: Internet
Author: User

Data excerpt from:< data structure C + + language description >

typedef int DATATYPE;

//Sequential lookup algorithm
Finds the element with key equivalent in the N-ary array list in order to return the subscript of the array element
If not found, returns-1
int Seqsearch (DataType list[], int n, DataType key)
{
for (int i = 0; i < n; i++)
{
if (list[i] = = key)
{
return i;
}
}
return-1;
}
/*
* The complexity of sequential lookups varies greatly between best and worst case scenarios. The best case is that the first element in the table is both the element to be found and the time of the operation is O (1).
* Worst case scenario is until you find the last element in the table or the element you are looking for does not exist at all, you need to find all n elements with an O (n) complexity. General situation
* The number of searches below is less. For random tables, the equivalent element may be found anywhere in the table. Find the average lookup length equal to the number of executions long enough
* Degree is N/2, that is, N/2 is the expected number of lookups. Therefore, we say that the average performance of the sequential lookup algorithm is O (n).
*/

//binary find
/*
* Sequential lookups can be applied to any table. If the table being looked for is ordered, it can be found using the binary lookup algorithm, which is an improved lookup algorithm. People in the phone number
* The way to find phone numbers in a book is the binary lookup algorithm, which generally finds the first, middle, and rear parts of a number based on the first character of a given name, perhaps
* Fortunately, the corresponding page number was found. If not found, you can also follow the relative position of the name to look forward or backward to find the number book. For example, to find the name of the person who
* "R" begins, and when you turn the page "T", you should flip forward. Repeat the operation until you find the appropriate phone number or make sure that the name is not in the number book.
* This is also the way we use to find an ordered table. The middle element in the table is first found, and its value is compared to the value we requested, and if not equal, the value is based on the
* To find the size of the value to determine the first half or the second part of the table. Typically, this algorithm can be used to shorten the lookup time if the table is orderly.
* Assume that the table is stored as an array. There are n elements in the array, the subscript range is low=0 and hight=n-1, then the binary lookup algorithm can be described as follows:
*/
Find the key value in an ordered array using the binary lookup algorithm. If found, returns its subscript value, otherwise returns-1
int Binsearch (DataType list[], int low, int. high, DataType Key)
{
int mid;
DataType Midvalue;
while (Low <= high)
{
MID = (int) (low + high)/2;
Midvalue = List[mid];
if (key = = Midvalue)
{
return mid;
}else if (Key < Midvalue) {
High = mid-1;
}else{
Low = mid + 1;
}
}
return-1;
}

Algorithms: Sequential lookups and binary lookups

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.