forked from Vishal1003/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarysearch.cpp
More file actions
39 lines (30 loc) · 776 Bytes
/
binarysearch.cpp
File metadata and controls
39 lines (30 loc) · 776 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <algorithm>
using namespace std;
//function to display array list
void dispArray(int arr[], int size)
{
for(int i = 0; i < size; i++)
cout <<" "<< arr[i];
cout<<endl;
}
//main code for binary search
int main()
{
int a[]= {10, 1, 20, 2, 30, 4};
//get array length
int arr_length = sizeof(a) / sizeof(int);
//print array
cout<<"Array elements are: ";
dispArray(a, arr_length);
//sort the array
sort(a, a + arr_length);
cout<<"Sorted array elements: ";
dispArray(a, arr_length);
//searching 30 in the array
if(binary_search(a, a+ arr_length, 30))
cout<<"Element found"<<endl;
else
cout<<"Element is not found"<<endl;
return 0;
}