冒泡排序
private static void bubble(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int a = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = a;
}
}
}
}
快速排序
public static void quickSort(int[] arr, int z, int y) {
if (y <= z) {
return;
}
int i = z, j = y, x = arr[z];
while (j != i) {
while (arr[j] >= x && j > i) {
j--;
}
while (arr[i] <= x && j > i) {
i++;
}
int a = arr[i];
arr[i] = arr[j];
arr[j] = a;
}
arr[z] = arr[i];
arr[i]=x;
quickSort(arr, z, i - 1);
quickSort(arr, i + 1, y);
}
二分查找
public static int half(int[] arr, int a) {
int x = 0, d = arr.length - 1;
while (x <= d) {
int z = (d + x) >> 1;
if (a < arr[z]) {
d = z - 1;
} else if (a > arr[z]) {
x = z + 1;
} else {
return z;
}
}
return -1;
}