首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

排序系列(3)-插入排序

2012-12-24 
排序系列(三)---插入排序?//lilywangcnpublic class InsertSort {public static void main(String[] args)

排序系列(三)---插入排序

?

//lilywangcn

public class InsertSort {

public static void main(String[] args) {

// TODO Auto-generated method stub

int[] array=new int[]{10,30,20,4,9,-1,6,15};

for(int i=1;i<array.length;i++){

int tmp=array[i];

int j=i-1;

while(j>=0 && array[j]>tmp){

array[j+1]=array[j];

--j;

}

array[j+1]=tmp;

}

for(int i=0;i<array.length;i++){

System.out.print(array[i]+" ");

}

}

?

}

算法复杂度:O(n*n),算法稳定。

运行结果:

-1 4 6 9 10 15 20 30?

热点排行