CJCoding With Joseph
15free / daySign inCreate account

University of Lethbridge - CPSC

Insertion Sort

Sorting Methods

What Is It?

  • Insertion Sort builds the sorted portion of the array one element at a time.

  • It starts by treating the first element as “sorted.”

  • Then it takes the next element and “inserts” it into the correct position among the already-sorted elements.

  • This repeats for each element in the array until everything is sorted.


Key Points

  • Great for small or nearly sorted arrays because it’s easy to implement.

  • Worst-case time complexity: O(n²).


Insertion Sort: Step by Step

Idea: In Insertion Sort, we treat the array in two parts: a “sorted” portion (on the left) and an “unsorted” portion (on the right). We take the first unsorted element and insert it into its correct spot among the sorted elements.

Members only

Keep reading this lesson

You've reached the end of the free preview. Unlock the rest of this lesson plus 27 more lessons in University of Lethbridge - CPSC.

  • Unlimited coding questions
  • Unlimited access to all classroom material
  • Weekly live call for questions and guidance
  • 3,000 AI tutor tokens / month
  • Personal SQL database (3 MB)
  • Progress tracking

Cancel any time.