Skip to main content

Insertion Sort Algorithm

Insertion sort is a simple sorting algorithm that builds the final sorted array one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort.

Insertion Sort

http://localhost:3000
class insertionSort{
public static void main(String [] args){
int i,j,current,size;

int a[] = {4,3,2,10,12,1,5,6};
size = a.length;
for(i = 1; i<size; i++){
current = a[i];
j = i- 1;
while(j>=0 && a[j]>current){
a[j+1] = a[j];
j--;
}
a[j+1] = current;
}
for(i = 0; i<a.length; i++){
System.out.print(a[i]+" ");
}
}
}