class insertionSort
{
public static void main(String args[])
{
int a[]={2,6,4,8,9};
int i, j, n;
for (i = 1; i < a.length; i++)
{
n = a[i];
j = i;
while (j > 0 && a[j - 1] > n)
{
a[j] = a[j - 1];
j--;
}
a[j] = n;
}
System.out.print("Sorted Array:");
for(i=0;i<a.length;i++)
System.out.print("\t" +a[i]);
}
}
{
public static void main(String args[])
{
int a[]={2,6,4,8,9};
int i, j, n;
for (i = 1; i < a.length; i++)
{
n = a[i];
j = i;
while (j > 0 && a[j - 1] > n)
{
a[j] = a[j - 1];
j--;
}
a[j] = n;
}
System.out.print("Sorted Array:");
for(i=0;i<a.length;i++)
System.out.print("\t" +a[i]);
}
}
Very Useful. For more examples visit http://answersz.com
ReplyDeletehttps://www.welookups.com/java/Java_Numbers_Class.htm
ReplyDelete