5.5 - Sorting Algorithms
The purpose of sorting algorithms
Sorting algorithms are methods used to arrange items in a list into a specific order, such as numerical or alphabetical.
How bubble sort works
Bubble sort is a straightforward sorting algorithm that repeatedly compares and swaps adjacent items in a list until the entire list is ordered.
Steps in the bubble sort algorithm
- Start with the first two items in the list.
- Compare them: if they are in the wrong order (e.g., for ascending order, if the first is larger than the second), swap them; otherwise, leave them as they are.
- Move to the next pair (second and third items) and repeat the comparison and possible swap.
- Continue this until you reach 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 process for the remaining items until a full pass occurs with no swaps, indicating the list is sorted.
Trace table for bubble sort example
Here is an example of sorting the numbers 62, 14, 89, 37, 51 from smallest to largest using bubble sort. The table shows each pass and the swaps made.
| Pass | List state after comparisons | Swaps made | Notes |
|---|---|---|---|
| Initial | 62, 14, 89, 37, 51 | - | Starting list |
| 1 | 14, 62, 37, 51, 89 | Swapped 62>14, 89>37, 89>51 | 89 is now in correct place |
| 2 | 14, 37, 51, 62, 89 | Swapped 62>37, 62>51 | 62 is now in correct place |
| 3 | 14, 37, 51, 62, 89 | No swaps | 51 is now in correct place |
| 4 | 14, 37, 51, 62, 89 | No swaps | List is sorted, but final pass confirms |
In this process, the algorithm performs a final pass with no swaps to confirm the list is fully sorted. This step is essential, as a computer program would need it to know when to stop.
How insertion sort works
Insertion sort is another simple sorting algorithm that builds a sorted list gradually by taking each item and inserting it into its correct position among the already sorted items. The algorithm stops once the last item in the list has been inserted.
Steps in the insertion sort algorithm
- Begin with the second item in the list.
- Compare it to all the items before it (initially just the first item) and insert it into the correct position to maintain order.
- Move to the next unsorted item (third, then fourth, and so on) and repeat the comparison and insertion with all previously sorted items.
- Continue until the last item in the list has been inserted into its correct place, at which point the entire list is sorted.
Trace table for insertion sort example
Here is an example of sorting the words "Banana", "Cherry", "Date", "Apple", "Elderberry" in alphabetical order using insertion sort. The table shows the list state after each insertion.
| Step | Item to insert | List state after insertion | Notes |
|---|---|---|---|
| Initial | - | Banana, Cherry, Date, Apple, Elderberry | Starting list |
| 1 | Cherry | Banana, Cherry, Date, Apple, Elderberry | Cherry after Banana (no change) |
| 2 | Date | Banana, Cherry, Date, Apple, Elderberry | Date after Cherry (no change) |
| 3 | Apple | Apple, Banana, Cherry, Date, Elderberry | Apple inserted before Banana |
| 4 | Elderberry | Apple, Banana, Cherry, Date, Elderberry | Elderberry inserted after Date |
Once the last item (Elderberry) is inserted, the algorithm stops because all items are now in alphabetical order.
Strengths and weaknesses of bubble sort and insertion sort
Bubble sort and insertion sort share similar advantages and limitations.
Strengths of bubble sort and insertion sort
- Simplicity - Both algorithms are easy to understand and implement in code, requiring minimal complex logic.
- Ease of checking sorted lists - They can quickly verify if a list is already sorted, as few comparisons are needed in such cases.
Weaknesses of bubble sort and insertion sort
- Inefficiency for large lists - They require many comparisons, making them slow for big datasets.
Other sorting algorithms, like merge sort, are more complex but far more efficient for large lists.