0% found this document useful (0 votes)
4 views

Java Insertion Sorting

Uploaded by

fojimaw327
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java Insertion Sorting

Uploaded by

fojimaw327
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.util.

Scanner;
public class freshers
{
public static void main(String[] args)
{
int i,j,k,temp;
Scanner sc=new Scanner(System.in);
System.out.println("enter the number of elements to be sorted");
int n=sc.nextInt();
int a[]=new int[n];
System.out.println("enter the elements to be sorted");
for(k=0;k<n;k++)
{
int num=sc.nextInt();
a[k]=num;
}
System.out.println("the elements before sorting are:");
for(int value: a)
{
System.out.println(value+" ");
}
for(i=1;i<n;i++)
{
temp=a[i];
j=i-1;
while((j>=0)&&(a[j]>temp))
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
System.out.println();
System.out.println("the elements after sorting are:");
for(int value: a)
{
System.out.println(value+" ");
}
}
}

You might also like