Algorithms Flashcards
Linear Search Definition
Works by comparing the search item with the first value and then comparing the search item with the second value. This process is repeated until either the end of the data set is reached or the search item is found
Linear Search Advantages
- Can perform fast searches of small data sets
- Not affected by insertions and deletions as the linear search doesn’t require the list to be sorted
Linear Search Disadvantages
- Inefficient for large data sets
Linear Search Suitability
- Small data set
- Data set isn’t sorted
Binary Search Definition
Works by starting at the middle item of the data set and repeatedly dividing the list in half until either the item is found pr the entire data set is searched
Precondition - Data set must be sorted first
Binary Search Advantages
- Efficiently searches large data sets
Binary Search Suitability
- Large data sets
Binary Search Time Complexity
Best - O(1)
Average - O(n)
Worst - O(n)
Linear Search Time Complexity
Best - O(1)
Average - O(log n)
Worst - O(log n)