summaryrefslogtreecommitdiff
path: root/notes.md
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2021-10-03 16:31:56 -0600
committermo khan <mo@mokhan.ca>2021-10-03 16:31:56 -0600
commitcff508b47a1cb2eee2da5f748dadeb406861ff28 (patch)
tree89ec432dc9c855c1bc099df7485c999c28331e0a /notes.md
parentb55090ddf6ca27a853c8687def4a8bbdf9115ad8 (diff)
pseudocode for binary search
Diffstat (limited to 'notes.md')
-rw-r--r--notes.md19
1 files changed, 19 insertions, 0 deletions
diff --git a/notes.md b/notes.md
index eb58099..38a2d88 100644
--- a/notes.md
+++ b/notes.md
@@ -807,3 +807,22 @@ time of this recursive version of insertion sort.
remaining portion of the sequence each time. Write pseudocode, either
iterative or recursive, for binary search. Argue that the worst case running
time of binary search is O(lg(n)).
+
+`BINARY-SEARCH(A, t, s, e)` where `A` is an array and `s`, and `r` are indices into the array such that `s < e` and `t` is the target value to find.
+
+```plaintext
+BINARY-SEARCH(A, t, s, e)
+ length = e - s
+ if length == 1
+ item = A[s]
+ else
+ mid = (length / 2) + s
+ item = A[mid]
+
+ if item == t
+ return mid
+ if item < t
+ return BINARY-SEARCH(A, t, s, mid-1)
+ else
+ return BINARY-SEARCH(A, t, mid+1, e)
+```