diff --git a/binary_search.cpp b/binary_search.cpp new file mode 100644 index 00000000..7a9b65c1 --- /dev/null +++ b/binary_search.cpp @@ -0,0 +1,49 @@ +#include +using namespace std; + +void bsearch(int a[],int n,int key) +{ + sort(a,a+n); + int start,end,mid; + + start = 0; + end = n-1; + bool found = false; + while(start<=end) + { + mid = (start+end)/2; + if(a[mid]==key) + { + found = true; + break; + } + else + if(a[mid]>key) + end = mid-1; + else + start = mid+1; + } + if(found) + cout<>n; + int arr[n]; + + cout<<"Enter "<>arr[i]; + + cout<<"Enter the element to be searched:- "; + cin>>k; + + bsearch(arr,n,k); + return 0; +} + diff --git a/linear_search.cpp b/linear_search.cpp new file mode 100644 index 00000000..01f81eb8 --- /dev/null +++ b/linear_search.cpp @@ -0,0 +1,39 @@ +//HEADER FILES +#include +using namespace std; + +void linearsearch(int arr[], int N,int K) +{ + int i; + bool found = false; + for(i=0;i>n; + int a[n]; + + cout<<"Enter "<>a[i]; + + cout<<"Enter the element to be searched:- "; + cin>>key; + + linearsearch(a,n,key); + return 0; +}