Search Algorithms Flashcards
1
Q
Time complexity of linear search
A
O(n)
2
Q
Time complexity of binary search
A
O(log n)
3
Q
Time complexity of hash table search
A
Without collision resolution: O(1)
With collision resolution: O(n)
4
Q
For a hash table, a hash function should:
A
- Take an input key of any length
- Return a location of fixed length
- Location should be unique to the key
Returns an output hash index of fixed size
5
Q
For a hash table, a key should be:
A
Immutable (so that its hashed location is always the same)
6
Q
Features of an ideal hash function
A
- Use all input data
- Small difference in input results in large difference in hash value
- Output should be uniformly distributed across all possible hash values
7
Q
The key steps for Binary Search are:
A
- Handle the base case (empty/single-element array)
- Initialise
start = 0
andend = array.length - 1
- Determine middle index
mid
- Check item at
mid
:- if match, return
mid
- if search key less than mid item, update
end
tomid - 1
- if search key greater than mid item, update
start
tomid + 1
- if match, return
- If
start > end
(no match found), return none or display/raise error, else repeat from step 3