/* related posts with thumb nails */

Program to implement Insertion Sort.:

import java.io.*;

class InsSort

{

public static void main(String as[]) throws Exception

{

int i,j,n,t,a[]=new int[10];

InputStreamReader isr=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(isr);

System.out.println("How many values:");

n=Integer.parseInt(br.readLine());

System.out.println("Enter Values:");

for(i=0;i

a[i]=Integer.parseInt(br.readLine());

for(i=1;i

{

t=a[i];

for(j=i;j>0;j--)

{

if(a[j-1]>t)

a[j]=a[j-1];

else

break;

}

a[j]=t;

}

System.out.println("After Sorting");

for(i=0;i

System.out.print("\t"+a[i]);

}

}

Output:

How many values: 4

Enter Values:

45

36

87

12

After Sorting:

12 36 45 87

Related Topics:

0 comments:

Post a Comment