What is a search algorithm?
A search algorithm is a method used to find the position of a specific value within a data structure such as an array or list.
What is binary search?
Binary search is a search algorithm that repeatedly divides a sorted list in half to locate a target value.
What condition must be true before binary search can be used?
The data must be sorted.
What are the steps of binary search?
Find the middle element; compare with the target; if equal stop; if smaller search left half; if larger search right half; repeat until found or list empty.
Why is binary search efficient?
Each comparison halves the search space, reducing the number of comparisons needed.
What is the best case time complexity of binary search?
O(1) when the item is found immediately.
What is the average time complexity of binary search?
O(log n) because the search space is halved each step.
What is the worst case time complexity of binary search?
O(log n) when the element is at the deepest level or not found.
What is the main disadvantage of binary search?
The data must be sorted before searching.
What is a hash table?
A data structure that stores data using a hash function to determine the index where the data is stored.
What is a hash function?
A function that converts a key into an index in a hash table, for example index = key mod table_size.
What is a key in a hash table?
The value used by the hash function to determine where data should be stored.
What is a hash collision?
When two different keys produce the same index in a hash table.
Why do collisions occur?
Because the number of possible keys is larger than the number of positions in the hash table.
What is chaining?
A collision resolution method where multiple values are stored at the same index using a list or linked list.
What is linear probing?
A collision resolution method where the algorithm searches the next available space in the table sequentially.
What is double hashing?
A collision resolution method where a second hash function is used to calculate a new index.
What is the average time complexity of searching in a hash table?
O(1) because the hash function directly calculates the storage index.
What is the worst case time complexity of a hash table search?
O(n) when many collisions occur.
Compare binary search and hash table searching.
Binary search requires sorted data and has O(log n) time complexity; hash tables do not require sorted data and have average O(1) time complexity.