Insertion Sort
Introduction
Insertion sort is called insertion sort because the algorithm repeated inserts a value into an array that is already sorted. It essentially chops the array into two pieces. The first piece is sorted, the second is not. We repeatedly take a number from the second piece and insert it into the already sorted first piece of the array.
Algorithm
start with second variable.
put number into a temporary variable. This makes it possible to move items into the spot the number use to occupy. This spot is now considered to be an empty spot in the sorted portion of the array
if number in temporary can go into empty spot, put it in
otherwise move last item in the part into empty spot
repeat steps 3 to 4 until item is temp is placed into the first part of the array
repeat for every number in the second part of the array until all numbers are placed
Animation
C/C++ Implementation
Last updated