public class TestDemo {
public static void main(String[] args) {
int[] arr = new int[10];
Random random = new Random();
for (int i = 0; i < arr.length; i++) {
arr[i] = random.nextInt(100);
}
display(arr);
sort(arr);
display(arr);
}
public static void sort(int[] arr) {
int len = arr.length;
int left, right, num;
for (int i = 1; i < len; i++) {
left = 0;
right = i - 1;
num = arr[i];
while (left <= right) {
int middle = (left + right) >> 0x1;
if (num < arr[middle]) {
right = middle - 1;
} else {
left = middle + 1;
}
}
for (int j = i - 1; j >= left; j--) {
arr[j + 1] = arr[j];
}
arr[left] = num;
}
}
private static void swap(int[] arr, int i, int j) {
if (i == j) {
return;
}
arr[i] = arr[i] ^ arr[j];
arr[j] = arr[i] ^ arr[j];
arr[i] = arr[i] ^ arr[j];
}
private static void display(int[] arr) {
for (int i = 0, len = arr.length; i < len; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
Java排序四:二分插入排序
最新推荐文章于 2022-11-01 15:49:26 发布