#include <stdio.h>
#define SIZE 49
int searchPage(int pages[], int left, int rigth, int page)
{
if (rigth >= left) {
int middle = left + (rigth - left) / 2;
// If the page is present at the middle itself
if (pages[middle] == page)
return middle;
// If the current page number is greater than page, then page is on the left side of the current page
if (pages[middle] > page)
return searchPage(pages, left, middle - 1, page);
// otherwise page is on the right side of the current page.
return searchPage(pages, middle + 1, rigth, page);
}
return -1;
}
int main()
{
int pages[SIZE];
int i,page;
for(i=0;i<SIZE;i++){
pages[i]=i+1;
}
printf("Enter page to search: ");
scanf("%d",&page);
i=searchPage(pages,0,SIZE,page);
if(i!=-1){
printf("\nPage is present in the book.\n\n");
}else{
printf("\nPage is NOT present in the book.\n\n");
}
scanf("%d",&i);
return 0;
}
Comments
Leave a comment