|
| 1 | +# Insertion Sort |
| 2 | + |
| 3 | +Goal: Sort an array from low to high (or high to low). |
| 4 | + |
| 5 | +You are given an array of numbers and need to put them in the right order. The insertion sort algorithm works as follows: |
| 6 | + |
| 7 | +- Put the numbers on a pile. This pile is unsorted. |
| 8 | +- Pick a number from the pile. It doesn't really matter which one you pick, but it's easiest to pick from the top of the pile. |
| 9 | +- Insert this number into a new array. |
| 10 | +- Pick the next number from the unsorted pile and also insert that into the new array. It either goes before or after the first number you picked, so that now these two numbers are sorted. |
| 11 | +- Again, pick the next number from the pile and insert it into the array in the proper sorted position. |
| 12 | +- Keep doing this until there are no more numbers on the pile. You end up with an empty pile and an array that is sorted. |
| 13 | + |
| 14 | +That's why this is called an "insertion" sort, because you take a number from the pile and insert it in the array in its proper sorted position. |
| 15 | + |
| 16 | +## An example |
| 17 | + |
| 18 | +Let's say the numbers to sort are `[ 8, 3, 5, 4, 6 ]`. This is our unsorted pile. |
| 19 | + |
| 20 | +Pick the first number, `8`, and insert it into the new array. There is nothing in that array yet, so that's easy. The sorted array is now `[ 8 ]` and the pile is `[ 3, 5, 4, 6 ]`. |
| 21 | + |
| 22 | +Pick the next number from the pile, `3`, and insert it into the sorted array. It should go before the `8`, so the sorted array is now `[ 3, 8 ]` and the pile is reduced to `[ 5, 4, 6 ]`. |
| 23 | + |
| 24 | +Pick the next number from the pile, `5`, and insert it into the sorted array. It goes in between the `3` and `8`. The sorted array is `[ 3, 5, 8 ]` and the pile is `[ 4, 6 ]`. |
| 25 | + |
| 26 | +Repeat this process until the pile is empty. |
| 27 | + |
| 28 | +## In-place sort |
| 29 | + |
| 30 | +The above explanation makes it seem like you need two arrays: one for the unsorted pile and one that contains the numbers in sorted order. |
| 31 | + |
| 32 | +But you can perform the insertion sort *in-place*, without having to create a separate array. You just keep track of which part of the array is sorted already and which part is the unsorted pile. |
| 33 | + |
| 34 | +Initially, the array is `[ 8, 3, 5, 4, 6 ]`. The `|` bar shows where the sorted portion ends and the pile begins: |
| 35 | + |
| 36 | + [| 8, 3, 5, 4, 6 ] |
| 37 | + |
| 38 | +This shows that the sorted portion is empty and the pile starts at `8`. |
| 39 | + |
| 40 | +After processing the first number, we have: |
| 41 | + |
| 42 | + [ 8 | 3, 5, 4, 6 ] |
| 43 | + |
| 44 | +The sorted portion is `[ 8 ]` and the pile is `[ 3, 5, 4, 6 ]`. The `|` bar has shifted one position to the right. |
| 45 | + |
| 46 | +This is how the content of the array changes during the sort: |
| 47 | + |
| 48 | + [| 8, 3, 5, 4, 6 ] |
| 49 | + [ 8 | 3, 5, 4, 6 ] |
| 50 | + [ 3, 8 | 5, 4, 6 ] |
| 51 | + [ 3, 5, 8 | 4, 6 ] |
| 52 | + [ 3, 4, 5, 8 | 6 ] |
| 53 | + [ 3, 4, 5, 6, 8 |] |
| 54 | + |
| 55 | +In each step, the `|` bar moves up one position. As you can see, the beginning of the array up to the `|` is always sorted. The pile shrinks by one and the sorted portion grows by one, until the pile is empty and there are no more unsorted numbers left. |
| 56 | + |
| 57 | +## How to insert |
| 58 | + |
| 59 | +At each step you pick the top-most number from the unsorted pile and insert it into the sorted portion of the array. You must put that number in the proper place so that the beginning of the array remains sorted. How does that work? |
| 60 | + |
| 61 | +Let's say we've already done the first few elements and the array looks like this: |
| 62 | + |
| 63 | + [ 3, 5, 8 | 4, 6 ] |
| 64 | + |
| 65 | +The next number to sort is `4`. We need to insert that into the sorted portion `[ 3, 5, 8 ]` somewhere. |
| 66 | + |
| 67 | +Here's one way to do this: Look at the previous element, `8`. |
| 68 | + |
| 69 | + [ 3, 5, 8, 4 | 6 ] |
| 70 | + ^ |
| 71 | + |
| 72 | +Is this greater than `4`? Yes it is, so the `4` should come before the `8`. We swap these two numbers to get: |
| 73 | + |
| 74 | + [ 3, 5, 4, 8 | 6 ] |
| 75 | + <--> |
| 76 | + swapped |
| 77 | + |
| 78 | +We're not done yet. The new previous element, `5`, is also greater than `4`. We also swap these two numbers: |
| 79 | + |
| 80 | + [ 3, 4, 5, 8 | 6 ] |
| 81 | + <--> |
| 82 | + swapped |
| 83 | + |
| 84 | +Again, look at the previous element. Is `3` greater than `4`? No, it is not. That means we're done with number `4`. The beginning of the array is sorted again. |
| 85 | + |
| 86 | +This was a description of the inner loop of the insertion sort algorithm, which you'll see in the next section. It inserts the number from the top of the pile into the sorted portion by swapping numbers. |
0 commit comments