Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int main()
- {
- int j, low, high, mid, n, search, array[50];
- printf("Enter number of elements\n");
- scanf("%d",&n);
- printf("Enter %d integers\n", n);
- for (j = 0; j < n; j++)
- scanf("%d",&array[j]);
- printf("Enter value to find\n");
- scanf("%d", &search);
- low = 0;
- high = n - 1;
- mid = (low+high)/2;
- while (low <= high) {
- if (array[mid] < search)
- low = mid + 1;
- else if (array[mid] == search) {
- printf("%d found at location %d.\n", search, mid+1);
- break;
- }
- else
- high = mid - 1;
- mid = (low + high)/2;
- }
- if (low > high)
- printf("Not found! %d isn't present in the list.\n", search);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement