Insertion Sort

Insertion sort is a simple sorting algorithm that works similar to the way you sort playing cards in your hands. The array is virtually split into a sorted and an unsorted part. Values from the unsorted part are picked and placed at the correct position in the sorted part.

Time Complexity:

1)The best-case time complexity of insertion sort is O(n).
2)The average case time complexity of insertion sort is O(n2).
3)The worst-case time complexity of insertion sort is O(n2).

Space Complexity:

The space complexity of insertion sort is O(1).

Java

python

JavaScript

c

                 
                   
                        // Javascript program for insertion sort
                        
                        // Function to sort an array using insertion sort
                        function insertionSort(arr, n)
                        {
                            let i, key, j;
                            for (i = 1; i < n; i++)
                            {
                                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 && arr[j] > key)
                                {
                                    arr[j + 1] = arr[j];
                                    j = j - 1;
                                }
                                arr[j + 1] = key;
                            }
                        }
                        
                        // A utility function to print an array of size n
                        function printArray(arr, n)
                        {
                            let i;
                            for (i = 0; i < n; i++)
                                document.write(arr[i] + " ");
                            document.write("
"); } // Driver code let arr = [12, 11, 13, 5, 6 ]; let n = arr.length; insertionSort(arr, n); printArray(arr, n);
// 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 program for implementation of Insertion Sort # Function to do insertion sort def insertionSort(arr): # Traverse through 1 to len(arr) for i in range(1, len(arr)): key = arr[i] # Move elements of arr[0..i-1], that are # greater than key, to one position ahead # of their current position j = i-1 while j >= 0 and key < arr[j] : arr[j + 1] = arr[j] j -= 1 arr[j + 1] = key # Driver code to test above arr = [12, 11, 13, 5, 6] insertionSort(arr) for i in range(len(arr)): print ("% d" % arr[i]) // C program for insertion sort #include #include /* Function to sort an array using insertion sort*/ void insertionSort(int arr[], int n) { int i, key, j; for (i = 1; i < n; i++) { 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 && arr[j] > key) { arr[j + 1] = arr[j]; j = j - 1; } arr[j + 1] = key; } } // A utility function to print an array of size n void printArray(int arr[], int n) { int i; for (i = 0; i < n; i++) printf("%d ", arr[i]); printf("\n"); } /* Driver program to test insertion sort */ int main() { int arr[] = { 12, 11, 13, 5, 6 }; int n = sizeof(arr) / sizeof(arr[0]); insertionSort(arr, n); printArray(arr, n); return 0; }