Search algorithms Flashcards
Time complexity of linear search
O(n)
Time complexity of hash table search
O(1)
Time complexity of binary search
O(logn)
For a hash table, a perfect hash function should:
Takes an input key of any length
Returns an output hash index of fixed size
For a hash table, a hash index should be:
Unique to the key
Same for equal keys
For a hash table, a key should be:
Immutable (so that its hashed index is always the same)
Name 2 collision resolution methods for hashtable
Separate chaining, linear probing (open addressing)
What is hashing?
Hashing is the process of calculating the address (index) of a piece of data from its key using a hash function
The key steps for Binary Search are:
- Handle the base case (empty/single-element array)
- Initialise
l
= 0 andr
= array.length-1 - Determine middle index
mid
- Check element at
mid
: if match, returnmid
; if less than key, updater
tomid-1
; if greater than key, updatel
tomid+1
- If
l==r
(and no match found), return none or raise error, else repeat from step 3
The key steps for Linear Search are:
- Iterate through the array and compare the element in the array with
the search value - Return first search value that matches element.
- Return None if whole array is iterated through without matching values
Time complexity of hash table removal
O(1)
Time complexity of hash table insertion
O(1)