### The Insertion Sort Algorithm

• How humans sort a hand of cards

• The insertion sort takes a page from the Human card sorting playbook.

• Here how most card player would do when they need to sport the cards in their hand:

 Make sure the cards at the start (= left) are sorted (So only cards at the end (right) are unsorted) Find the smallest card in the unsorted (tail) "set" Insert this smallest card at the end of the sorted (head) set

• Example:

• Suppose we have 13 cards and 7 cards have been sorted (with 6 card unsorted):

 ``` A 2 3 4 5 6 7 K 10 J 8 9 Q | | +-------------------+ Sorted ```

• Iteration:

• Find the smallest card in the unsorted (tail) "set": result is 8

• Insert this smallest card at the end of the sorted (head) set

State after this iteration:

 ``` A 2 3 4 5 6 7 8 K 10 J 9 Q | | +----------------------+ Sorted ```

• Iteration:

• Find the smallest card in the unsorted (tail) "set": result is 9

• Insert this smallest card at the end of the sorted (head) set

State after this iteration:

 ``` A 2 3 4 5 6 7 8 9 K 10 J Q | | +-------------------------+ Sorted ```

• And so on !!!

• Example: insertion sort

• Example of the operation of the insertion sort

• Input data:

 ``` 6.4 4.5 1.2 2.2 3.1 ```

• Iteration 1:

• i = 0
• Find the smallest value among a[0], a[1], ..., a[n]
• Let s be the index of the array element with the smallest value

 ``` i=0 s=2 | | V V 6.4 4.5 1.2 2.2 3.1 ```

• Copy a[s] to a help variable to make space:

 ``` i=0 s=2 help = 1.2 | | V V 6.4 4.5 ... 2.2 3.1 ```

• Move the array elements a[i(=0)], ..., a[s-1] down one position to make space for the value 1.2:

 ``` i=0 s=2 help = 1.2 | | V V ... 6.4 4.5 2.2 3.1 ```

• Put the value 1.2 (in help) in a[i(=0)]:

 ``` i=0 s=2 help = 1.2 | | V V 1.2 6.4 4.5 2.2 3.1 ```

• Iteration 2:

• i = 1
• Find the smallest value among a[1], a[2], ..., a[n]
• Let s be the index of the array element with the smallest value

 ``` i=1 s=2 | | V V 1.2 6.4 4.5 2.2 3.1 ```

• Copy a[s] to a help variable to make space:

 ``` i=1 s=2 help = 2.2 | | V V 1.2 6.4 4.5 ... 3.1 ```

• Move the array elements a[i(=1)], ..., a[s-1] down one position to make space for the value 2.2:

 ``` i=1 s=2 help = 2.2 | | V V 1.2 ... 6.4 4.5 3.1 ```

• Put the value 2.2 (in help) in a[i(=1)]:

 ``` i=1 s=2 help = 2.2 | | V V 1.2 2.2 6.4 4.5 3.1 ```

• Iteration 3:

• i = 2
• Find the smallest value among a[2], a[3], ..., a[n]
• Let s be the index of the array element with the smallest value

 ``` i=2 s=4 | | V V 1.2 2.2 6.4 4.5 3.1 ```

• Copy a[s] to a help variable to make space:

 ``` i=2 s=4 help = 3.1 | | V V 1.2 2.2 6.4 4.5 ... ```

• Move the array elements a[i(=2)], ..., a[s-1] down one position to make space for the value 3.1:

 ``` i=2 s=4 help = 3.1 | | V V 1.2 2.2 ... 6.4 4.5 ```

• Put the value 3.1 (in help) in a[i(=2)]:

 ``` i=2 s=4 help = 3.1 | | V V 1.2 2.2 3.2 6.4 4.5 ```

• And so on

You must do this n−1 times.

• Insertion sort in Java

• Will be a homework assignment....