0% found this document useful (0 votes)
14 views1 page

BinarySearch 101902

Uploaded by

tirthajitdas78
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views1 page

BinarySearch 101902

Uploaded by

tirthajitdas78
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <stdio.

h>
#include <conio.h>

int main()
{

int arr[100], n, key, i, lb, ub, mid, flag;

printf("Enter the size of the Array: ");


scanf("%d", &n);

printf("\nEnter the elements in Ascending Order:\n");


for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}

printf("\nEnter the Keyvalue you want to search: ");


scanf("%d", &key);

lb = 0;
ub = n - 1;

for (i = lb; i <= ub; i++)


{
mid = (lb + ub) / 2;

if (arr[mid] == key)
{
flag = 1;
}

else if (arr[mid] > key)


{
ub = mid - 1;
}

else
{
lb = mid + 1;
}
}

if (flag == 1)
{
printf("\nThe value found on arr[%d]...\n", mid);
}

else
{
printf("\nValue not found...\n");
}

return 0;
}

You might also like