summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2022-12-15 14:38:32 -0700
committermo khan <mo@mokhan.ca>2022-12-15 14:38:32 -0700
commit24c677e809f77e9798f1a07da347e620b78b7a1b (patch)
tree9047aa06e683e39b48516915500f2e1084eedb78
parent742e5854f2bebc64e427fc53bc4c95b5df8b7207 (diff)
start work on chapter 3 exercise
-rw-r--r--README.md2
-rw-r--r--assignments/1.md18
2 files changed, 20 insertions, 0 deletions
diff --git a/README.md b/README.md
index 866d9a4..986383b 100644
--- a/README.md
+++ b/README.md
@@ -107,3 +107,5 @@ The formal term for "doable" is `effectively computable`.
* top-down design: Viewing an operation at a high level of abstraction and
fleshing out the details of its implementation at a later time constitute an
imporant computer science problem-solving strategy.
+
+# Chapter 3: The Efficiency of Algorithms
diff --git a/assignments/1.md b/assignments/1.md
index 5ecb711..cf0d98e 100644
--- a/assignments/1.md
+++ b/assignments/1.md
@@ -190,3 +190,21 @@
list of numbers shown in Exercise 25, the output of your algorithm would be
something like `Yes, the numbers 7 and 7 are adjacent to each other in the
list`.
+
+## Chapter 3: Exercises
+
+21. Use the binary search algorithm to decide whether 35 is in the following
+ list: `3, 6, 7, 9, 12, 14, 18, 21, 22, 31, 43`
+
+ ```ruby
+ def search(target, items)
+ return false if items.empty?
+
+ mid = items.size / 2
+ return true if items[mid] == target
+ return items[mid] > target ? items[0..mid] : items[mid+1..-1]
+ end
+ ```
+
+ The comparisons are:
+