Insertion Sort | Write a program in java to perform insertion sort on an array. | ISC Computer Science | ICSE Computer Applications


Insertion Sort in Java

Algorithm

  1.   Start
  2. a:=null
  3. i:=0
  4. n:=0
  5. j:=0
  6. temp:=0
  7. Print "Enter the size of the array"
  8. Read n
  9. a=new int[n]
  10. i=0
  11. If i<n, go to step 12 or else go to step 15
  12. Print "Enter a number"
  13. Read a[i]
  14. i++. Go to step 11
  15. i=0
  16. If i<n, go to step 17 or else go to step 25
  17. temp=a[i]
  18. j=i-1
  19. If j>=0 && a[j]>temp, go to step 20 or else go to step 23
  20. a[j+1]=[j]
  21. j=j-1
  22. Go to step 19
  23. a[j+1]=temp
  24. i++. Go to step 16
  25. i=0
  26. If i<n, go to step 27 or else go to step 29
  27. Print a[i];
  28. i++. Go to step 26
  29. Stop

Program

Buffered Reader

import java.io.*;
class SortArray
{
    int a[], i, n, j, temp;
    
    void accept() throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader (System.in));
        System.out.println("Enter the size of the array");
        n=Integer.parseInt(br.readLine());
        a=new int[n];
        for(i=0;i<n;i++)
        {
            System.out.println("Enter a number");
            a[i]=Integer.parseInt(br.readLine());
        }
    }
    
    void sort()
    {
        for(i=0;i<n;i++)
        {
            temp=a[i];
            j=i-1;
            while(j>=0&&a[j]>temp)
            {
                a[j+1]=a[j];
                j=j-1;
            }
            a[j+1]=temp;
        }
    }
    
    void display()
    {
        for(i=0;i<n;i++)
        {
            System.out.println(a[i]);
        }
    }
    
    void main() throws IOException
    {
        accept();
        sort();
        display();
    }
}

Scanner

import java.util.*;
class SortArray
{
    int a[], i, n, j, temp;
    
    void accept() 
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the size of the array");
        n=sc.nextInt();
        a=new int[n];
        for(i=0;i<n;i++)
        {
            System.out.println("Enter a number");
            a[i]=sc.nextInt();
        }
    }
    
    void sort()
    {
        for(i=0;i<n;i++)
        {
            temp=a[i];
            j=i-1;
            while(j>=0&&a[j]>temp)
            {
                a[j+1]=a[j];
                j=j-1;
            }
            a[j+1]=temp;
        }
    }
    
    void display()
    {
        for(i=0;i<n;i++)
        {
            System.out.println(a[i]);
        }
    }
    
    void main() 
    {
        accept();
        sort();
        display();
    }
}

Documentation

Variable Description

 Data TypeVariable NameDescription
 int[]To store an array of numbers 
 intTo store the size of the array 
 intTo act as  counter variable 
 intTo act as a counter variable 
 inttemp To act as a temp variable 

Method Description

 Return TypeMethod Name Description
 voidaccept() To accept data from the user 
 voidsort() To sort data in the array using insertion sort 
 voiddisplay() To display the array 
 voidmain() To act as an entry gateway for the compiler 

Output


Insertion Sort Output





Share with all ISC Computer Science Students you know.

Comment below with all your program requests and blog suggestions.

Follow us on social media by clicking on these links. 
Instagram : @ISCJavaCode
Twitter: @ISCJavaCode
Reddit: @ISCJavaCode
Pinterest: @ISCJavaCode
Facebook: @ISCJavaCode
LinkedIn : ISC Java Code 


Stay Healthy
Stay Happy
Stay Helpful


Comments

You might also like:

Conversion of Complex Sentences to Simple Sentences

Emirp Number | Write a program in Java to print an Emirp Number. | ISC Computer Science | ICSE Computer Applications

Conversion of Complex Sentences to Compound Sentences