Let’s apply Insertion Sort to the array, [4, 2, 7, 1, 3] .

We begin the first pass-through by inspecting the value at index 1. This happens to contain the value 2:

images/optimizing_for_optimistic_scenarios/insertion_sort_6.png

Step 1: We temporarily remove the 2 and keep it inside a variable called temp_value . We represent this value by shifting it above the rest of the array:

images/optimizing_for_optimistic_scenarios/insertion_sort_7.png

Step 2: We compare the 4 to the temp_value , which is 2:

images/optimizing_for_optimistic_scenarios/insertion_sort_8.png

Step 3: Because 4 is greater than 2, we shift the 4 to the right:

images/optimizing_for_optimistic_scenarios/insertion_sort_9.png

There’s nothing left to shift, as the gap is now at the left end of the array.

Step 4: We insert the temp_value into the gap, completing our first pass-through:

images/optimizing_for_optimistic_scenarios/step_4.png

Next, we begin the second pass-through:

Step 5: In our second pass-through, we temporarily remove the value at index 2. We’ll store this in temp_value . In this case, the temp_value is 7:

images/optimizing_for_optimistic_scenarios/insertion_sort_12.png

Step 6: We compare the 4 to the temp_value :

images/optimizing_for_optimistic_scenarios/insertion_sort_13.png

The 4 is lower, so we won’t shift it. Since we reached a value that is less than the temp_value , this shifting phase is over.

Step 7: We insert the temp_value back into the gap, ending the second pass-through:

images/optimizing_for_optimistic_scenarios/insertion_sort_14.png

We now begin the third pass-through:

Step 8: We temporarily remove the 1 and store it in temp_value :

images/optimizing_for_optimistic_scenarios/insertion_sort_16.png

Step 9: We compare the 7 to the temp_value :

images/optimizing_for_optimistic_scenarios/insertion_sort_17.png

Step 10: The 7 is greater than 1, so we shift the 7 to the right:

images/optimizing_for_optimistic_scenarios/insertion_sort_18.png

Step 11: We compare the 4 to the temp_value :

images/optimizing_for_optimistic_scenarios/insertion_sort_19.png

Step 12: The 4 is greater than 1, so we shift it as well:

images/optimizing_for_optimistic_scenarios/insertion_sort_20.png

Step 13: We compare the 2 to the temp_value :

images/optimizing_for_optimistic_scenarios/insertion_sort_21.png

Step 14: The 2 is greater, so we shift it:

images/optimizing_for_optimistic_scenarios/insertion_sort_22.png

Step 15: The gap has reached the left end of the array, so we insert the temp_value into the gap, concluding this pass-through:

images/optimizing_for_optimistic_scenarios/step_15.png

Now, we begin the fourth pass-through:

Step 16: We temporarily remove the value from index 4, making it our temp_value . This is the value 3:

images/optimizing_for_optimistic_scenarios/insertion_sort_24.png

Step 17: We compare the 7 to the temp_value :

images/optimizing_for_optimistic_scenarios/insertion_sort_25.png

Step 18: The 7 is greater, so we shift the 7 to the right:

images/optimizing_for_optimistic_scenarios/insertion_sort_26.png

Step 19: We compare the 4 to the temp_value :

images/optimizing_for_optimistic_scenarios/insertion_sort_27.png

Step 20: The 4 is greater than the 3, so we shift the 4:

images/optimizing_for_optimistic_scenarios/insertion_sort_28.png

Step 21: We compare the 2 to the temp_value . The 2 is less than 3, so our shifting phase is complete:

images/optimizing_for_optimistic_scenarios/insertion_sort_29.png

Step 22: We insert the temp_value back into the gap:

images/optimizing_for_optimistic_scenarios/step_22.png

Our array is now fully sorted:

images/optimizing_for_optimistic_scenarios/insertion_sort_31.png

Code Implementation: Insertion Sort

Here is a Python implementation of Insertion Sort:

​  ​ def ​ ​ insertion_sort ​(array):
​  ​ for ​ index ​ in ​ range(1, len(array)):
​  
​  temp_value = array[index]
​  position = index - 1
​  
​  ​ while ​ position >= 0:
​  ​ if ​ array[position] > temp_value:
​  array[position + 1] = array[position]
​  position = position - 1
​  ​ else ​:
​  ​ break ​
​  
​  array[position + 1] = temp_value
​  
​  ​ return ​ array

Let’s walk through this code step by step.

First, we start a loop beginning at index 1 that runs through the entire array. Each round of this loop represents a pass-through:

​  ​ for ​ index ​ in ​ range(1, len(array)):

Within each pass-through, we save the value we’re “removing” in a variable called temp_value :

​  temp_value = array[index]

Next, we create a variable called position , which will start immediately to the left of the index of the temp_value . This position will represent each value we compare against the temp_value :

​  position = index - 1

As we move through the pass-through, this position will keep moving leftward as we compare each value to the temp_value .

We then begin an inner while loop, which runs as long as position is greater or equal to 0:

​  ​ while ​ position >= 0:

We then perform our comparison. That is, we check whether the value at position is greater than the temp_value :

​  ​ if ​ array[position] > temp_value:

If it is, we shift that left value to the right:

​  array[position + 1] = array[position]

We then decrement position by 1 to compare the next left value against the temp_value in the next round of the while loop:

​  position = position - 1

If at any point we encounter a value at position that is greater or equal to the temp_value , we can get ready to end our pass-through, since it’s time to move the temp_value into the gap:

​  ​ else ​:
​  ​ break ​

The final step of each pass-through is moving the temp_value into the gap:

​  array[position + 1] = temp_value

After all pass-throughs have been completed, we return the sorted array:

​  ​ return ​ array