Binary Lookup algorithm

Source: Internet
Author: User

/* date:2014.12.16

The binary lookup algorithm, also called a binary lookup, is used to find the owning unique element.

It needs to be sequenced before it can be done. Applies only to ordered tables.
*/
Quickly sort first: (Here the order is added or descending)
void QuickSort (int *arr,int left,int right)
{
int key,temp,ltemp,rtemp;

Ltemp = left;
Rtemp = right;
key=arr[(left + right)/2];

while (Ltemp <= rtemp)
{
while (Arr[ltemp] < key)
{
Ltemp + +;
}
while (Key < arr[rtemp])
{
Rtemp--;
}
if (ltemp <= rtemp)
{
temp = arr[ltemp];
Arr[ltemp] = arr[rtemp];
ARR[RTEMP] = temp;
Ltemp + +;
Rtemp--;
}
}

if (left < rtemp)
{
QuickSort (arr,left,rtemp);
}
if (Ltemp < right)
{
QuickSort (Arr,ltemp,right);
}
}
Binary lookup for ordered tables (sequential increments)

int binarysearch (int *arr,int n,int x)
{
int Low,mid,high;
Low = 0;
High = n-1;
while (Low <= high)
{
Mid = (low + high)/2;
if (arr[mid] = = x)
{
return mid;
}
else if (Arr[mid] > x)
{
High = mid-1;
}
Else
{
Low = mid + 1;
}
}
return-1;
}



Binary Lookup algorithm

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.