mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
Ubiquitous_Binary_Search algorithm added
This commit is contained in:
parent
e5dad3fa8d
commit
5b2c4d73e6
42
searching/Ubiquitous_Binary_Search.c
Normal file
42
searching/Ubiquitous_Binary_Search.c
Normal file
@ -0,0 +1,42 @@
|
||||
#include<stdio.h>
|
||||
int UbiquitousBinarySearch(int A[], int lt, int rt, int key)
|
||||
{
|
||||
int m;
|
||||
|
||||
while( rt - lt > 1 )
|
||||
{
|
||||
m = lt + (rt-lt)/2;
|
||||
|
||||
if( A[m] <= key )
|
||||
lt = m;
|
||||
else
|
||||
rt = m;
|
||||
}
|
||||
|
||||
if( A[lt] == key )
|
||||
return lt;
|
||||
if( A[rt] == key )
|
||||
return rt;
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int l, r, p, key;
|
||||
|
||||
// Get the array
|
||||
// Sort the array if not sorted
|
||||
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
|
||||
|
||||
// Starting index
|
||||
l = 0;
|
||||
// length of array
|
||||
r = 9;
|
||||
key = 4;
|
||||
// Search the key using ternarySearch
|
||||
p = UbiquitousBinarySearch(arr,l,r,key);
|
||||
|
||||
// Print the result
|
||||
printf("Index of %d is %d\n", key, p);
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user