What is Big-O notation?
It allows us to talk about the efficiency of an algorithm no matter what computer it is running on.
What is time complexity?
It is based on the number of items the algorithm is executed on - it tells us how the time taken scales with the size of the problem.
What are the types of time complexity?
Worst-case complexity - e.g. maximum time it could take to sort a list.
Best-case complexity - e.g. shortest possible time it could take to sort a list.
Average-case complexity: e.g. expected time it generally takes to sort a list.
Algorithmic time complexity always refers to the worst case.
What is the time complexity of a linear search?
O(n)
What is the time complexity of a binary search?
O(log n)
What is the time complexity of a binary tree search?
O(log n)
What is the time complexity of a bubble sort?
O(n^2)
What is the time complexity of a merge sort?
O(n logn)
What is the explanation of the time complexity for a linear search?
As the size of the list/problem increases, the time taken increases at the same rate. The worst/case scenario is that all n items will be visited (if the item is at the end).
What is the explanation of the time complexity for a binary search?
Each comparison halves the size of the list that has to be searched through.
If the size of the list doubles then the number of comparisons needed only increases by 1.
What are the disadvantages of a binary search?
It requires the list to be ordered. This takes time and is reliant on an efficient sorting algorithm.
What is the explanation of the time complexity for a bubble sort?
For n items in the list:
The algorithm makes (at most) n passes though the list.
On each pass it examines n items.
There are (n)(n) operations in total.
A time complexity of O(n^2).
This means that if the list size doubles, the time taken to sort it quadruples.
What is the explanation of the time complexity for a merge sort?
Merge sort works by repeatedly halving the list into sub lists until there are n lists of 1 element.
The halving process takes O(log n) operations.
These sublists are then compared with adjacent sublists and ordered.
This takes O(n) operations.