C Program
#include<stdio.h> int main() { int a[100], n, i, j, key; scanf("%d", &n); for(i = 0; i < n; i++) scanf("%d", &a[i]); for(i = 1; i < n; i++) { key = a[i]; j = i - 1; while(j >= 0 && a[j] > key) a[j+1] = a[j--]; a[j+1] = key; } for(i = 0; i < n; i++) printf("%d ", a[i]); }
C Output
Input: 5 4 2 5 1 3 Output: 1 2 3 4 5
C++ Program
#include<iostream> using namespace std; int main() { int a[100], n; cin >> n; for(int i = 0; i < n; i++) cin >> a[i]; for(int i = 1; i < n; i++) { int key = a[i], j = i - 1; while(j >= 0 && a[j] > key) a[j+1] = a[j--]; a[j+1] = key; } for(int i = 0; i < n; i++) cout << a[i] << " "; }
C++ Output
Input: 4 9 7 3 6 Output: 3 6 7 9
JAVA Program
import java.util.*; class I { public static void main(String[] a) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), i, j, key; int[] arr = new int[n]; for(i = 0; i < n; i++) arr[i] = sc.nextInt(); for(i = 1; i < n; i++) { key = arr[i]; j = i - 1; while(j >= 0 && arr[j] > key) arr[j+1] = arr[j--]; arr[j+1] = key; } for(i = 0; i < n; i++) System.out.print(arr[i] + " "); } }
JAVA Output
Input: 6 10 2 8 1 6 4 Output: 1 2 4 6 8 10
Python Program
a = list(map(int, input().split())) for i in range(1, len(a)): k = a[i] j = i - 1 while j >= 0 and a[j] > k: a[j+1] = a[j] j -= 1 a[j+1] = k print(*a)
Python Output
Input: 8 4 3 7 Output: 3 4 7 8
In-Depth Learning – Entire Concept in Paragraphs
What is Insertion Sort?
Insertion Sort is a straightforward, intuitive algorithm for sorting that functions exactly the way you would sort cards in your hand. You pick one element at a time and insert it into its proper position with respect to the portion of the array that is already sorted on the left. It constructs the sorted array incrementally.
In contrast to Bubble Sort, Insertion Sort tends to work better on smaller lists and lists that are already partially sorted.
How the Code Works
Begin at the second element (index 1) since the first element is already "sorted".
Save the value to be inserted in a variable named key.
Compare key with items to its left.
If those items are larger than key, move them to the right.
Insert key into the proper position.
Do this for all items.
Example
Suppose we have:
javascript
Array: [4, 2, 5, 1, 3]
Pass 1: Insert 2 into [4] → [2, 4]
Pass 2: Insert 5 → [2, 4, 5]
Pass 3: Insert 1 → [1, 2, 4, 5]
Pass 4: Insert 3 → [1, 2, 3, 4, 5]
Every insertion puts the number into its proper place in the sorted sub-array.
Real-Life Analogy
Suppose you are organizing books on a shelf
You choose one book at a time and place it in the proper position among the previously sorted ones.
You move the rest to the right to create space — precisely how Insertion Sort acts.
This metaphor also explains why insertion sort works extremely well for almost sorted arrays — very little shifting is necessary.
Where and When Is It Used?
Insertion Sort works particularly well for:
Small datasets
Almost sorted arrays
Online sorting (where objects arrive individually)
It's not effective for big data, but it's very useful to develop a solid grasp of sorting concepts, conditionals, and loops.
It's frequently requested in:
College practicals and viva
Coding tests and exams
Logical MCQ questions
Time Complexity
Best Case (Already Sorted): O(n)
Average & Worst Case: O(n²)
Space Complexity: O(1) → In-place sorting
Although its worst-case time complexity is quadratic, it works very well with small or almost sorted data sets.
Python-Specific Benefit
Python simplifies the syntax with clear loops and index-based comparisons. It is a great tool for learning the algorithm without jumping into performance optimizations using more sophisticated techniques such as TimSort (which the built-in sort() function of Python utilizes).
SEO-Optimized Natural Paragraph for Ranking
Need to sort numbers easily and efficiently using Insertion Sort? This easy-to-follow tutorial demonstrates implementing Insertion Sort in C, C++, Java, and Python using concise and tidy code samples. Insertion Sort is a gradual sorting method that simulates the way people will naturally sort objects — inserting each object into its proper position one by one. Perfect for tiny or almost sorted data sets, this timeless algorithm imparts the essence of the sorting logic and is a stalwart interviewee and exam favorite. With examples, step-by-step breakdown of the logic, and real-world analogies, this is the ideal tutorial to master insertion sort in any programming language.
Social Plugin