|
| 1 | +--- |
| 2 | +title: "Implementing bubble sort" |
| 3 | +categories: |
| 4 | + - Sorting algorithms |
| 5 | +tags: |
| 6 | + - sorting |
| 7 | + - performance |
| 8 | + - loops |
| 9 | +toc: true |
| 10 | +toc_sticky: true |
| 11 | +--- |
| 12 | +Bubble sort is a simple sorting algorithm. Whilst not particularly efficient, it can be useful for small datasets, or datasets that are already reasonably sorted. The reason for this is that bubble sort takes multiple loops through the entire array to get to a fully sorted state, so the more that we can limit these iterations, the more effective it is. |
| 13 | + |
| 14 | +The algorithm is based on comparison of pairs. It compares pairs of values and swaps them based on their rank. |
| 15 | + |
| 16 | +# Step-by-step |
| 17 | + |
| 18 | +Bubble sort begins by comparing the first two values in the unsorted array. In this case, 3 is greater than 2, so the values are swapped such that they are sorted. |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +The algorithm then compares elements 2 and 3. In this case, 3 is less than 4, so the elements are already sorted and no action is carried out. |
| 23 | + |
| 24 | +This is repeated for each pair across the array until we reach the end. At this point, the last element in the array is in its completely sorted position. We've moved the largest number to the end of the array. |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +Then the algorithm starts again from the left, swapping pairs as applicable. At the end of this iteration, the last two elements are in their completely sorted positions. |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +This is repeated until the algorithm can move through the entire array without having to do any pair swapping. |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +# Implementation |
| 37 | +Here was my first attempt at implementing bubble sort. |
| 38 | + |
| 39 | +```matlab |
| 40 | +function a = bubblesort(a) |
| 41 | +%BUBBLESORT Sort using bubble sort algorithm |
| 42 | +% |
| 43 | +% out = bubblesort(in) where in is an vector of numbers |
| 44 | +
|
| 45 | +% loop through the entire array multiple times |
| 46 | +for ii = 1:numel(a) - 1 |
| 47 | + swapped = false; |
| 48 | + |
| 49 | + % loop through each pair in the array |
| 50 | + for jj = 1:numel(a) - 1 |
| 51 | +
|
| 52 | + % swap the pair if required |
| 53 | + if a(jj) > a(jj + 1) |
| 54 | + a(jj:jj + 1) = a(jj + 1:-1:jj); |
| 55 | + swapped = true; |
| 56 | + end % if |
| 57 | + end % for |
| 58 | + |
| 59 | + % if we didn't have to any swapping, then the array is sorted |
| 60 | + if ~swapped |
| 61 | + return |
| 62 | + end % if |
| 63 | +end % for |
| 64 | +
|
| 65 | +end % bubblesort |
| 66 | +``` |
| 67 | + |
| 68 | +While this does work, there are a couple improvements we can make. One of them is performance based. In this original implementation, I am ignoring the fact that for each full iteration through the array, we no longer need to check the last element. Hence, for each `ii`, we can check one less `jj`. For an array of 10,000 elements, this cut my sorting time down from around 120 seconds to 60 seconds. |
| 69 | + |
| 70 | +```matlab |
| 71 | +% loop through each pair in the array |
| 72 | +for jj = 1:numel(a) - ii |
| 73 | +``` |
| 74 | + |
| 75 | +The final thing to do was to tidy up the original loop. Rather than using a for loop which we break out of, this is the perfect use case for a while loop. We know that we need to continue to iterate through the array until a certain condition is met; in this case that no swaps were carried out. |
| 76 | + |
| 77 | +```matlab |
| 78 | +function a = bubblesort(a) |
| 79 | +%BUBBLESORT Sort using bubble sort algorithm |
| 80 | +% |
| 81 | +% out = bubblesort(in) where in is an vector of numbers |
| 82 | +
|
| 83 | +% initialise |
| 84 | +swapped = true; |
| 85 | +iter = 0; |
| 86 | +
|
| 87 | +% loop through the entire array until we haven't done any pair swapping |
| 88 | +while swapped |
| 89 | + |
| 90 | + % reset and increment |
| 91 | + swapped = false; |
| 92 | + iter = iter + 1; |
| 93 | + |
| 94 | + % for each pair in the array that we can't guanrantee is in its correct |
| 95 | + % location, swap if out of order |
| 96 | + for ii = 1:numel(a) - iter |
| 97 | + if a(ii) > a(ii + 1) |
| 98 | + a(ii:ii + 1) = a(ii + 1:-1:ii); |
| 99 | + swapped = true; |
| 100 | + end % if |
| 101 | + end % for |
| 102 | +end % for |
| 103 | +
|
| 104 | +end % bubblesort |
| 105 | +``` |
| 106 | + |
| 107 | +**BONUS:** Due to how strings work in matlab, this function will also sort arrays of strings alphabetically! Try `bubblesort(["g" "c" "e" "b" "a" "d" "f"])` |
| 108 | +{: .notice--info} |
0 commit comments