C++ Program For Insertion Sort Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Insertion sort is a simple sorting algorithm that works by dividing the array into two parts, sorted and unsorted part. In each iteration, the first element from the unsorted subarray is taken and it is placed at its correct position in the sorted array. In this article, we will learn to write a C++ program to implement the Insertion sort algorithm. Insertion Sort Algorithm in C++The first element of the array is assumed to be a sorted subarray.Start with the element at index 1 and store it in a variable key.Compare the current element key with the elements in the sorted subarray from right to left (elements at indices from j = i-1 to j = 0).Shift elements greater than the key one position to the right to make space for the current element until it finds a smaller element than the key and j is not zero.When the correct position for the key is found, it is inserted at arr[j + 1].Repeat these steps for the remaining elements of the unsorted subarray until the complete array is sorted.Recommended PracticeInsertion SortTry It!Insertion Sort Program in C++ C++ // C++ program for insertion sort #include <bits/stdc++.h> using namespace std; // Function to sort an array // using insertion sort void insertionSort(int arr[], int n) { int i, key, j; for (i = 1; i < n; i++) { key = arr[i]; j = i - 1; // Move elements of arr[0..i-1], // that are greater than key, to one // position ahead of their // current position while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j = j - 1; } arr[j + 1] = key; } } // A utility function to print // an array of size n void printArray(int arr[], int n) { int i; for (i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; } // Driver code int main() { int arr[] = { 12, 11, 13, 5, 6 }; int N = sizeof(arr) / sizeof(arr[0]); insertionSort(arr, N); printArray(arr, N); return 0; } // This is code is contributed by rathbhupendra Output5 6 11 12 13 Complexity Analysis of Insertion SortTime Complexity Worst Case: O(n2)Best Case: O(n)Average Case: O(n2)Auxiliary Space: O(1) Working of Insertion Sort in C++Below is the working of Insertion Sort with the help of a pictorial example: Insertion Sort in C++ Please refer complete article on Insertion Sort for more details! Related ArticlesInsertion Sort for Singly Linked List Comment More info K kartik Follow Improve Article Tags : C++ C++ Array Programs Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++5 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++11 min readFile Handling through C++ Classes8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++4 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL3 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like