Article written by Rishabh Dev Choudhary, under the guidance of Neeraj Jhawar, a Senior Software Development Manager and Engineering Leader. Reviewed by Manish Chawla, a problem-solver, ML enthusiast, and an Engineering Leader with 20+ years of experience.
Insertion sort is a simple sorting algorithm that builds the final sorted array one item at a time, much like sorting a hand of playing cards. When you pick up a new card, you compare it to the ones already in your hand and insert it into its correct position.
While it has an O(n squared) general time complexity, it performs remarkably well in O(n) time, on nearly sorted data or small datasets.
This guide covers how it works, a step-by-step example, practical implementations, time and space complexity analysis, and when to choose it over other algorithms.
Insertion sort is a fundamental sorting algorithm that demonstrates the concept of incremental ordering by building a sorted sequence one element at a time. It plays a key role in developing a strong understanding of how sorting algorithms operate and manipulate data. Due to its simplicity and conceptual clarity, it is an important topic for beginners and a common topic in technical interview preparation at all levels.
Insertion Sort is a comparison-based sorting algorithm that builds a sorted list one element at a time. It works by taking each element from the unsorted part and inserting it into its correct position in the sorted part. At its core, the insertion sort algorithm operates by dividing the data into two conceptual parts: a sorted subarray and an unsorted subarray.
Initially, the sorted subarray consists of just the first element. The algorithm then iterates through the remaining unsorted items one by one. For each iteration, it selects the next unsorted element and scans backward through the sorted portion, shifting larger elements to the right until it finds the correct spot to insert the new item.
It mirrors exactly how a person organizes playing cards in their hand, taking an unsorted card, scanning the organized ones from right to left, and placing the new card in its rightful spot.
Understanding how insertion sort works is critical for mastering the algorithm. Here is the conceptual process of how insertion sort operates step by step.
Insertion sort follows a structured, iterative process where each element is positioned into its correct place within a growing sorted portion of the array. The algorithm progresses sequentially, ensuring that the left side of the array remains sorted after every iteration.
Here is how insertion sort processes the array [5, 3, 4, 1, 2]:
| Pass | Array State | Action Taken |
|---|---|---|
| Initial | [5, 3, 4, 1, 2] | Start, first element considered sorted |
| Pass 1 | [3, 5, 4, 1, 2] | 3 inserted before 5 |
| Pass 2 | [3, 4, 5, 1, 2] | 4 inserted between 3 and 5 |
| Pass 3 | [1, 3, 4, 5, 2] | 1 shifted to front |
| Pass 4 | [1, 2, 3, 4, 5] | 2 inserted between 1 and 3 |
A correct implementation of insertion sort depends on following a well-defined control flow. The algorithm is designed to iterate through the array while maintaining a sorted segment and repositioning elements through controlled shifting rather than swapping.
To write the insertion sort code, you need a precise sequence of instructions. The formal steps of the insertion sort algorithm are structured to iterate over the array while shifting elements efficiently:
Pseudocode provides a language-independent representation of the algorithm, allowing you to focus on logic and control flow without worrying about syntax. It is particularly useful for interviews and for translating the algorithm into any programming language.
Here is the language-agnostic pseudocode for insertion sort.
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i - 1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
return arr
Insertion sort follows a consistent algorithmic pattern across all programming languages. The implementation involves iterating through the array, selecting each element, and placing it in its correct position within the sorted portion of the array. While syntax varies between languages, the underlying logic and control flow remain the same.
Discussed in detail in the sections below are how insertion sort is written in Python, Java, and C++, providing a clear reference for practical usage.
Here is a clean insertion sort implementation that Python developers can use directly in their projects. Understanding insertion sort implementation in Python provides a solid foundation for algorithmic thinking.
public class InsertionSort {
public static void sort(int[] arr) {
int n = arr.length;
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
}
Writing an insertion sort in a Python program is straightforward because of its readable syntax. Any basic insertion sort in Python program follows the same structure. Using insertion sort in Python is particularly useful for small datasets. Mastering insertion sort in Python will help you pass initial coding screens.
This is the standard insertion sort code for Java developers looking for an efficient array-sorting method.
public class InsertionSort {
public static void sort(int[] arr) {
int n = arr.length;
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
}
If you are working with performance-critical systems, here is an optimized C++ insertion sort template.
#include <iostream>
#include <vector>
void insertionSort(std::vector<int>& arr) {
int n = arr.size();
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
Using this C++ insertion sort ensures fast, in-place processing with minimal memory overhead.
The complexity of insertion sort is essential for evaluating when it is an appropriate choice. The algorithm’s performance varies significantly depending on the initial order of the input data, which makes it a common topic in technical interviews and algorithm analysis.
The time and space complexity of insertion sort across all cases is:
| Case | Time Complexity | When It Occurs |
|---|---|---|
| Best case | O(n) | When the array is already sorted |
| Average case | O(n squared) | When the array elements are in random order |
| Worst case | O(n squared) | When the array is sorted in reverse order |
| Space complexity | O(1) | Always, since it sorts in-place |
The best case of O(n) occurs because the algorithm only makes one comparison per element and no swaps when the data is already sorted. The worst case of O(n squared) happens when the data is in reverse order, forcing the algorithm to scan and shift every preceding element for every single new item. The average case also results in O(n squared) because, probabilistically, each item requires shifting half of the already-sorted elements.
When asked about insertion sort complexity, interviewers do not just want the values – they want to know if you can explain why the best case is O(n) and why the worst case is O(n squared).
Yes, insertion sort is stable because it does not swap the relative order of equal elements. It is also an in-place algorithm because it only requires a constant O(1) amount of extra memory space to hold the “key” variable.
Understanding how insertion sort compares with other basic algorithms helps you choose the right approach based on data size, structure, and performance constraints. While all three, including insertion sort, bubble sort, and selection sort, are easy to code, their practical efficiency differs significantly.
Insertion sort is generally more efficient for small or nearly sorted datasets, as it minimizes unnecessary comparisons and shifts. It is preferred in practice due to better real-world performance compared to bubble sort and selection sort. Below is the detailed comparison of insertion sort, bubble sort, and selection sort:
| Algorithm | Best Case | Average Case | Worst Case | Space | Stable? | When to Use |
|---|---|---|---|---|---|---|
| Insertion Sort | O(n) | O(n squared) | O(n squared) | O(1) | Yes | Small arrays or nearly sorted data |
| Bubble Sort | O(n) | O(n squared) | O(n squared) | O(1) | Yes | Rarely – insertion sort is almost always preferred |
| Selection Sort | O(n squared) | O(n squared) | O(n squared) | O(1) | No | When write operations are expensive |
If you want to dive deeper into how these performance metrics stack up against more advanced methods, review the time complexities of all sorting algorithms.
The right sorting algorithm depends on input size, data distribution, and system constraints. Insertion sort is not designed for scalability, but it delivers strong performance in controlled scenarios where simplicity, low overhead, and adaptability to existing order provide a measurable advantage.
Understanding the ideal use cases will help you make better architectural decisions. Use insertion sort when:
Avoid insertion sort when:
Before implementing insertion sort, it is important to evaluate its trade-offs in practical scenarios. While the algorithm is simple and efficient under certain conditions, its limitations become evident as data size and disorder increase.
Before implementing this algorithm, consider these core pros and cons to see if it fits your project needs.
Advantages-
Disadvantages-
Insertion sort remains the optimal choice for small datasets, nearly sorted arrays, and online sorting scenarios. To master this and other fundamental algorithms, you should practice mapping out step-by-step dry runs and comparing time complexities. If you are preparing for top-tier technical interviews and need expert guidance, check out our FAANG interview preparation program.
Insertion sort and selection sort differ in how they build the sorted portion of the array. Insertion sort shifts elements to dynamically insert a new item and is highly adaptive (faster on nearly sorted data), whereas selection sort statically searches for the absolute minimum value and swaps it, never adapting to pre-sorted data.
Binary insertion sort uses binary search to find the correct insertion position instead of doing a linear backward scan. This reduces the number of comparisons, but because the array elements still need to be shifted one by one, the overall time complexity remains O(n squared).
Yes, insertion sort is generally preferred over bubble sort. It usually requires fewer write operations (swaps) in the average case and stops scanning early if the element is in the correct place, whereas bubble sort blindly compares adjacent elements.
Insertion sort is not better than quicksort for large datasets because it has a time complexity of O(n²), while quicksort typically runs in O(n log n). However, insertion sort can outperform quicksort for very small arrays or nearly sorted data due to its low overhead and adaptive nature. This is why many optimized sorting algorithms switch to insertion sort for small partitions.
Insertion sort is called adaptive because its performance improves when the input data is already partially sorted. In such cases, it performs fewer comparisons and shifts, achieving a time complexity close to O(n). This ability to take advantage of existing order makes it efficient for real-world scenarios where data is often not completely random.
Recommended Reads:
Time Zone:
100% Free — No credit card needed.
Time Zone:
Master ML interviews with DSA, ML System Design, Supervised/Unsupervised Learning, DL, and FAANG-level interview prep.
Get strategies to ace TPM interviews with training in program planning, execution, reporting, and behavioral frameworks.
Course covering SQL, ETL pipelines, data modeling, scalable systems, and FAANG interview prep to land top DE roles.
Course covering Embedded C, microcontrollers, system design, and debugging to crack FAANG-level Embedded SWE interviews.
Nail FAANG+ Engineering Management interviews with focused training for leadership, Scalable System Design, and coding.
End-to-end prep program to master FAANG-level SQL, statistics, ML, A/B testing, DL, and FAANG-level DS interviews.
Learn to build AI agents to automate your repetitive workflows
Upskill yourself with AI and Machine learning skills
Prepare for the toughest interviews with FAANG+ mentorship
Time Zone:
Join 25,000+ tech professionals who’ve accelerated their careers with cutting-edge AI skills
25,000+ Professionals Trained
₹23 LPA Average Hike 60% Average Hike
600+ MAANG+ Instructors
Webinar Slot Blocked
Register for our webinar
Learn about hiring processes, interview strategies. Find the best course for you.
ⓘ Used to send reminder for webinar
Time Zone: Asia/Kolkata
Time Zone: Asia/Kolkata
Hands-on AI/ML learning + interview prep to help you win
Explore your personalized path to AI/ML/Gen AI success
The 11 Neural “Power Patterns” For Solving Any FAANG Interview Problem 12.5X Faster Than 99.8% OF Applicants
The 2 “Magic Questions” That Reveal Whether You’re Good Enough To Receive A Lucrative Big Tech Offer
The “Instant Income Multiplier” That 2-3X’s Your Current Tech Salary
Join 25,000+ tech professionals who’ve accelerated their careers with cutting-edge AI skills
Join 25,000+ tech professionals who’ve accelerated their careers with cutting-edge AI skills
Webinar Slot Blocked
Time Zone: Asia/Kolkata
Hands-on AI/ML learning + interview prep to help you win
Time Zone: Asia/Kolkata
Hands-on AI/ML learning + interview prep to help you win
Explore your personalized path to AI/ML/Gen AI success
See you there!