Week 13-14
Week 13-14
Binary Search
Binary Search is a searching algorithm for
finding an element's position in a sorted
array.
In this approach, the element is always
searched in the middle of a portion of an
array.
Binary search can be implemented only on a
sorted list of items. If the elements are not
sorted already, we need to sort them first.
Binary Search
Cont..
3. Find the middle element mid of the array ie. arr[(low
+ high)/2] = 6.
Cont.
Binary Search
8. x = 4 is found.
Binary Search Algorithm
int main()
{
char str[100];
return 0;
}
Output
Enter a string: C++
You entered: C++
#include <iostream>
using namespace std;
int main()
{
char str[100];
cout << "Enter a string: ";
cin.get(str, 100);
#include <iostream>
using namespace std;
int main()
{
// Declaring a string object
string str;
cout << "Enter a string: ";
getline(cin, str);