You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Insertion sort is a simple sorting algorithm that builds the sorted list one item at a time. It works similarly to the way you might sort a hand of playing cards — you pick up each card and insert it into its correct position among the cards you have already sorted.
FUNCTION insertionSort(list)
FOR i = 1 TO LENGTH(list) - 1
current = list[i]
j = i - 1
WHILE j >= 0 AND list[j] > current
list[j + 1] = list[j]
j = j - 1
ENDWHILE
list[j + 1] = current
NEXT i
RETURN list
ENDFUNCTION
Sort the list: [5, 3, 8, 1, 2]
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.