-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearch.cpp
More file actions
96 lines (90 loc) · 2.02 KB
/
binarySearch.cpp
File metadata and controls
96 lines (90 loc) · 2.02 KB
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
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct Array
{
int A[10];
int size;
int length;
};
void append(struct Array *arr, int x)
{ //->O(1)
if (arr->length < arr->size)
{
arr->A[arr->length++] = x;
}
}
void insert(struct Array *arr, int index, int x)
{ //->O(n)
// checking if the index is valid or not
if (index >= 0 && index <= arr->length)
{
for (int i = arr->length; i > index; i--)
{
arr->A[i] = arr->A[i - 1];
}
arr->A[index] = x;
arr->length++;
}
}
int Delete(struct Array *arr, int index)
{
int x = 0;
if (index >= 0 && index < arr->length)
{
x = arr->A[index];
for (int i = index; i < arr->length - 1; i++)
{
arr->A[i] = arr->A[i + 1];
}
arr->length--;
return x;
}
return 0;
}
void display(struct Array arr)
{ //->O(1)
for (int i = 0; i < arr.length; i++)
{
cout << arr.A[i] << " ";
}
cout << endl;
}
int binarySearchI(int s, int e, int key, struct Array arr){
while(s<=e){
int mid= (s+e)/2;
if(arr.A[mid]==key) return mid;
else if(arr.A[mid]<key){
s= mid+1;
}
else{
e=mid-1;
}
}
return -1;
}
int binarySearchR(int s, int e, int key, struct Array arr){
if(s<=e){
int mid= (s+e)/2;
if(arr.A[mid]==key) return mid;
else if(arr.A[mid]<key){
return binarySearchR(mid+1,e,key,arr);
}
else{
return binarySearchR(s,mid-1,key,arr);
}
}
return -1;
}
int main()
{
struct Array a = {{2, 4, 6, 8, 10}, 10, 5};
display(a);
int s=0, e=a.length-1;
cout<<binarySearchI(s,e,8,a)<<endl;
cout<<binarySearchI(s,e,2,a)<<endl;
cout<<binarySearchR(s,e,8,a)<<endl;
cout<<binarySearchR(s,e,2,a)<<endl;
cout<<binarySearchR(s,e,9,a)<<endl;
return 0;
}