Linear search
Binary search
Linear search
Binary search
Divide and conquer algorithm
Data needs to be sorted
1 | 2 | 3 | 4 | 6 | 7 | 9 |
---|
1 | 2 | 3 | 4 | 6 | 7 | 9 |
---|
1 | 2 | 3 | 4 | 6 | 7 | 9 |
---|
1 | 2 | 3 | 4 | 6 | 7 | 9 |
---|
MID
MID
Find: 6
Iterative
function binarySearch(data, target) {
var low = 0,
high = data.length - 1,
mid;
while (low <= high) {
mid = Math.floor((low + high) / 2);
if (data[mid] < target) {
low = mid + 1;
} else if (data[mid] > target) {
high = mid - 1;
} else {
return mid;
}
}
return -1;
}
(JavaScript)
Recursive
function binarySearchRecursive(data, target, low, high) {
if (low > high) return -1;
var mid = Math.floor((low + high) / 2);
if (data[mid] < target) {
return binarySearchRecursive(data, target, mid + 1, high);
} else if (data[mid] > target) {
return binarySearchRecursive(data, target, low, mid - 1);
} else {
return mid;
}
}
(JavaScript)
Instead the need of sorting every time we add an element to the collection
Properties
Warning: Don't assume that a binary tree is a Binary Search Tree
4 |
---|
3 |
---|
2 |
---|
1 |
---|
6 |
---|
7 |
---|
9 |
---|
Operations Complexity
Access | Search | Insertion | Deletion | Access | Search | Insertion | Deletion |
---|---|---|---|---|---|---|---|
O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(n) | O(n) | O(n) | O(n) |
4 |
---|
3 |
---|
2 |
---|
1 |
---|
6 |
---|
7 |
---|
9 |
---|
Worst case
Binary tree from a sorted collection
Average
Given a sorted array of n integers that has been rotated an unknown number of times, give an O(log n) algorithm that finds an element in the array. You may assume that the array was originally sorted in increasing order.
EXAMPLE:
**Input**: find 5 in array (15 16 19 20 25 1 3 4 5 7 10 14)
**Output**: 8 (the index of 5 in the array)
Given a sorted array of strings which is interspersed with empty strings, write a method to find the location of a given string.
find “ball” in [“at”, “”, “”, “”, “ball”, “”, “”, “car”, “”, “”, “dad”, “”, “”] will return 4
find “ballcar” in [“at”, “”, “”, “”, “”, “ball”, “car”, “”, “”, “dad”, “”, “”] will return -1
Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal height.
References
Grokking algorithms
Algorithms-in-a-nutshell-in-a-nutshell
Cracking the coding interview
http://bigocheatsheet.com/
CS50 youtube channel