#include <stdio.h>
void binary_ser(int array[], int first, int last, int n)
{
int i ,mid;
mid = (first + last) / 2;
while (first <= last) {
if (array[mid] < n)
first = mid + 1;
else if (array[mid] == n) {
printf("%d found at location %d.\n", n, mid+1);
break;
}
else
last = mid - 1;
mid = (first + last) / 2;
}
if ( first > last )
printf("Not found! %d is not present in the list.\n", n);
}
search(int arr[], int size, int data)
{
int p = (size - 1) / 2, low, high, a1 = 0, a2 = 1, i = 1;
low = p + a1;
high = p + a2;
while(i)
{
if(data >= arr[low] && data <= arr[high])
{
binary_ser(arr, low, high, data);
break;
}
else if(data < arr[low])
{
binary_ser(arr, 0, low, data);
break;
}
else
{
a2 = a2 * 2;
low = high;
high = p + a2;
}
}
}
int main()
{
int a[200], i, j, n, size;
printf("Enter the size of the array:");
scanf("%d", &size);
printf("Enter %d Number in ascending \n", size);
for (i = 0; i < size; i++)
scanf("%d", &a[i]);
printf("Enter element to search\n");
scanf("%d", &n);
search(a, size, n );
return 0;
}
Bhuvan Arora
No comments:
Post a Comment