C++ implementation of Binary Search without using sort function
kw.cpp
#include<iostream>usingnamespacestd;voidBinarySearch(inta[],intnum,intfirst,intlast){if(first>last){cout<<"\nElement not found!"<<endl;cout<<"———————————————————————————————————————————\n";}else{intmid;mid=(first+last)/2;if(a[mid]==num){cout<<"\nElement found at index "<<mid+1<<endl;cout<<"———————————————————————————————————————————\n";}elseif(a[mid]>num){BinarySearch(a,num,first,mid-1);}else{BinarySearch(a,num,mid+1,last);}}}intmain(){intn,i,j,num,end,beg,temp;cout<<"———————————————————————————————————————————";cout<<"\nImplementation of a Binary Search";cout<<"\n———————————————————————————————————————————";try{cout<<"\nHow many numbers you want to enter ";cin>>n;if(cin.fail())throw"";inta[n];for(i=0;i<n;i++){cout<<"Enter Number "<<i+1<<" | ";cin>>a[i];}for(i=0;i<n;i++){for(j=i+1;j<n;j++){if(a[i]>a[j]){temp=a[i];a[i]=a[j];a[j]=temp;}}}cout<<"\nSorted numbers ";for(i=0;i<n;i++){cout<<a[i]<<" ";}beg=0;end=n-1;cout<<"\nEnter a number to be searched ";cin>>num;BinarySearch(a,num,beg,end);}catch(...){cout<<"\nEnter the number please"<<endl;cout<<"———————————————————————————————————————————\n";}return(0);}
Output
kodingwindow@kw:~$ g++ kw.cpp kodingwindow@kw:~$ ./a.out
———————————————————————————————————————————
Implementation of a Binary Search
———————————————————————————————————————————
How many numbers you want to enter 9
Enter Number 1 | 55
Enter Number 2 | -23
Enter Number 3 | -78
Enter Number 4 | 48
Enter Number 5 | 96
Enter Number 6 | 236
Enter Number 7 | 45
Enter Number 8 | 12
Enter Number 9 | -1024
Sorted numbers -1024 -78 -23 12 45 48 55 96 236
Enter a number to be searched -23
Element found at index 3
———————————————————————————————————————————
kodingwindow@kw:~$
C++ implementation of Binary Search using sort function
kw.cpp
#include<iostream>
#include<algorithm>usingnamespacestd;voidBinarySearch(inta[],intnum,intfirst,intlast){if(first>last){cout<<"\nElement not found!"<<endl;cout<<"———————————————————————————————————————————\n";}else{intmid;mid=(first+last)/2;if(a[mid]==num){cout<<"\nElement found at index "<<mid+1<<endl;cout<<"———————————————————————————————————————————\n";}elseif(a[mid]>num){BinarySearch(a,num,first,mid-1);}else{BinarySearch(a,num,mid+1,last);}}}intmain(){intn,i,j,num,end,beg,temp;cout<<"———————————————————————————————————————————";cout<<"\nImplementation of a Binary Search";cout<<"\n———————————————————————————————————————————";cout<<"\nHow many numbers you want to enter ";cin>>n;inta[n];for(i=0;i<n;i++){cout<<"Enter Number "<<i+1<<" | ";cin>>a[i];}sort(a,a+n);cout<<"\nSorted numbers ";for(i=0;i<n;i++){cout<<a[i]<<" ";}beg=0;end=n-1;cout<<"\nEnter a number to be searched ";cin>>num;BinarySearch(a,num,beg,end);return(0);}
Output
kodingwindow@kw:~$ g++ kw.cpp kodingwindow@kw:~$ ./a.out
———————————————————————————————————————————
Implementation of a Binary Search
———————————————————————————————————————————
How many numbers you want to enter 9
Enter Number 1 | 55
Enter Number 2 | -23
Enter Number 3 | -78
Enter Number 4 | 48
Enter Number 5 | 96
Enter Number 6 | 236
Enter Number 7 | 45
Enter Number 8 | 12
Enter Number 9 | -1024
Sorted numbers -1024 -78 -23 12 45 48 55 96 236
Enter a number to be searched -23
Element found at index 3
———————————————————————————————————————————
kodingwindow@kw:~$
Dear User, Thank you for visitng Kodingwindow. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.
Dear User, We are grateful for your interest in the Kodingwindow Android app. We intend to soon make it available on the Google Play Store. Currently, Android versions 10 and up require manual apk installations. Would you like to get the app?