O'Reilly logo

A Common-Sense Guide to Data Structures and Algorithms by Jay Wengrow

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Insertion Sort in Action

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

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

images/chapter7/new/optimizing_for_optimistic_scenarios-centered-no-text_Part6.png

Setup: 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/chapter7/new/optimizing_for_optimistic_scenarios-centered-no-text_Part7.png

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

images/chapter7/new/optimizing_for_optimistic_scenarios-centered-no-text_Part8.png

Step #2: Since 4 is greater than 2, we shift the 4 to the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required