import java.io.*;
class insertionsort
{
public static void main(String args[])
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n=0,i,j,t;
int a[] = new int[10];
System.out.println("Enter size of the array:");
try
{
n= Integer.parseInt(br.readLine());
}
catch(Exception e) { }
System.out.println("Enter elements into the array:");
try
{
for(i=0;i<n;i++)
a[i] = Integer.parseInt(br.readLine());
}
catch(Exception e) { }
for(i=0;i<n-1;i++)
{
for(j=i+1;j>0;j--)
{
if(a[j]<a[j-1])
{
t=a[j];
a[j]=a[j-1];
a[j-1]=t;
}
}
}
System.out.println("The sorted array is :");
for(i=0;i<n;i++)
System.out.println(a[i]);
}
}
class insertionsort
{
public static void main(String args[])
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n=0,i,j,t;
int a[] = new int[10];
System.out.println("Enter size of the array:");
try
{
n= Integer.parseInt(br.readLine());
}
catch(Exception e) { }
System.out.println("Enter elements into the array:");
try
{
for(i=0;i<n;i++)
a[i] = Integer.parseInt(br.readLine());
}
catch(Exception e) { }
for(i=0;i<n-1;i++)
{
for(j=i+1;j>0;j--)
{
if(a[j]<a[j-1])
{
t=a[j];
a[j]=a[j-1];
a[j-1]=t;
}
}
}
System.out.println("The sorted array is :");
for(i=0;i<n;i++)
System.out.println(a[i]);
}
}
No comments:
Post a Comment