Insertion Sort
It sorts the array proceeding towards the end by shifting the numbers until we find an element that is less than equal to the item we have selected presently, worst time O(n^2) and best time O(nlog n).
public class InsertionSort{
public static void main(String args[]){
int num[]={44,22,33,34,25}; //any arbit array of number, u can change its length
//Sorting Algorithm Start
int i=0, j=0, value;
for(i=1; i<num.length; i++){
value = num[i];
j=i-1;
while(j>=0 && num[j]>value){ //check
num[j+1] = num[j];
j=j-1;
}
num[j+1] = value;
}
//Sorting Algorithm Ends
System.out.print("Sorted: ");
for(i=0; i<num.length; i++){
System.out.print(num[i] + " ");
}
}
}
Download Program


