|
1 | 1 | = Bubble Sort
|
2 | 2 |
|
3 | 3 | Bubble sort is a simple sorting algorithm that "bubbles up" the biggest values to the right side of the array.
|
| 4 | +It's also call _((sinking sort))_ because the largest values sinks to the right side of the array. |
| 5 | +This algorithm is adaptive which means that if the array is already sorted it will take only _O(n)_ to "sort". |
| 6 | +However, if the array is quite out of order it will require _O(n^2^)_ to sort. |
4 | 7 |
|
5 | 8 | == Bubble Sort Implementation
|
6 | 9 |
|
7 |
| -Bubble sort can be implemented in any programming language. This are the general steps that this sorting algorithm follows: |
8 |
| - |
9 |
| -.Bubble Sort Algorithm |
10 |
| -. It moves one element at a time (from left to right). Everything on the left of the current element is already sorted, while everything to the right is not. |
11 |
| -. Start with the first element and make it the current element. |
12 |
| -. Compare elements to right of the current element. |
13 |
| -. Bubble up big values to the right of the array. |
14 |
| -.. Swap elements if the previous element is bigger than the previous one. |
15 |
| -. Move the current pointer to the next element and repeat for the rest of the array |
16 |
| - |
17 |
| - |
18 |
| -Let's convert these words into code! |
| 10 | +Bubble sort can be implemented in any programming language. Let's implement it first and explain it. |
19 | 11 |
|
20 | 12 | .Bubble Sort implementation in JavaScript
|
21 | 13 | [source, javascript]
|
22 | 14 | ----
|
23 | 15 | include::{codedir}/algorithms/sorting/bubble-sort.js[tag=sort, indent=0]
|
24 | 16 | ----
|
25 | 17 | <1> Convert any kind of iterable (array, sets, etc.) into an array or if is already and array it clones it so the input is not modified.
|
26 |
| -<2> Goes through all the elements in the array |
27 |
| -<3> *Compares in pairs* (current vs next element) and swap them only if they are out of order. Bubbles up largest |
28 |
| -<4> If no swap was needed that means everything is sorted (this make this sorting adaptive, if everything is sorted it will be _O(n)_) |
29 |
| -<5> If a swap as performed means that the largest element is all the way to the right side and no need compare it anymore, thus `array.length - left`. |
| 18 | +<2> Starting from 0 compare current and next element |
| 19 | +<3> If they are out of order, swap the pair |
| 20 | +<4> Repeat pair comparison `n - 1` times, until the largest element is on the right side. |
| 21 | +<5> (optimization) If there was no swap means that the array is already sorted and no more work is needed. This make this sorting adaptive, if everything is sorted it will be only one pass: _O(n)_. |
| 22 | +<6> Each step moves the largest element from `0` to `n - i` to the right side. So, we need to do this `n - 1` to sort the array in case most elements needs to be swapped. |
30 | 23 |
|
31 | 24 | Bubble sort has a <<Quadratic>> running time, as you might infer from the nested for-loop.
|
32 | 25 |
|
|
0 commit comments