There are 4 algorithms you need to know for A-Level computer science.
- Quicksort
- Insertion Sort
- Merge Sort
- Bubble Sort
These algorithms are designed to sort a list into a certain order.
Quicksort
Time Complexity: Best -
O(n log(n))Average -O(n log(n))Worst -O(n²)
Space Complexity:O(log(n))
Quicksort is another recursive algorithm.
It broadly works by sorting items around a “pivot”, such that items to the left are less than the pivot, and items on the right are greater than the pivot.
After each pass, the pivot is in the correct position. The 2 sublists however are not yet sorted.
A left and right pointer is used (set to the start and end of the list respectively). The left pointer moves to the right until it reaches an element greater than the pivot. The right pointer moves to the left until it reaches an item smaller than the pivot. Once both have occured, the values at the left and right pointer swap positions.
This repeats until the pointers cross, at which point the item at the pivot swaps with the item at the right pointer. The pivot is now sorted.
Insertion Sort
Time Complexity: Best -
O(n)Average -O(n²)Worst -O(n²)
Space Complexity:O(1)
Insertion sort places elements into a sorted sequence, such that on the i-th iteration, the first i elements are sorted.
Insertion sort begins with the element at index 1, comparing it to the element on its left. If the elements are in the wrong order they are swapped.
The next item is then checked, it is inserted into the correct position on the sorted list on the left.
Merge Sort
Time Complexity: Best -
O(n log(n))Average -O(n log(n))Worst -O(n log(n))
Space Complexity:O(n)
Merge sort is a divide and conquer sorting algorithm.
It begins by recursively splitting the list in 2 until it is left with n lists of length 1.
It then merges these lists back together, each time merging the lists such that they are sorted.
This repeats until we have the final sorted list.
Bubble Sort
Time Complexity: Best -
O(n)Average -O(n²)Worst -O(n²)
Space Complexity:O(1)
Bubble sort works by “bubbling” the largest/last item to the end of the array each iteration.
The first 2 items are compared, and swapped if needed. Then the next two get swapped and compared.
This repeats until the end of the list, where the last item has now been sorted. This is one pass.
The time complexity is n², as n passes occur (each requiring n comparisons/swaps).
Optimisations
Bubble sort can be optimised:
- As the last item is guaranteed to be sorted each time, you only need to compare until
array#length - (i + 1) - If no swaps occur in a given pass, the list must be sorted and hence you can return early.
Cheat Sheet