-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExperiment 3.cpp
More file actions
66 lines (66 loc) · 1013 Bytes
/
Copy pathExperiment 3.cpp
File metadata and controls
66 lines (66 loc) · 1013 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<iostream>
using namespace std;
int main()
{
int a[20],n,i,j,ele,flag=0,temp;
int mid,min,max;
cout<<"Enter the size of array: ";
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Array is:\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"\nAfter sorting the array: \n";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
cout<<"\nEnter the element to search: ";
cin>>ele;
min=0;
max=n-1;
mid=(min+max)/2;
if(min<max)
{
for(i=0;i<n;i++)
{
if(a[mid]==ele)
{
cout<<"Position of "<<ele<<" is: "<<mid+1;
flag=1;
break;
}
else if(a[mid]>ele)
{
max=mid-1;
}
else if(a[mid]<ele)
{
min=mid+1;
}
mid=(min+max)/2;
}
}
if(flag==0)
cout<<"Element not found ";
return 0;
}