/**
* 插入排序。
* @author Bright Lee
*/
public class InsertionSort {
public static void sort(int[] a) {
// 从小到大排序。
// a[0..i-1]是已排好序的子数组。
// a[i..a.length-1]是仍旧没有参与排序的子数组。
for (int i = 1; i < a.length; i++) {
int key = a[i];
// 将比key大的数组元素依次后移:
int j;
for (j = i - 1; j >= 0; j--) {
if (a[j] <= key) {
break;
}
a[j + 1] = a[j];
}
// 将key填到最后位置上:
a[j + 1] = key;
}
}
public static void main(String[] args) {
int[] a = {2, 5, 3, 4, 1, 0};
sort(a);
for (int i = 0; i < a.length; i++) {
int v = a[i];
System.out.println(v);
}
}
}
榴芒客服系统:https://blog.youkuaiyun.com/look4liming/article/details/83146776
67

被折叠的 条评论
为什么被折叠?



