0% found this document useful (0 votes)
17 views

13.a) Write A Recursive C Program For Searching An Element On A Given List Using Binary Search Method

This C program implements a recursive binary search function to search for a key element in a sorted array. The program takes user input for the number of elements, values to populate the array in ascending order, and the key element to search for. It calls the recursive binary search function, passing the array, key, and low and high index values. The function recursively searches halves of the array until it finds the key or returns -1 if not found, and the main function prints the search result.

Uploaded by

navin_killer
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

13.a) Write A Recursive C Program For Searching An Element On A Given List Using Binary Search Method

This C program implements a recursive binary search function to search for a key element in a sorted array. The program takes user input for the number of elements, values to populate the array in ascending order, and the key element to search for. It calls the recursive binary search function, passing the array, key, and low and high index values. The function recursively searches halves of the array until it finds the key or returns -1 if not found, and the main function prints the search result.

Uploaded by

navin_killer
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

/* 13.a) Write a recursive C program for Searching an element on a given list using Binary Search Method */ #include<stdio.

h> void main() { int a[10],n,p,i,key,low,high; clrscr(); printf("Enter number of elements\n"); scanf("%d",&n); printf("Enter the elements in ascending order\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Enter key element\n"); scanf("%d",&key); low=0; high=n-1; p=search(a,key,low,high); if(p==-1) printf("Unsucessful search"); else printf("Sucessful search,key element is present at the position %d",p+1); getch(); } int search(int a[],int key,int low,int high) { int mid; while(low<=high) { mid=(low+high)/2; if(key==a[mid]) return(mid); else if(key<a[mid]) return(search(a,key,low,mid-1)); else return(search(a,key,mid+1,high)); } return(-1); } /* ******************************OUTPUT************************* Enter number of elements 5 Enter the elements in ascending order 10 25 35

45 50 Enter key element 45 Sucessful search,key element is present at the position 4 ************************************************************ */

You might also like