You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/num_methods/binary_search.md
+6-6Lines changed: 6 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -142,9 +142,9 @@ This paradigm is widely used in tasks around trees, such as finding lowest commo
142
142
143
143
<small>Note that this section follows the description in [Sports programming in practice](https://kostka.dev/sp/).</small>
144
144
145
-
Imagine that we want to answer $Z$ queries about the index of the largest value less than or equal to some $X_i$ (for $i=1,2,\ldots,Z$) in some sorted 0-indexed array $A$. Naturally, each query can be answered using binary search.
145
+
Imagine that we want to answer $Z$ queries about the index of the largest value less than or equal to some $X_i$ (for $i=1,2,\ldots,Z$) in a sorted 0-indexed array $A$. Naturally, each query can be answered using binary search.
146
146
147
-
Specifally, let us consider the following array $A = [1,3,5,7,9,9,13,15]$
147
+
Specifically, let us consider the following array $A = [1,3,5,7,9,9,13,15]$
148
148
with queries: $X = [8,11,4,5]$. We can use binary search for each query sequentially.
@@ -161,11 +161,11 @@ with queries: $X = [8,11,4,5]$. We can use binary search for each query sequenti
161
161
|**step 4**| answer in \([3,4)\)| answer in \([5,6)\)| answer in \([1,2)\)| answer in \([2,3)\)|
162
162
||\( index = 3 \)|\( index = 5 \)|\( index = 1 \)|\( index = 2 \)|
163
163
164
-
We generally process this table by columns (queries), but notice that in each row we often repeat access to certain values of our array. To limit access to the values, we can process the table by rows (steps). This does not make huge difference in our small example problem (as we can access all elements in $\mathcal{O}(1)$), but in more complex problems, where computing these values is more complicated, this might be essential to solve these problems efficiently. Moreover, note that we can arbitrarily choose the order in which we answer questions in a single row. Let us look at the code implementing this approach.
164
+
We generally process this table by columns (queries), but notice that in each row we often repeat access to certain values of the array. To limit access to these values, we can process the table by rows (steps). This does not make huge difference in our small example problem (as we can access all elements in $\mathcal{O}(1)$), but in more complex problems, where computing these values is more complicated, this might be essential to solve these problems efficiently. Moreover, note that we can arbitrarily choose the order in which we answer questions in a single row. Let us look at the code implementing this approach.
165
165
166
166
```{.cpp file=parallel-binary-search}
167
-
// Computes the index of the largest value in table A less than or equal to X_i for all i.
168
-
vector<int> ParallelBinarySearch(vector<int>& A, vector<int>& X) {
167
+
// Computes the index of the largest value in a sorted array A less than or equal to X_i for all i.
168
+
vector<int> parallel_binary_search(vector<int>& A, vector<int>& X) {
169
169
int N = A.size();
170
170
int M = X.size();
171
171
vector<int> left(M, -1);
@@ -192,7 +192,7 @@ vector<int> ParallelBinarySearch(vector<int>& A, vector<int>& X) {
0 commit comments