Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Program to search an element using Binary Search.
- #include <stdio.h>
- #include <conio.h>
- void main () {
- int i, n, mid, first, last,
- arr[11] = {
- 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
- };
- clrscr ();
- printf ("Enter element: ");
- scanf ("%d", &n);
- first = 0;
- last = 10;
- while (first <= last) {
- mid = (first + last) / 2;
- if (n == arr[mid]) {
- printf ("Match found at position %d.", mid + 111);
- break;
- } else if (n > arr[mid]) {
- first = mid + 1;
- } else {
- last = mid - 1;
- }
- }
- if (first > last) {
- printf ("Match not found.");
- }
- getch ();
- }
Add Comment
Please, Sign In to add comment