C++ Programming Code Examples C++ > Sorting Searching Code Examples Simple Insertion Sort Program in C++ Simple Insertion Sort Program in C++ Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. However, insertion sort provides several advantages like simple implementation, efficient for (quite) small data sets, more efficient in practice than most other simple quadratic algorithms, adaptive, stable, in-place; i.e., only requires a constant amount of additional memory space, online; i.e., can sort a list as it receives it. #include <iostream> #include<conio.h> #include<stdlib.h> #define maxsize 5 using namespace std; void insertion(int[]); int main() { int arr_sort[maxsize], i, j, a, t; cout << "Simple C++ Insertion Sort Example - Array\n"; cout << "\nEnter " << maxsize << " Elements for Sorting : " << endl; for (i = 0; i < maxsize; i++) cin >> arr_sort[i]; cout << "\nYour Data :"; for (i = 0; i < maxsize; i++) { cout << "\t" << arr_sort[i]; } for (i = 1; i < maxsize; i++) { t = arr_sort[i]; j = i - 1; while (j >= 0 && arr_sort[j] > t) { arr_sort[j + 1] = arr_sort[j]; j = j - 1; } arr_sort[j + 1] = t; cout << "\nIteration : " << i; for (a = 0; a < maxsize; a++) { cout << "\t" << arr_sort[a]; } } cout << "\n\nSorted Data :"; for (i = 0; i < maxsize; i++) { cout << "\t" << arr_sort[i]; } getch(); }