插入排序:
public class InsertSort {
public static void main(String[] args) {
int[] ret = insertSort(new int[] { 9, 2, 4, 1, 2, 5, 10, 23, 33, 2, 22 });
for (int val : ret) {
System.out.println(val);
}
}
public static int[] insertSort(int[] arr) {
if (arr == null || arr.length < 2) {
return arr;
}
for (int i = 1; i < arr.length; i++) {
for (int j = i; j > 0; j--) {
if (arr[j] < arr[j - 1]) {
// TODO:
int temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
} else {
// 接下来是无用功
break;
}
}
}
return arr;
}
}