Introduction
Insertion Sort is a simple and efficient sorting algorithm for small datasets.
The idea is:
- build sorted portion gradually
- insert each element
into its correct position
It works similarly to:
- arranging playing cards in hand
This problem helps in understanding:
- sorting fundamentals
- shifting elements
- insertion logic
- partially sorted arrays
Example
Input:arr = [5, 2, 4, 6, 1, 3]
Output:
[1, 2, 3, 4, 5, 6]
Explanation: Each element is inserted into correct position inside sorted portion.
1 <= arr.length <= 10^5-10^4 <= arr[i] <= 10^4Approach 1 : Basic Insertion Sort
Explanation
The simplest way to sort using Insertion Sort is:
- Assume first element is sorted
- Pick next element
- Insert it into correct position
inside sorted portion - Repeat for all elements
After every pass:
- left portion remains sorted
Steps
- Start from second element.
- Store current element.
- Shift larger elements right.
- Insert current element.
- Continue traversal.
Dry Run
Input:[5, 2, 4, 6, 1, 3]
Pass 1: Current element: 2
Shift 5 right
Insert 2
Array becomes: [2, 5, 4, 6, 1, 3]
Pass 2:
Current element: 4 Shift 5 right
Insert 4
Array becomes:
[2, 4, 5, 6, 1, 3]
Continue remaining passes...
Final Result: [1, 2, 3, 4, 5, 6]
Basic Insertion Sort Code
Complexity Analysis
Time Complexity: O(n²)Explanation:
Elements may require repeated shifting.
Space Complexity: O(1) Explanation:
Sorting is performed in-place.
Approach 2 : Optimized Insertion Sort
Explanation
Insertion Sort performs efficiently for:
- nearly sorted arrays
Optimization idea:
- stop shifting immediately
when correct position is found
This reduces unnecessary comparisons.
Steps
- Traverse array.
- Store current element.
- Shift larger elements only.
- Stop early if position correct.
- Insert element.
Dry Run
Input:[1, 2, 3, 5, 4]
Pass 1: Current element:
2 Already greater than 1 No shifting needed
Pass 2: Current element: 3
Already greater than 2
No shifting needed
Pass 4: Current element: 4 Shift 5 right Insert 4
Final Result: [1, 2, 3, 4, 5]
Optimized Insertion Sort Code
Complexity Analysis
Best Case Time Complexity: O(n)Explanation:Already sorted arrays require minimal shifting.
Average Case Time Complexity: O(n²)
Worst Case Time Complexity: O(n²)
Space Complexity: O(1) Explanation:
Sorting is performed in-place.
Edge Cases
- Empty array
- Single element array
- Already sorted array
- Reverse sorted array
- Duplicate elements present
Why This Problem is Important
Insertion Sort helps in understanding:
- Sorting fundamentals
- Element shifting
- Insertion logic
- Partially sorted arrays
- In-place sorting
It is one of the most important beginner sorting algorithms.
Real-World Applications
Insertion Sort concepts are used in:
- Small datasets
- Hybrid sorting algorithms
- Online sorting systems
- Educational visualization
- Nearly sorted data handling
Common Mistakes
- Incorrect shifting logic
- Wrong insertion position
- Forgetting key storage
- Index out of bounds errors
Interview Tips
Interviewers often expect:
- Sorting fundamentals
- Shifting explanation
- Best case optimization understanding
Always explain why:
- left portion remains sorted
after every iteration
Related Questions
- Bubble Sort
- Selection Sort
- Merge Sort
- Quick Sort
- Sort Colors
Final Takeaway
Insertion Sort is a fundamental comparison-based sorting algorithm that teaches element shifting and insertion techniques. Understanding Insertion Sort builds a strong foundation for advanced sorting algorithms and interview problems.