[java]代码库
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
int[] a = { 5, 4, 2, 4, 9, 1 };
Arrays.sort(a);
System.out.println("Arrays.sort(a)");
for (int i : a) {
System.out.println(i);
}
System.out.println("冒泡排序");
for (int i : bubbleSort(a)) {
System.out.println(i);
}
System.out.println("选择排序");
for (int i : selectSort(a)) {
System.out.println(i);
}
System.out.println("插入排序");
for (int i : insertSort(a)) {
System.out.println(i);
}
}
public static int[] bubbleSort(int[] args) {
for (int i = 0; i < args.length - 1; i++) {
for (int j = i + 1; j < args.length; j++) {
if (args[i] > args[j]) {
int temp = args[i];
args[i] = args[j];
args[j] = temp;
}
}
}
return args;
}
public static int[] selectSort(int[] args) {
for (int i = 0; i < args.length - 1; i++) {
int min = i;
for (int j = i + 1; j < args.length; j++) {
if (args[min] > args[j])
min = j;
}
if (min != i) {
int temp = args[i];
args[i] = args[min];
args[min] = temp;
}
}
return args;
}
public static int[] insertSort(int[] args) {
for (int i = 1; i < args.length; i++) {
for (int j = i; j > 0; j--) {
if (args[j] < args[j - 1]) {
int temp = args[-1];
args[j - 1] = args[j];
args[j] = temp;
} else
break;
}
}
return args;
}
}