C Program For Insertion Sort Last Updated : 12 Dec, 2025 Comments Improve Suggest changes 20 Likes Like Report Insertion Sort is a simple comparison-based sorting algorithm that builds the final sorted list one element at a time.It divides the list into sorted and unsorted part. Initially, the first element is already considered sorted, while the rest of the list is considered unsorted. The algorithm then iterates through each element in the unsorted part, picking one element at a time, and inserts it into its correct position in the sorted part. It is easy to implement and works well for small datasets.Let's take an example of array arr = {12, 11, 13, 5, 6} that we need to sort in ascending order.Insertion Sort Algorithm in CStart with the second element (index 1) as the key.Compare the key with the elements before it.If the key is smaller than the compared element, shift the compared element one position to the right.Insert the key where the shifting of elements stops.Repeat steps 2-4 for all elements in the unsorted part of the list. C #include <math.h> #include <stdio.h> void insertionSort(int arr[], int N) { // Starting from the second element for (int i = 1; i < N; i++) { int key = arr[i]; int j = i - 1; // Move elements of arr[0..i-1], that are // greater than key, to one position to // the right of their current position while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j = j - 1; } // Move the key to its correct position arr[j + 1] = key; } } int main() { int arr[] = {12, 11, 13, 5, 6}; int N = sizeof(arr) / sizeof(arr[0]); printf("Unsorted array: "); for (int i = 0; i < N; i++) { printf("%d ", arr[i]); } printf("\n"); // Calling insertion sort on array arr insertionSort(arr, N); printf("Sorted array: "); for (int i = 0; i < N; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; } OutputUnsorted array: 12 11 13 5 6 Sorted array: 5 6 11 12 13 Time Complexity: O(N2)Auxiliary Space: O(1)Advantages of Insertion SortInsertion sort is that it is an in-place sorting algorithm.It is simple to implement which is great for small datasets.Insertion sort is adaptive in nature, i.e. it is appropriate for data sets which are already partially sorted.Best-case scenario only requires O(n) time.Note: The algorithm typically shows only the final sorted result, not the progress after each iteration, making it harder to trace or debug. Create Quiz Comment K kartik Follow 20 Improve K kartik Follow 20 Improve Article Tags : C Language InsertionSort C-DSA Explore C BasicsC Language Introduction6 min readIdentifiers in C3 min readKeywords in C2 min readVariables in C4 min readData Types in C3 min readOperators in C8 min readDecision Making in C (if , if..else, Nested if, if-else-if )7 min readLoops in C6 min readFunctions in C5 min readArrays & StringsArrays in C4 min readStrings in C5 min readPointers and StructuresPointers in C7 min readFunction Pointer in C5 min readUnions in C3 min readEnumeration (or enum) in C5 min readStructure Member Alignment, Padding and Data Packing8 min readMemory ManagementMemory Layout of C Programs5 min readDynamic Memory Allocation in C7 min readWhat is Memory Leak? How can we avoid?2 min readFile & Error HandlingFile Handling in C11 min readRead/Write Structure From/to a File in C3 min readError Handling in C8 min readUsing goto for Exception Handling in C4 min readError Handling During File Operations in C5 min readAdvanced ConceptsVariadic Functions in C5 min readSignals in C language5 min readSocket Programming in C8 min read_Generics Keyword in C3 min readMultithreading in C9 min read Like