Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions Searching problems/Binary_Search_Using_Recursion.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include<stdlib.h>
#include<stdio.h>

int BinarySearch(int A[],int ele,int first,int last){

int mid=(first+last)/2;

if(A[mid]==ele) {
return mid;
}

if(A[mid]!=ele){

if(A[mid]>ele && first<=last){
return BinarySearch(A,ele,first,mid-1);
} else if(A[mid]<ele && first<=last){
return BinarySearch(A,ele,mid+1,last);
}

}

printf("Searched Element Not Found");

return -1;
}
int main(){

int i ,size,ele,loc,A[20];

printf("Enter the size:");
scanf("%d",&size);

printf("\nEnter the Array:");
for(i=0;i<size;i++){
scanf("%d",&A[i]);
}

printf("\nEnter the no. to be searched :");
scanf("%d",&ele);

loc=BinarySearch(A,ele,0,size-1);

if(loc!=-1){
printf("\nThe searched element %d is present at location:%d",ele,loc+1);
}

return 0;
}