import java.io.*;
class Insertion1
{
public static void main ( String args[] )
throws IOException
{
BufferedReader br = new BufferedReader ( new InputStreamReader ( System.in ) );
System.out.print("\n\nEnter size of array : ");
int n = Integer.parseInt ( br.readLine() );
int a[] = new int[n];
System.out.println("\n\nEnter array elements -\n\n");
for ( int i=0 ; i<n ; i++ )
{
System.out.print("a[" + i + "] = ");
a[i] = Integer.parseInt ( br.readLine() );
System.out.println();
}
for ( int j=0 ; j<n ; j++ )
{
int key = a[j];
int i = j - 1;
while ( i >= 0 && a[i] > key )
{
a[i+1] = a[i];
i--;
}
a[i+1] = key;
}
System.out.print("\nSorted Array : ");
for ( int i=0 ; i<n ; i++ )
System.out.print( a[i] + " " );
System.out.println("\n");
}
}
No comments:
Post a Comment