-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
92 lines (85 loc) · 1.75 KB
/
main.cpp
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include<fstream>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main()
{
int n,i,a[10000],search;
clock_t t1,t2;
float dtime;
cout<<"enter the number of elements\n";
cin>>n;
cout<<"enter the element\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
ofstream outfile("rishabh.txt");
for(i=0;i<n;i++)
{
outfile<<a[i]<<" ";
}
outfile.close();
ifstream infile("rishabh.txt");
for(i=0;i<n;i++)
{
infile>>a[i];
}
infile.close();
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
for(i=0;i<n-1;i++)
{
for(int j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"sorted elements are \n";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
int first=0;
int last=n-1;
int middle=(first+last)/2;
cout<<"\nmiddle element is "<<a[middle]<<"\n";
t1=clock();
cout<<"enter the element to search\n";
cin>>search;
while(first<=last)
{
if(a[middle]==search)
{
cout<<"element "<<a[middle]<<" found at location "<<(middle+1);
break;
}
else if(search>a[middle])
{
first=middle+1;
}
else
{
last=middle-1;
}
middle=(first+last)/2;
}
if(first>last)
{
cout<<"\nelement not found\n";
}
t2=clock();
cout<<"time t1 and t2 are \n"<<float(t1)/CLOCKS_PER_SEC<<" and "<<float(t2)/CLOCKS_PER_SEC;
dtime=float(t2)-float(t1);
cout<<"\nIt tooks me seconds "<<(dtime)/CLOCKS_PER_SEC;
return 0;
}