From 24c677e809f77e9798f1a07da347e620b78b7a1b Mon Sep 17 00:00:00 2001 From: mo khan Date: Thu, 15 Dec 2022 14:38:32 -0700 Subject: start work on chapter 3 exercise --- README.md | 2 ++ assignments/1.md | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) 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: + -- cgit v1.2.3