Skip to main content

Binary Search use Recursion in C or C Program for Binary Search using Array Passing UDF

Binary Search using Recursion function in C or C Program for Binary Search using Array Passing User Defined Function :-

Example : How to write C Program for Binary Search using Recursive Function and its solutions given below :-

#include<stdio.h>
#include<conio.h>

int binarySearch(int lmt[],int arrayStart,int arrayEnd,int a) /* UDF declar with definition */
{
int m,pos;
if (arrayStart<=arrayEnd)
{
m=(arrayStart+arrayEnd)/2;
if (lmt[m]==a)
return m;
else if ( a < lmt[m] )
return binarySearch(lmt,arrayStart,m-1,a); /* function calls itself called Recursive function */
else
return binarySearch(lmt,m+1,arrayEnd,a); /* function calls itself called Recursive function */
}
return -1;
}

void readArray(int lmt[],int n) /* UDF declar with definition */
{
int i;
printf("Enter the array elements :\n");
for( i=0 ; i < n ; i++ )
scanf("%d",&lmt[i]);
}

/* main() function starts here */
void main()
{

int lmt[10], num, searchElement,pos;

clrscr();


printf("Enter the number of array elements to be printed :");
scanf("%d",&num);

readArray(lmt,num);

printf("\nEnter the element you want to search :");
scanf("%d",&searchElement);

pos=binarySearch(lmt,0,num,searchElement);
if(pos==-1)
{
printf("\nElement not found");
}
else
{
printf("\nElement found at %d position",++pos);
}

getch();
}
/*
if we give no of elements is 5 and array elements are 11,22,33,66,45 then element to be searched is 66 then answer will be "Element found at 4 position" otherwise "Element not found"
*/

Popular posts from this blog

C Code Shortening Techniques or My Logic for Reduced Source Code Writing in C Program Language

C Code Shortening Techniques or My Logic for Reduced Source Code Writing Tips and Tricks in C Program Language :- Role of Semi Colon(;) and Curly Braces({ }) in C Program Perhaps, you might have heard that the for() loop should not end with ";" like if(). But, in fact the ";" is using in C language as a Statement Terminator, so, if we want to terminate for() loop, then we should use ";" at the end of for() loop. [ Header files are to be included as usual, it is not written in the examples ] Question : write a C program to print 1 to 10 numbers using for() loop ? Example 1 : void main() { int i; for ( i = 1 ; i <= 10 ; i++ ) { printf("%d\n",i); } } /* above program, for() loop has one statement outside, so we do not use ";" at the end, because if we put ";" the for() will not take the continuing statement as its own. */ Example 2 : void main() { int i; for ( i = 1 ; i <= 10 ; printf( "%d \n" , i ...

How to create User Defined Functions in C, see example Perfect_Number() function used as UDF in C Program

How to create User Defined Functions in C, see example Perfect_Number() function used as UDF in C Program :- Example : This is a program to find Perfect Numbers upto a limit using User Defined Function(UDF) in C Program Language :- #include<stdio.h> #include<conio.h> int Perfect_Number(int); /* UDF function prototype declaration : int argument and int returns */ main() { int lmt,num; clrscr(); printf("Enter a Limit :"); scanf("%d",&lmt); for(num=5 ; num<=lmt ; num++) { /* function calls here with Actual Parameter num and returns either 1 or 0 and checks */ if(Perfect_Number(num)==1) { printf("\n%d",num); } } getch(); } /* Either the function should be defined out side the main() or it should be in one of the header files included. function definition with a Formal Parameter as n */ int Perfect_Number(int n) { int a,sm; sm=0; for(a=1 ; a<n ; a++) { if(n%a==0) { sm=sm+a; } } if(sm==n) return 1; else ...

Hello World C Program with full explanation.

Hello World C Program with Explanation are given below :- #include<stdio.h> #include <conio.h> void main() { clrscr(); printf("Hello World!"); getch(); } Explanations: - printf() function is defined in the header file stdio.h, so it must be included at top of the program. Like clrscr() and getch() functions are defined in conio.h. To include a Header file, the syntax is #include at the top of the program. C Program to be written in main() function. Then void main() means the main() function returns nothing and it will study later. We use ";" as a statement terminator so clrscr(),printf() and getch() are used ";" but "void main()" doesn't have ";" because it is not terminated there, it follows "{" and "}" . clrscr() function uses for to clear the screen. printf() for print something. getch() uses for to get a character from keyboard and it is used here as a wait command. Save file name as " an...