Insertion Sort
Insertion sort is the simple sorting algorithm which is commonly used in the daily lives while ordering a deck of cards. In this algorithm, we insert each element onto its proper place in the sorted array. This is less efficient than the other sort algorithms like quick sort, merge sort, etc.
Technique
Consider an array A whose elements are to be sorted. Initially, A[0] is the only element on the sorted set. In pass 1, A[1] is placed at its proper index in the array.
In pass 2, A[2] is placed at its proper index in the array. Likewise, in pass n-1, A[n-1] is placed at its proper index into the array.
To insert an element A[k] to its proper index, we must compare it with all other elements i.e. A[k-1], A[k-2], and so on until we find an element A[j] such that, A[j]<=A[k].
All the elements from A[k-1] to A[j] need to be shifted and A[k] will be moved to A[j+1].
Complexity
Complexity | Best Case | Average Case | Worst Case |
---|---|---|---|
Time | Ω(n) | θ(n2) | o(n2) |
Space | o(1) |
Algorithm
- Step 1: Repeat Steps 2 to 5 for K = 1 to N-1
- Step 2: SET TEMP = ARR[K]
- Step 3: SET J = K ? 1
- Step 4: Repeat while TEMP <=ARR[J]
SET ARR[J + 1] = ARR[J]
SET J = J ? 1
[END OF INNER LOOP] - Step 5: SET ARR[J + 1] = TEMP
[END OF LOOP] - Step 6: EXIT
C Program
- #include<stdio.h>
- void main ()
- {
- int i,j, k,temp;
- int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
- printf("\nprinting sorted elements...\n");
- for(k=1; k<10; k++)
- {
- temp = a[k];
- j= k-1;
- while(j>=0 && temp <= a[j])
- {
- a[j+1] = a[j];
- j = j-1;
- }
- a[j+1] = temp;
- }
- for(i=0;i<10;i++)
- {
- printf("\n%d\n",a[i]);
- }
- }
Output:
Printing Sorted Elements . . . 7 9 10 12 23 23 34 44 78 101
C++ Program
- #include<iostream>
- using namespace std;
- int main ()
- {
- int i,j, k,temp;
- int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
- cout<<"\nprinting sorted elements...\n";
- for(k=1; k<10; k++)
- {
- temp = a[k];
- j= k-1;
- while(j>=0 && temp <= a[j])
- {
- a[j+1] = a[j];
- j = j-1;
- }
- a[j+1] = temp;
- }
- for(i=0;i<10;i++)
- {
- cout <<a[i]<<"\n";
- }
- }
Output:
printing sorted elements... 7 9 10 12 23 23 34 44 78 101
Java Program
- public class InsertionSort {
- public static void main(String[] args) {
- int[] a = {10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
- for(int k=1; k<10; k++)
- {
- int temp = a[k];
- int j= k-1;
- while(j>=0 && temp <= a[j])
- {
- a[j+1] = a[j];
- j = j-1;
- }
- a[j+1] = temp;
- }
- System.out.println("printing sorted elements ...");
- for(int i=0;i<10;i++)
- {
- System.out.println(a[i]);
- }
- }
- }
Output:
Printing sorted elements . . . 7 9 10 12 23 23 34 44 78 101
C# Program
- using System;
- public class Program
- {
- public static void Main()
- {
- int i,j, k,temp;
- int[] a = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
- Console.WriteLine("\nprinting sorted elements...\n");
- for(k=1; k<10; k++)
- {
- temp = a[k];
- j= k-1;
- while(j>=0 && temp <= a[j])
- {
- a[j+1] = a[j];
- j = j-1;
- }
- a[j+1] = temp;
- }
- for(i=0;i<10;i++)
- {
- Console.WriteLine(a[i]);
- }
- }
- }
Output:
printing sorted elements . . . 7 9 10 12 23 23 34 44 78 101
Python Program
- a=[10, 9, 7, 101, 23, 44, 12, 78, 34, 23]
- for k in range(1,10):
- temp = a[k]
- j = k-1
- while j>=0 and temp <=a[j]:
- a[j+1]=a[j]
- j = j-1
- a[j+1] = temp
- print("printing sorted elements...")
- for i in a:
- print(i)
Output:
printing sorted elements . . . 7 9 10 12 23 23 34 44 78 101
Swift Program
- import Foundation
- import Glibc
- var a = [10, 9, 7, 101, 23, 44, 12, 78, 34, 23];
- print("\nprinting sorted elements...\n");
- for k in 1...9
- {
- let temp = a[k];
- var j = k-1;
- while j>=0 && temp <= a[j]
- {
- a[j+1] = a[j];
- j = j-1;
- }
- a[j+1] = temp;
- }
- for i in a
- {
- print(i);
- }
Output:
printing sorted elements... 7 9 10 12 23 23 34 44 78 101
JavaScript Program
- <html>
- <head>
- <title>
- Insertion Sort
- </title>
- </head>
- <body>
- <script>
- var txt = "<br>";
- var a = [10, 9, 7, 101, 23, 44, 12, 78, 34, 23];
- document.writeln("printing sorted elements ... "+txt);
- for(k=0;k<10;k++)
- {
- var temp = a[k]
- j=k-1;
- while (j>=0 && temp <= a[j])
- {
- a[j+1] = a[j];
- jj = j-1;
- }
- a[j+1] = temp;
- }
- for(i=0;i<10;i++)
- {
- document.writeln(a[i]);
- document.writeln(txt);
- }
- </script>
- </body>
- </html>
Output:
printing sorted elements ... 7 9 10 12 23 23 34 44 78 101
PHP Program
- <html>
- <head>
- <title>Insertion Sort</title>
- </head>
- <body>
- <?php
- $a = array(10, 9, 7, 101, 23, 44, 12, 78, 34, 23);
- echo("printing sorted elements ... \n");
- for($k=0;$k<10;$k++)
- {
- $temp = $a[$k];
- $j=$k-1;
- while ($j>=0 && $temp <= $a[$j])
- {
- $a[$j+1] = $a[$j];
- $j = $j-1;
- }
- $a[$j+1] = $temp;
- }
- for($i=0;$i<10;$i++)
- {
- echo $a[$i];
- echo "\n";
- }
- ?>
- </body>
- </html>
Output:
printing sorted elements ... 7 9 10 12 23 23 34 44 78 101