Insertion Sort pseudocode

Now that you know how to insert a value into a sorted subarray, you can implement insertion sort:

  1. Call insert to insert the element that starts at index 11 into the sorted subarray in index 00.

  2. Call insert to insert the element that starts at index 22 into the sorted subarray in indices 00 through 11.

  3. Call insert to insert the element that starts at index 33 into the sorted subarray in indices 00 through 22.

  4. ...

  5. Finally, call insert to insert the element that starts at index n1n-1 into the sorted subarray in indices 00 through n2n-2.

As a reminder, here’s the visualization that steps through the algorithm on a deck of cards:

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy