2.3.1 Searching algorithms Flashcards
1
Q
What are the two searching algorithms?
A
- Binary search
- Linear search
2
Q
Explain binary search
A
- DIvide and conquer
- List must be sorted
- Looks at middle element
- Looks if desired element is larger or greater
- Undesired side is discarded
- Repeat until element is found
3
Q
What is the time and space complexity for binary search
A
- Time complexity: O(log n)
- Space complexity: O(1)
4
Q
Explain linear search
A
- Look at first item
- Is it desired element
- If not look at next item
- Repeat until desired element found
5
Q
What is the time and space complexity of linear search?
A
- Time complexity: O(n)
- Space complexity: O(1)