ITeye博客

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

lilywangcn · 2011-04-12 18:15 · algorithms · 编程语言 · J# · Markdown

//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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。