java冒泡排序
public class BubbleSort {
public static void main(String[] args) {
int a[] = { 2, 3, 6, 4, 0, 1, 7, 8, 5, 9 };
bubbleSort(a);
}
public static void toString(int[] a) {
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
private static void bubbleSort(int[] a) {
int length = a.length;
int temp = 0;
for (int j = 0; j < a.length - 1; j++) {
for (int i = 0; i < a.length - 1 - j; i++) {
if (a[i] > a[i + 1]) {
temp = a[i + 1];
a[i + 1] = a[i];
a[i] = temp;
}
}
}
toString(a);
}
}
2.java选择排序
public class SortDemo { public static void main(String[] args) { int[] arr = new int[] { 5, 3, 6, 2, 10, 2, 1 }; selectSort(arr); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } public static void selectSort(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { int minIndex = i; for (int j = i + 1; j < arr.length; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } if (i != minIndex) { int temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; } } } } 3.java插入排序 public class CRPX {