Insertion Sort Algorithm

Last updated by Vartika Rai on May 2, 2026 at 07:37 PM

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.

| Reading Time: 3 minutes

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.

Key Takeaways

  • Understand that insertion sort is a simple, comparison-based sorting algorithm that builds a sorted array incrementally by inserting elements into their correct position.
  • Learn that the algorithm works by maintaining a sorted and unsorted portion, shifting elements instead of swapping to place each item correctly.
  • Recognize that insertion sort performs efficiently with a best-case time complexity of O(n) when the data is already or nearly sorted.
  • Understand that its average and worst-case time complexity is O(n²), making it unsuitable for large or highly unsorted datasets.
  • Discover that insertion sort is both stable (preserves order of equal elements) and in-place (requires O(1) extra space), making it memory-efficient.

What Is Insertion Sort?

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.

Q1. What is Insertion Sort?

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.

How Insertion Sort Works?

Understanding how insertion sort works is critical for mastering the algorithm. Here is the conceptual process of how insertion sort operates step by step.

How Insertion Sort Works

Q2. How does insertion sort work 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.

  • Start by treating the first element of the array as the already sorted portion.
  • Pick the next element from the unsorted portion to act as the “key.”
  • Compare this key to the elements in the sorted portion, moving from right to left.
  • Shift every element that is strictly greater than the key one position to the right.
  • Insert the key into the newly opened space and repeat until the entire array is sorted.

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

Insertion Sort Algorithm

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.

Q3. What are the steps of the insertion sort algorithm?

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:

  • Start at index 1 (the second element). Index 0 is treated as an already-sorted sub-array.
  • Store the current element as key.
  • Set j = i – 1 (the last index of the sorted portion).
  • While j >= 0 and arr[j] > key: move arr[j] to arr[j+1] and decrement j.
  • Place key at arr[j+1]. Increment i and repeat.

Insertion Sort Pseudocode

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.

Q4. What is the pseudocode for insertion sort?

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 Code

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.

Q5. How do you implement insertion sort in Python?

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.

Q6. How do you implement insertion sort in Java?

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;
        }
    }
}

Q7. How do you implement insertion sort in C++?

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.

Insertion Sort Time and Space Complexity

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.

Q8. What is the time complexity of insertion sort?

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).

Q9. Is insertion sort stable and in-place?

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.

Insertion Sort vs Other Sorting Algorithms

Insertion Sort vs Other Sorting Algorithms

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.

Q10. How does insertion sort compare to bubble sort and selection sort?

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.

When to Use Insertion Sort?

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.

Q11. When should you use insertion sort?

Understanding the ideal use cases will help you make better architectural decisions. Use insertion sort when:

  • The array is small: The low overhead of the algorithm makes it faster than complex algorithms like QuickSort for arrays smaller than 10-20 elements.
  • Data is nearly sorted: It operates in O(n) time when only a few elements are out of place.
  • Memory is severely restricted: The O(1) space complexity is ideal for embedded systems.
  • Data arrives continuously: It can sort a list as it receives it live (online sorting).

Avoid insertion sort when:

  • The dataset is large: The O(n squared) complexity makes it incredibly slow for massive databases.
  • Data is in reverse order: It triggers the worst-case scenario, maximizing the number of element shifts.
  • Performance is critical: O(n log n) algorithms like Merge Sort or QuickSort are vastly superior for general large-scale sorting.

Advantages and Disadvantages of Insertion Sort

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.

Q12. What are the advantages and disadvantages of insertion sort?

Before implementing this algorithm, consider these core pros and cons to see if it fits your project needs.

Advantages-

  • Extremely simple to implement and understand.
  • Highly efficient for small or partially sorted datasets.
  • Requires no additional memory (in-place sorting).
  • Maintains the relative order of equal elements (stable sorting).

Disadvantages-

  • Highly inefficient for large datasets.
  • Scales poorly due to its quadratic O(n squared) time complexity.
  • Performance severely degrades if the array is reverse-sorted.
  • Requires numerous write operations (shifting elements), which can be costly in certain memory architectures.

Conclusion

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.

FAQs – Insertion Sort

Q1. What is the difference between insertion sort and selection sort?

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.

Q2. What is binary insertion sort?

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).

Q3. Is insertion sort better than bubble sort?

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.

Q4. Is insertion sort better than quicksort?

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.

Q5. Why is insertion sort considered an adaptive algorithm?

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.

References

  1. Sorting algorithm
  2. Timsort
  3. Benchmarks from Rosetta Code’s comparative sorting
  4. Bidirectional Conditional Insertion Sort algorithm; An efficient progress on the classical insertion sort

Recommended Reads:

Last updated on: May 2, 2026
Register for our webinar

Uplevel your career with AI/ML/GenAI

Loading_icon
Loading...
1 Enter details
2 Select webinar slot
By sharing your contact details, you agree to our privacy policy.

Select a Date

Time slots

Time Zone:

Strange Tier-1 Neural “Power Patterns” Used By 20,013 FAANG Engineers To Ace Big Tech Interviews

100% Free — No credit card needed.

Register for our webinar

Uplevel your career with AI/ML/GenAI

Loading_icon
Loading...
1 Enter details
2 Select webinar slot
By sharing your contact details, you agree to our privacy policy.

Select a Date

Time slots

Time Zone:

IK courses Recommended

Master ML interviews with DSA, ML System Design, Supervised/Unsupervised Learning, DL, and FAANG-level interview prep.

Fast filling course!

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.

Select a course based on your goals

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

Register for our webinar

How to Nail your next Technical Interview

Loading_icon
Loading...
1 Enter details
2 Select slot
By sharing your contact details, you agree to our privacy policy.

Select a Date

Time slots

Time Zone:

Almost there...
Share your details for a personalised FAANG career consultation!
Your preferred slot for consultation * Required
Get your Resume reviewed * Max size: 4MB
Only the top 2% make it—get your resume FAANG-ready!

Registration completed!

🗓️ Friday, 18th April, 6 PM

Your Webinar slot

Mornings, 8-10 AM

Our Program Advisor will call you at this time

Register for our webinar

Transform Your Tech Career with AI Excellence

Transform Your Tech Career with AI Excellence

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

Interview Kickstart Logo

Register for our webinar

Transform your tech career

Transform your tech career

Learn about hiring processes, interview strategies. Find the best course for you.

Loading_icon
Loading...
*Invalid Phone Number

Used to send reminder for webinar

By sharing your contact details, you agree to our privacy policy.
Choose a slot

Time Zone: Asia/Kolkata

Choose a slot

Time Zone: Asia/Kolkata

Build AI/ML Skills & Interview Readiness to Become a Top 1% Tech Pro

Hands-on AI/ML learning + interview prep to help you win

Switch to ML: Become an ML-powered Tech Pro

Explore your personalized path to AI/ML/Gen AI success

Your preferred slot for consultation * Required
Get your Resume reviewed * Max size: 4MB
Only the top 2% make it—get your resume FAANG-ready!
Registration completed!
🗓️ Friday, 18th April, 6 PM
Your Webinar slot
Mornings, 8-10 AM
Our Program Advisor will call you at this time

Get tech interview-ready to navigate a tough job market

Best suitable for: Software Professionals with 5+ years of exprerience
Register for our FREE Webinar

Next webinar starts in

00
DAYS
:
00
HR
:
00
MINS
:
00
SEC

Your PDF Is One Step Away!

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

Transform Your Tech Career with AI Excellence

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

Loading_icon
Loading...
*Invalid Phone Number
By sharing your contact details, you agree to our privacy policy.
Choose a slot

Time Zone: Asia/Kolkata

Build AI/ML Skills & Interview Readiness to Become a Top 1% Tech Pro

Hands-on AI/ML learning + interview prep to help you win

Choose a slot

Time Zone: Asia/Kolkata

Build AI/ML Skills & Interview Readiness to Become a Top 1% Tech Pro

Hands-on AI/ML learning + interview prep to help you win

Switch to ML: Become an ML-powered Tech Pro

Explore your personalized path to AI/ML/Gen AI success

Registration completed!

See you there!

Webinar on Friday, 18th April | 6 PM
Webinar details have been sent to your email
Mornings, 8-10 AM
Our Program Advisor will call you at this time