7.5 - Sorting Algorithms
Introduction to sorting algorithms
Sorting algorithms are methods used to arrange items in a list into a specific order, such as ascending or descending. They are essential in computer science because organised data is easier to search, process, and analyse. Different algorithms vary in efficiency, especially for large lists, and each has strengths and weaknesses depending on the list size and initial order.
Bubble sort algorithm
Bubble sort is a simple algorithm that repeatedly compares and swaps adjacent items in a list until the entire list is sorted. It gets its name because larger (or smaller) items "bubble" to their correct position with each pass through the list. While easy to understand, it can be slow for large lists.
Steps in bubble sort

- Start with the first two items in the list.
- Compare them: if they are in the wrong order, swap them; if not, leave them as they are.
- Move to the next pair (second and third items) and repeat the comparison and possible swap.
- Continue this process until the end of the list – this completes one pass. The last item is now in its correct position, so exclude it from future passes.
- Repeat the passes until a full pass occurs with no swaps, meaning the list is sorted.
Example of bubble sort
Consider sorting the numbers 72, 18, 45, 12, 91, 53 in ascending order.
First pass:
- Compare 72 and 18: swap to get 18, 72, 45, 12, 91, 53
- Compare 72 and 45: swap to get 18, 45, 72, 12, 91, 53
- Compare 72 and 12: swap to get 18, 45, 12, 72, 91, 53
- Compare 72 and 91: no swap
- Compare 91 and 53: swap to get 18, 45, 12, 72, 53, 91
Second pass:
Results in 18, 12, 45, 53, 72, 91
Third pass:
Results in 12, 18, 45, 53, 72, 91
Fourth pass:
No swaps occur, so the list is sorted as 12, 18, 45, 53, 72, 91
This example shows how bubble sort focuses on pairs, making it straightforward but potentially requiring many passes.
Advantages of bubble sort
- Simple to implement and understand, as it only compares two items at a time.
- Efficient for checking if a list is already sorted: for items, it needs just comparisons in one pass.
- Uses minimal memory, as sorting happens in the original list without creating new ones.
Disadvantages of bubble sort
- Inefficient for large lists: in the worst case, it requires comparisons for items.
- Does not handle very large datasets well due to its slow performance.
Merge sort algorithm
Merge sort is a divide-and-conquer algorithm that breaks a list into smaller sub-lists, sorts them, and then merges them back together. It exploits the fact that small lists are easier to sort and ordered lists are simpler to merge than unordered ones. This makes it more efficient for larger lists compared to simpler sorts.
Steps in merge sort

- Divide the list into two sub-lists, with the second starting at the middle item (calculated as for items).
- Recursively divide each sub-list until every sub-list contains only one item (which is inherently sorted).
- Merge pairs of sub-lists, sorting items into the correct order during the merge.
- Repeat the merging process until all sub-lists are combined into one sorted list.
Merging always occurs between two ordered lists, comparing the smallest unsorted items from each and adding the smaller one to the new list.
Example of merge sort
Consider sorting the letters M, S, B, R, V, E, J, G in alphabetical order.
Splitting phase:
- Original: M S B R V E J G
- Split into: M S B R and V E J G
- Further split: M S and B R; V E and J G
- Final split: M and S; B and R; V and E; J and G
Merging phase:
- Merge M and S to M S; B and R to B R; V and E to E V; J and G to G J
- Merge M S and B R to B M R S; E V and G J to E G J V
- Final merge: B M R S and E G J V to B E G J M R S V
This process handles uneven splits naturally, such as merging a two-item list with a one-item list.
Advantages of merge sort
- More efficient and faster than bubble or insertion sort for large lists, with consistent performance regardless of the initial order.
- Used in many programming languages like Python and Java as a primary sorting method due to its reliability.
Disadvantages of merge sort
- Slower for small lists, as it always performs full splitting and merging even if the list is already sorted.
- Requires more memory to create and manage separate sub-lists during the process.
Insertion sort algorithm
Insertion sort builds a sorted list by taking each item in sequence and inserting it into its correct position relative to the items already sorted. It starts with the first item as a sorted sub-list and expands it one item at a time, making it intuitive and effective for small or nearly sorted lists.
Steps in insertion sort
- Begin with the second item in the list.
- Compare it to all previous items (initially just the first) and insert it in the correct position.
- Repeat for the third item, comparing it to the now-sorted first two items, and insert accordingly.
- Continue this process for each subsequent item until the entire list is sorted.
Example of insertion sort

Consider sorting the words Apple, Zebra, Pear, Grape, Orange, Kiwi, Banana in alphabetical order.
Step-by-step process:
- Initial: Apple Zebra Pear Grape Orange Kiwi Banana
- Insert Zebra: No change (after Apple)
- Insert Pear: Apple Pear Zebra Grape Orange Kiwi Banana
- Insert Grape: Apple Grape Pear Zebra Orange Kiwi Banana
- Insert Orange: Apple Grape Orange Pear Zebra Kiwi Banana
- Insert Kiwi: Apple Grape Kiwi Orange Pear Zebra Banana
- Insert Banana: Apple Banana Grape Kiwi Orange Pear Zebra
The final sorted list is Apple Banana Grape Kiwi Orange Pear Zebra.
This method efficiently handles additions to an already ordered list.
Advantages of insertion sort
- Intuitive and easy to code, mimicking how people might sort items manually.
- Performs well on small lists or those that are already mostly sorted: best case requires comparisons for items.
- Uses minimal additional memory, as it sorts within the original list.
- Quick to add new items to a sorted list or verify if a list is already ordered.
- Often combined with merge sort in hybrid algorithms to optimise for small sub-lists.
Disadvantages of insertion sort
- Inefficient for very large lists: worst case requires comparisons, similar to bubble sort.