Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int main() {
- int size, i, key, low, high, mid, found = 0;
- printf("Enter the size of the array: ");
- scanf("%d", &size);
- int arr[size];
- printf("Enter the elements of the array in ascending order:\n");
- for (i = 0; i < size; i++) {
- scanf("%d", &arr[i]);
- }
- printf("Enter the element to search: ");
- scanf("%d", &key);
- low = 0;
- high = size - 1;
- while (low <= high) {
- mid = (low + high) / 2;
- if (arr[mid] == key) {
- found = 1;
- break;
- } else if (arr[mid] < key) {
- low = mid + 1;
- } else {
- high = mid - 1;
- }
- }
- if (found) {
- printf("Element %d found at index %d\n", key, mid);
- } else {
- printf("Element %d not found in the array\n", key);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment