- public class InsertionSort {
- public static void main(String args[]){
- InsertionSort s=new InsertionSort();
- s.a();
- }
- public void a(){
- int []num={7,5,1,4,6,5,3,9,23,13,57,2,456,434};
- long t=System.currentTimeMillis();
- for(int i=1;i<num.length;i++){
- int key=num[i];
- int j=i-1;
- while(j!=0 && j>=0 && num[j]<key){
- num[j+1]=num[j];
- j=j-1;
- }
- num[j+1]=key;
- }
- long t1=System.currentTimeMillis();
- System.out.println(t+"-----"+t1);
- for(int i=0;i<num.length;i++){
- System.out.print(num[i]+"--");
- }
- }
- }
Insertion Sort :
Moderately for small N
Not At All for large N