Insertion Sort Program
Insertion sort works as follows.
-
The array of values to be sorted is divided into two partitions:
the partition of sorted values and that of unsorted values.
-
In each step of insertion sorting, the first element in the unsorted partition
is picked up and is inserted into an appropriate position
in the sorted partition.
-
The sorting proceeds until the elements in the unsorted partition
are exhausted.
-
We use floorIdx to designate the last element in the sorted
partition.
-
The array elements with indexes 0 through floorIdx
are in the partition of sorted values, and the rest in the patition
of unsorted values.
-
The first element of the unsorted partition
has array index floorIdx + 1.
-
Initially, only the first element in the array belongs to the sorted
partition, and hence floorIdx = 0.
-
While the slot where the insertion should occur is searched for,
sorted values greater than the value to be inserted are
pushed down within the sorted partition.
-
Sorted values are shown in blue, and unsorted values in green.
Source Code of the InsertSort Program
Source Code of the InsertSortG Program
(Graphical Version)
Back
Home