Sorting Algorithms ------[Insertion Sort]

来源:互联网 发布:office在哪卸载软件 编辑:程序博客网 时间:2024/06/07 06:59
  1. public class InsertionSort {
  2.     public static void main(String args[]){
  3.         InsertionSort s=new InsertionSort();
  4.         s.a();
  5.     }
  6.     public void a(){
  7.         int []num={7,5,1,4,6,5,3,9,23,13,57,2,456,434};
  8.         long t=System.currentTimeMillis();
  9.         for(int i=1;i<num.length;i++){
  10.             int key=num[i];
  11.             int j=i-1;
  12.             while(j!=0 && j>=0 && num[j]<key){
  13.                 num[j+1]=num[j];
  14.                 j=j-1;
  15.             }
  16.             num[j+1]=key;
  17.         }
  18.         long t1=System.currentTimeMillis();
  19.         System.out.println(t+"-----"+t1);
  20.         for(int i=0;i<num.length;i++){
  21.             System.out.print(num[i]+"--");
  22.         }
  23.     }
  24. }

Insertion Sort :

 

Moderately for small N

Not At All for large N