0% found this document useful (0 votes)
6 views

Exp 11 DS

Uploaded by

robincyril712
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)
6 views

Exp 11 DS

Uploaded by

robincyril712
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>

int binarySearch(int arr[], int left, int right, int key) {


while (left <= right) {
int mid = left + (right - left) / 2;

if (arr[mid] == key) {
return mid;
}
else if (arr[mid] < key) {
left = mid + 1;
}
else {
right = mid - 1;
}
}
return -1;
}

int main() {
int arr[8], key1, key2, result1, result2;

printf("Enter 8 elements in sorted order:\n");


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

printf("Enter the first element to search: ");


scanf("%d", &key1);

printf("Enter the second element to search: ");


scanf("%d", &key2);

result1 = binarySearch(arr, 0, 7, key1);


result2 = binarySearch(arr, 0, 7, key2);

if (result1 != -1)
printf("Element %d found at index %d.\n", key1, result1);
else
printf("Element %d not found.\n", key1);

if (result2 != -1)
printf("Element %d found at index %d.\n", key2, result2);
else
printf("Element %d not found.\n", key2);

return 0;
}

You might also like