-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path7_search_element.cpp
91 lines (84 loc) · 2.37 KB
/
7_search_element.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
/**
* Given an array and target value k, check if k exists in the array.
*
* * Solutions
*
* (1) linear search: O(n) time, O(1) space
* -> works whether the array is sorted or not
*
* (2) binary search: O(log n) time, O(1) space
* -> works ONLY IF array is SORTED
* -> RECURSIVE VERSION & ITERATIVE VERSION
*
* ** What I learned
*
* ** make sure to check array is SORTED before using BINARY SEARCH!
*
* * memorize structure for binary search (both iterative & recursive)
* - use range (left <= right); EQUAL sign included for both versions
* -> if target value does not exist, the range will go out of bounds
* - put return false at end of code
*
* => if target value does not exist, left and right pointers will move out of bounds
* (left > right) so automatically stop loop or recursion
*
*/
#include <bits/stdc++.h>
using namespace std;
/**
* (1) linear search
*/
bool search(vector<int> v, int k) {
// iterate over each element
for (auto x : v) {
// check if k exists in array
if (x == k) {
return true;
}
}
// if k doesn't exist in array,
return false;
}
/** (2-a) binary search: ITERATIVE version
* (based on assumption that array is sorted)
*/
bool search(vector<int> v, int k) {
int left = 0;
int right = v.size() - 1;
while (left <= right) {
// check the mid value
int mid = (left + right) / 2;
if (v[mid] == k) {
return true;
}
// cut the range in half and continue searching
if (k < v[mid]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
// if k doesn't exist in array,
return false;
}
/** (2-b) binary search: RECURSIVE version
* (based on assumption that array is sorted)
*/
bool search(vector<int> v, int left, int right, int k) {
// check range is right
if (left <= right) {
// compare mid value w/ target k
int mid = (left + right) / 2;
if (v[mid] == k) {
return true;
}
// cut the range in half and recursively call function
if (k < v[mid]) {
return search(v, left, mid - 1, k);
} else {
return search(v, mid + 1, right, k);
}
}
// if out of range,
return false;
}