#include
#define TRUE 0
#define FALSE 1
int main(void) {
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int left = 0;
int right = 10;
int middle = 0;
int number = 0;
int bsearch = FALSE;
int i = 0;
printf("ARRAY: ");
for(i = 1; i <= 10; i++)
printf("[%d] ", i);
printf("\nSearch for Number: ");
scanf("%d", &number);
while(bsearch == FALSE && left <= right) {
middle = (left + right) / 2;
if(number == array[middle]) {
bsearch = TRUE;
printf("** Number Found **\n");
} else {
if(number < array[middle]) right = middle - 1;
if(number > array[middle]) left = middle + 1;
}
}
if(bsearch == FALSE)
printf("-- Number Not found --\n");
return 0;
}
Binary search : Sample program
Subscribe to:
Post Comments (Atom)
Labels
- Code Request (1)
- Date / time functions (2)
- Elementary Tutorial (2)
- Journals and Publications (1)
- Searching and Sorting (4)
- Series and Numbers (2)
- String functions (4)
Submit Request
If you need some code or programs just post a comment on this post. I will try to provide you the same at the earliest.
About this blog
Free sample code, example code , code snippet , tutorials in C C++. Find, download and reuse the code database available which vary from small programs to large ones as well. Feel free to request for code that is not in the list.
Me & Site Sponsors
About Me | Reach Me |
Sharad Kumar Singh | Feedback |
0 comments:
Post a Comment