/* 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