Insertion Sort Program in Python | Java | C | C++

Insertion sort is not the best sorting algorithm in terms of performance, but it’s a little more efficient than selection sort and bubble sort in practical scenarios. Insertion sort is also a pretty intuitive sorting technique.

Insertion sort Program in C

Solution In C


Code:

#include<stdio.h>
int main()
{
    int size;
    printf("Enter the size of the array:");
    scanf("%d",&size);
    int arr[size],i;
    printf("Enter the element of the array:");
    for(i=0;i<size;i++)
        scanf("%d",&arr[i]);

    printf("Before Sorting Array Element are: ");
    for(i=0;i<size;i++)
        printf("%d ",arr[i]);

    int in, out;
    for(out=1; out<size; out++)
        {
            int temp = arr[out];
            in = out;
            while(in>0 && arr[in-1] >= temp)
                {
                    arr[in] = arr[in-1];
                    --in;
                    }
                    arr[in] = temp;
                    }

        printf("\nAfter Sorting Array Element are: ");
        for(i=0;i<size;i++)
        printf("%d ",arr[i]);
}

Input/Output:
Enter the size of the array:5
Enter the element of the array:
4
3
7
8
9
Before Sorting Array Elements are: 4 3 7 8 9
After Sorting Array Elements are: 3 4 7 8 9

Insertion sort Program in C plus plus

Solution In C++


Code:

#include<iostream>
using namespace std;
int main()
{
    int size;
    cout<<"Enter the size of the array:";
    cin>>size;
    int arr[size],i;
    cout<<"Enter the element of the array:";
    for(i=0;i<size;i++)
        cin>>arr[i];

    cout<<"Before Sorting Array Element are: ";
    for(i=0;i<size;i++)
        cout<<arr[i]<<" ";

   int in, out;
    for(out=1; out<size; out++)
        {
            int temp = arr[out];
            in = out;
            while(in>0 && arr[in-1] >= temp)
                {
                    arr[in] = arr[in-1];
                    --in;
                    }
                    arr[in] = temp;
                    }

        cout<<"\nAfter Sorting Array Element are: ";
        for(i=0;i<size;i++)
        cout<<arr[i]<<" ";
}

Input/Output:
Enter the size of the array:4
Enter the element of the array:
7
9
3
5
Before Sorting Array Elements are: 7 9 3 5
After Sorting Array Elements are: 5 3 7 9

Insertion sort Program in Java


Solution In Java

Code:


import java.util.Scanner;
public class Insertion_Sort {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int size;
    System.out.print("Enter the size of the array:");
    size=cs.nextInt();
    int arr[]=new int[size],i;
    System.out.print("Enter the element of the array:");
    for(i=0;i<size;i++)
        arr[i]=cs.nextInt();

    System.out.print("Before Sorting Array Element are: ");
    for(i=0;i<size;i++)
        System.out.print(arr[i]+" ");

    int in, out;
    for(out=1; out<size; out++)
        {
            int temp = arr[out];
            in = out;
            while(in>0 && arr[in-1] >= temp)
                {
                    arr[in] = arr[in-1];
                    --in;
                    }
                    arr[in] = temp;
                    }

        System.out.print("\nAfter Sorting Array Element are: ");
        for(i=0;i<size;i++)
        System.out.print(arr[i]+" ");
cs.close();
}
}


Input/Output:
Enter the size of the array:4
Enter the element of the array:3
2
2
6
Before Sorting Array Elements are: 3 2 2 6 
After Sorting Array Elements are: 2 2 3 6 

Insertion sort Program in python using list


Solution In Python

size=int(input("Enter the size of the array:"));
arr=[]
print("Enter the element of the array:");
for i in range(0,size):
    num = int(input())
    arr.append(num)

print("Before Sorting Array Element are: ",arr)


for out in range(1,size-1):
    temp = arr[out]
    inn=out
    while inn > 0 and arr[inn -1] >= temp:
        arr[inn] = arr[inn -1]
        inn-=1
    arr[inn] = temp

print("\nAfter Sorting Array Element are: ",arr)


Input/Output:
Enter the size of the array:5
Enter the element of the array:
4
3
6
7
4
Before Sorting Array Element are:  [4, 3, 6, 7, 4]

After Sorting Array Element are:  [3, 4, 4, 6, 7]



Post a Comment

1 Comments

  1. In python, in the sorting section.. there is an error.
    You wrote

    for out in range(1,size-1):

    The array is zero_indexed so the the array have to traversed from 0 to size-1 or 1 to size

    for out in range(0,size-1):

    ReplyDelete

Please do not Enter any spam link in the comment box