Skip to content

Commit dd79a92

Browse files
authored
Update binary_search.md
1 parent 7542c7e commit dd79a92

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

strings_arrays/binary_search.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Binary search is a method for locating an element in a sorted list efficiently.
22

33
Algorithm
44
------------------
5-
In binary search, you are provided a list of sorted numbers and a key. The desired output is the index of the key, if it exists.
5+
In binary search, you are provided a list of sorted numbers and a key. The desired output is the index of the key, if it exists and None if it doesn't.
66

77
Binary search is a recursive algorithm. The high level approach is that we examine the middle element of the list. The value of the middle element determines whether to terminate the algorithm (found the key), recursively search the left half of the list, or recursively search the right half of the list.
88
```
@@ -58,4 +58,12 @@ def binary_search(nums, key):
5858

5959
## Runtime and Space Complexity
6060

61-
Binary search completes in **O(log N)** time because each iteration decreases the size of the list by a factor of 2. Its space complexity is constant because we only need to maintain two pointers to locations in the list. Even the recursive solution has constant space with tail call optimization.
61+
Binary search completes in **O(log N)** time because each iteration decreases the size of the list by a factor of 2. Its space complexity is constant because we only need to maintain two pointers to locations in the list. Even the recursive solution has constant space with [tail call optimization](https://en.wikipedia.org/wiki/Tail_call).
62+
63+
## Example problems
64+
* [Search insert position](https://leetcode.com/problems/search-insert-position/description/)
65+
* [Search in a 2D matrix](https://leetcode.com/problems/search-a-2d-matrix/description/)
66+
67+
## Video walkthrough
68+
* [HackerRank binary search video](https://www.youtube.com/watch?v=P3YID7liBug)
69+
* [Search a 2D matrix](https://www.youtube.com/playlist?list=PL7zKQzeqjecINi-_8CmiFLMLCCxjIHBPj)

0 commit comments

Comments
 (0)