Insertion sort is a simple sorting algorithm in which the sorted list is built one entry at a time. It works like someone who “inserts” one more card at a time into a hand of cards that are already sorted. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. Complexity for insertion sort is O(n2) similar to algorithms such as selection sort or bubble sort.

For example,

To insert 12, we need to make room for it by moving first 36 and then 24.

Insertion Sort Example

Insertion Sort: Pass One

Insertion Sort: Pass Two

Insertion Sort: Pass Three

Insertion Sort: Pass Four

Insertion Sort: Pass Five

Insertion sort implementation in C++

void insertionSort(int arr[], int size)
	for (int i=1; i< size; i++)
		int value = arr[i];
		for(int j = i-1; j >= 0 && arr[j] > value;  j--)
			arr[j+1] = arr[j];
		arr[j+1] = value;
Tagged with: C/C++ languageData structures

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.