07 September, 2012

Insertion Sort

import java.util.*;
class insertionsort
{

public static void main(String[] args)
{
int []a = {5,25,12,28,9,12,15,13,10,7} ;
insertionsort(a);
for(int i=0;i<=a.length-1;i++)
    System.out.println(a[i]);
}

public static void insertionsort(int[] a)
{
 for(int j=1;j<a.length;j++)
 {
  int key=a[j];
  int i;
  for(i=j-1;i>=0&&a[i]>key;i--)
     a[i+1]=a[i];
  a[i+1]=key;
 }
}

}

No comments:

Post a Comment

Thank You !