Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. However, insertion sort provides several advantages.
Insertion Sort Java Program Example
import java.util.Scanner;public class InsertionSort
{
public static void main(String args[])
{
int n,j,i,temp;
Scanner scr=new Scanner(System.in);
System.out.println(" Insertion Sort");
System.out.println(" Enter the no.of integer elements");
n=scr.nextInt();
int a[]=new int[n];
System.out.println(" enter elements into array");
for(i=0;i
/* .............. Sort Technique Code ............*/
for(i=1;i
j=1;
temp=a[i];
while(j>0&&temp{
a[j]=a[j-1];
j=j-1;
}
a[j]=temp;
}
System.out.println("\n elements after sorting");
for(i=0;i
}
}
Out Put:
0 comments :
Post a Comment
Note: only a member of this blog may post a comment.