Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int binarysearch (int arr[], int high, int low, int key)
- {
- int mid;
- while (high >= low)
- {
- mid = (high + low) / 2;
- if(arr[mid] == key)
- {
- return mid + 1;
- }
- else if( key > arr[mid])
- {
- low = mid + 1;
- }
- else
- {
- high = mid - 1;
- }
- }
- return -1;
- }
- int main()
- {
- int key, loc;
- int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
- printf("Enter the Key: ");
- scanf("%d",&key);
- loc = binarysearch (a, 9, 0, key);
- if (loc == -1)
- {
- printf("Search Unsuccessful. Input a correct key.\n");
- }
- else
- {
- printf("The location of Key %d is %d", key, loc);
- }
- getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement