-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgorithm Assignment.cpp
144 lines (127 loc) · 2.4 KB
/
Algorithm Assignment.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>
#include <conio.h>
using namespace std;
template <class t>
class Search{
private:
public:
int BinarySearch(t*,int,int,t);
int BinaryQuizOne(t*,int,int,t);
int binaryQuizTwo(t*,int,int,t);
int binaryQuizThree(t*,int,int,t);
};
template <class t>
int Search<t>::BinaryQuizOne(t arr[],int l,int r,t key)
{
if(l<r)
{
int mid=(r+l)/2;
if(arr[mid]==key)
{
cout<<"This element is exist in this position : "<<mid<<endl;
}
if(arr[mid]>key)
{
return BinaryQuizOne(arr,l,mid,key);
}
else{
return BinaryQuizOne(arr,mid,r,key);
}
}
}
template <class t>
int Search<t>::BinarySearch(t arr[],int l,int r,t key)
{
if(l<r)
{
int mid=(r+l)/2;
if(arr[mid]==key)
{
cout<<"This element is exist in this position : "<<mid<<endl;
return mid;
}
if(arr[mid]>key)
{
return BinarySearch(arr,l,mid-1,key);
}
else{
return BinarySearch(arr,mid+1,r,key);
}
}
}
template <class t>
int Search<t>::binaryQuizTwo(t arr[],int l,int r,t key)
{
while(l<r)
{
int mid=(r+l)/2;
if(arr[mid]<arr[mid-1])
{
if(arr[mid]>key)
{
BinarySearch(arr,mid,r,key);
}
else
{
BinarySearch(arr,mid,r,key);
}
return 0;
}
if(arr[mid]<arr[l])
{
r=mid+1;
}
else{
l=mid-1;
}
}
}
template <class t>
int Search<t>::binaryQuizThree(t arr[],int l,int r,t key)
{
if(l<=r)
{
int mid=(r+l)/2;
if(arr[mid]==mid)
{
cout<<"This element is exist in this position : "<<mid<<endl;
return mid;
}
if(arr[mid]>mid)
{
return binaryQuizThree(arr,l,mid-1,key);
}
else{
return binaryQuizThree(arr,mid+1,r,key);
}
}
}
int main()
{
int choose;
int arr[]={-2,-1,2,3,7,52,62,72,82};
int x=3;
Search<int> obj;
int size=(sizeof(arr)/sizeof(arr[0]));
do{
cout<<"Enter the choose \n";
cout<<"1 for Question one without (mid-1 & mid+1) \n";
cout<<"2 for Question two rotated array \n";
cout<<"3 for Question three find arr[k]=k \n ";
cin>>choose;
switch(choose)
{
case 1 :obj.BinaryQuizOne(arr,0,size,x);
break;
case 2 : obj.binaryQuizTwo(arr,0,size,x);
break;
case 3 : obj.binaryQuizThree(arr,0,size,arr[0]);
break;
case 0 :continue;
break;
default:cout<<"wrong choose \n";
}
}while(choose!=0);
getch();
return 0;
}