Insertion Sort Algorithm (original) (raw)

Last Updated : 24 Feb, 2026

Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. Then, you pick a card from the unsorted group and put it in the right place in the sorted group.

Try It Yourselfredirect icon

C++ `

// C++ program for implementation of Insertion Sort #include using namespace std;

/* Function to sort array using insertion sort */ void insertionSort(int arr[], int n) { 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 ahead
       of their current position */
    while (j >= 0 && arr[j] > key) {
        arr[j + 1] = arr[j];
        j = j - 1;
    }
    arr[j + 1] = key;
}

}

/* A utility function to print array of size n */ void printArray(int arr[], int n) { for (int i = 0; i < n; ++i) cout << arr[i] << " "; cout << endl; }

// Driver method int main() { int arr[] = { 12, 11, 13, 5, 6 }; int n = sizeof(arr) / sizeof(arr[0]);

insertionSort(arr, n);
printArray(arr, n);

return 0;

}

C

// C program for implementation of Insertion Sort #include <stdio.h>

/* Function to sort array using insertion sort */ void insertionSort(int arr[], int n) { 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 ahead
       of their current position */
    while (j >= 0 && arr[j] > key) {
        arr[j + 1] = arr[j];
        j = j - 1;
    }
    arr[j + 1] = key;
}

}

/* A utility function to print array of size n */ void printArray(int arr[], int n) { for (int i = 0; i < n; ++i) printf("%d ", arr[i]); printf("\n"); }

// Driver method int main() { int arr[] = { 12, 11, 13, 5, 6 }; int n = sizeof(arr) / sizeof(arr[0]);

insertionSort(arr, n);
printArray(arr, n);

return 0;

}

Java

// Java program for implementation of Insertion Sort public class InsertionSort { /* Function to sort array using insertion sort */ void sort(int arr[]) { int n = arr.length; 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 ahead
           of their current position */
        while (j >= 0 && arr[j] > key) {
            arr[j + 1] = arr[j];
            j = j - 1;
        }
        arr[j + 1] = key;
    }
}

/* A utility function to print array of size n */
static void printArray(int arr[])
{
    int n = arr.length;
    for (int i = 0; i < n; ++i)
        System.out.print(arr[i] + " ");

    System.out.println();
}

// Driver method
public static void main(String args[])
{
    int arr[] = { 12, 11, 13, 5, 6 };

    InsertionSort ob = new InsertionSort();
    ob.sort(arr);

    printArray(arr);
}

}

Python

Python program for implementation of Insertion Sort

Function to sort array using insertion sort

def insertionSort(arr): for i in range(1, len(arr)): key = arr[i] j = i - 1

    # Move elements of arr[0..i-1], that are
    # greater than key, to one position ahead
    # of their current position
    while j >= 0 and key < arr[j]:
        arr[j + 1] = arr[j]
        j -= 1
    arr[j + 1] = key

A utility function to print array of size n

def printArray(arr): for i in range(len(arr)): print(arr[i], end=" ") print()

Driver method

if name == "main": arr = [12, 11, 13, 5, 6] insertionSort(arr) printArray(arr)

C#

// C# program for implementation of Insertion Sort using System;

class InsertionSort { /* Function to sort array using insertion sort */ void sort(int[] arr) { int n = arr.Length; 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 ahead
           of their current position */
        while (j >= 0 && arr[j] > key) {
            arr[j + 1] = arr[j];
            j = j - 1;
        }
        arr[j + 1] = key;
    }
}

/* A utility function to print array of size n */
static void printArray(int[] arr) {
    int n = arr.Length;
    for (int i = 0; i < n; ++i)
        Console.Write(arr[i] + " ");

    Console.WriteLine();
}

// Driver method
public static void Main() {
    int[] arr = { 12, 11, 13, 5, 6 };

    InsertionSort ob = new InsertionSort();
    ob.sort(arr);

    printArray(arr);
}

}

JavaScript

// Javascript program for insertion sort

// Function to sort array using insertion sort function insertionSort(arr) { for (let i = 1; i < arr.length; i++) { let key = arr[i]; let j = i - 1;

    /* Move elements of arr[0..i-1], that are
       greater than key, to one position ahead
       of their current position */
    while (j >= 0 && arr[j] > key) {
        arr[j + 1] = arr[j];
        j = j - 1;
    }
    arr[j + 1] = key;
}

}

// A utility function to print array of size n function printArray(arr) { console.log(arr.join(" ")); }

// Driver method let arr = [12, 11, 13, 5, 6];

insertionSort(arr); printArray(arr);

PHP

i<i < i<n; $i++) { key=key = key=arr[$i]; j=j = j=i - 1; // Move elements of arr[0..i-1], // that are greater than key, to // one position ahead of their // current position while ($j >= 0 && arr[arr[arr[j] > $key) { arr[arr[arr[j + 1] = arr[arr[arr[j]; j=j = j=j - 1; } arr[arr[arr[j + 1] = $key; } } // A utility function to print an array of size n function printArray(&$arr, $n) { for ($i = 0; i<i < i<n; $i++) echo arr[arr[arr[i] . " "; echo "\n"; } // Driver Code $arr = array(12, 11, 13, 5, 6); n=sizeof(n = sizeof(n=sizeof(arr); insertionSort($arr, $n); printArray($arr, $n); ?>

`

**Illustration

Insertion-sorting

**arr = {23, 1, 10, 5, 2}

**Initial:

First Pass:

Second Pass:

Third Pass:

Fourth Pass:

Final Array:

Complexity Analysis of Insertion Sort

Time Complexity

Space Complexity

Please refer Complexity Analysis of Insertion Sort for details.

**Advantages and Disadvantages of Insertion Sort

**Advantages

Disadvantages

Applications **of Insertion Sort

Insertion sort is commonly used in situations where: