Skip to content

Commit 02825e3

Browse files
committed
Binary search code and doc added
1 parent 7c875d4 commit 02825e3

File tree

6 files changed

+54
-1
lines changed

6 files changed

+54
-1
lines changed

Searching/Binary Search/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Binary Search
2+
3+
**Binary search** is an efficient algorithm for finding an item from an ordered list of items. It works by repeatedly dividing in half the portion of the list that could contain the item, until you've narrowed down the possible locations to just one.
4+
5+
![Binary Search](binary-search.png)
6+
7+
For a binary search to work, it is mandatory for the target array to be sorted.
8+
9+
In the sequential search, when we compare against the first item, there are at most *(n−1)* more items to look through if the first item is not what we are looking for. Instead of searching the list in sequence, a binary search will start by examining the middle item. If that item is the one we are searching for, we are done. If it is not the correct item, we can use the ordered nature of the list to eliminate half of the remaining items. If the item we are searching for is greater than the middle item, we know that the entire lower half of the list as well as the middle item can be eliminated from further consideration. The item, if it is in the list, must be in the upper half.
10+
11+
We can then repeat the process with the upper half. Start at the middle item and compare it against what we are looking for. Again, we either find it or split the list in half, therefore eliminating another large part of our possible search space.
12+
13+
14+
#### Linear Search vs. Binary Search
15+
A linear search looks down a list, one item at a time, without jumping. In complexity terms this is an O(n) search - the time taken to search the list gets bigger at the same rate as the list does.
16+
17+
A binary search is when you start with the middle of a sorted list, and see whether that's greater than or less than the value you're looking for, which determines whether the value is in the first or second half of the list. Jump to the half way through the sub-list, and compare again etc. In complexity terms this is an O(log n) search - the number of search operations grows more slowly than the list does, because you're halving the "search space" with each operation.
18+
19+
![Linear Vs. Binary Search](binary-and-linear-search-animations.gif)
20+
21+
#### Complexity Analysis
22+
- Worst Case - O(logn)
23+
- Best Case - O(1)
24+
- Average Case - O(logn)
25+
26+
27+
### More on this topic
28+
- https://en.wikipedia.org/wiki/Binary_search_algorithm
29+
- https://www.khanacademy.org/computing/computer-science/algorithms/binary-search/a/binary-search
30+
- https://www.topcoder.com/community/data-science/data-science-tutorials/binary-search/
31+
- https://www.tutorialspoint.com/data_structures_algorithms/binary_search_algorithm.htm
Loading
41 KB
Loading

Searching/Binary Search/search.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def binarySearch(item, item_list):
2+
low, high = 0, len(item_list) - 1
3+
4+
found = False
5+
6+
while low <= high and not found:
7+
mid = (low + high) // 2
8+
9+
if item_list[mid] == item:
10+
found = True # You can catch the found value or it's position here too
11+
else:
12+
if item < item_list[mid]:
13+
high = mid - 1
14+
else:
15+
low = mid + 1
16+
17+
return found

Searching/Binary Search/test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from search import binarySearch
2+
3+
# This list must be sorted. If it is not given as sorted, sort it first, then call the binarySearch method
4+
testlist = [0, 1, 2, 8, 13, 17, 19, 32, 42]
5+
print(binarySearch(3, testlist))
6+
print(binarySearch(13, testlist))

Searching/Linear Search/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ but sometimes you might need to do some customization on the searching algorithm
2121
- Worst Case - O(n)
2222
- Best Case - O(1)
2323
- Average Case - O(n)
24-
- Space Complexity - O(n)
2524

2625

2726
### More on this topic

0 commit comments

Comments
 (0)