标签:
一、参考资料
1.
http://www.codeceo.com/article/8-java-sort.html
二、代码
1.插入排序
public class Demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[]={3,1};
InsertSort(a);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+",");
}
}
static void InsertSort(int a[]) {
for (int i = 1; i < a.length; i++) {
int temp=a[i];//插入算法需要一个辅助空间
for (int j = i-1; j >= 0; j--) { //此处需要往后移动,所以必须从后往前顺序对比
if (a[j]>temp) {
a[j+1]=a[j];
a[j+1]=temp;
}
}
}
}
}
标签:
原文地址:http://www.cnblogs.com/yaolei/p/5497716.html