📅  最后修改于: 2023-12-03 14:50:40.195000             🧑  作者: Mango
合并排序与插入排序是两种常见的排序算法。两者各有优点,对于不同的数据规模和数据类型可以选择不同的算法。
public static void mergeSort(int[] nums, int left, int right) {
if (left >= right) {
return;
}
int mid = (left + right) / 2;
mergeSort(nums, left, mid);
mergeSort(nums, mid + 1, right);
merge(nums, left, mid, right);
}
public static void merge(int[] nums, int left, int mid, int right) {
int[] temp = new int[right - left + 1];
int i = left, j = mid + 1, k = 0;
while (i <= mid && j <= right) {
temp[k++] = nums[i] < nums[j] ? nums[i++] : nums[j++];
}
while (i <= mid) {
temp[k++] = nums[i++];
}
while (j <= right) {
temp[k++] = nums[j++];
}
System.arraycopy(temp, 0, nums, left, temp.length);
}
public static void mergeSort(int[] nums) {
int n = nums.length;
for (int len = 1; len < n; len *= 2) {
for (int i = 0; i < n - len; i += len * 2) {
int left = i, mid = i + len - 1, right = Math.min(i + len * 2 - 1, n - 1);
merge(nums, left, mid, right);
}
}
}
public static void merge(int[] nums, int left, int mid, int right) {
int[] temp = new int[right - left + 1];
int i = left, j = mid + 1, k = 0;
while (i <= mid && j <= right) {
temp[k++] = nums[i] < nums[j] ? nums[i++] : nums[j++];
}
while (i <= mid) {
temp[k++] = nums[i++];
}
while (j <= right) {
temp[k++] = nums[j++];
}
System.arraycopy(temp, 0, nums, left, temp.length);
}
public static void insertionSort(int[] nums) {
for (int i = 1; i < nums.length; i++) {
int j = i;
int temp = nums[j];
while (j > 0 && temp < nums[j - 1]) {
nums[j] = nums[j - 1];
j--;
}
nums[j] = temp;
}
}
public static void binaryInsertionSort(int[] nums) {
for (int i = 1; i < nums.length; i++) {
int low = 0, high = i - 1;
int temp = nums[i];
while (low <= high) {
int mid = (low + high) / 2;
if (nums[mid] < temp) {
low = mid + 1;
} else {
high = mid - 1;
}
}
for (int j = i; j > low; j--) {
nums[j] = nums[j - 1];
}
nums[low] = temp;
}
}