Insertion sort
public class InsertionSort
{
public static void main(String args[])
{
int arr[] = {4, 5, 1, 9, 3, 2};
int l, i, key;
l= arr.length;
for(i=1;i<l;i++)
{
int key = arr[i];
j = i-1;
while(j>=0 && arr[j]>key)
{
arr[j+1] = arr[j];
j= j-1;
}
arr[j+1] = key;
}
for(i=0;i<l;i++)
{
System.out.print(arr[i] + " ");
}
}
}
Comments
Post a Comment