gpt4 book ai didi

java - MergeSort 和 insertionSort 组合算法一起运行比单独运行慢但应该运行得更快

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:39:08 24 4
gpt4 key购买 nike

我有一个合并排序需要在特定数字切换到插入排序,这是我的阈值。但我的阈值只能是 1、2 或 3,因为在任何其他情况下,我的合并排序都会变得非常慢。我似乎无法让代码协同工作。

这是我的代码:

public class InsertionSort {

// I haven't found the right Threshold yet, but it should work with any number between 1-100.

public final static int M = 16;

private void Merge(int arr[], int left, int mid, int right) {
int size1 = mid - left + 1;
int size2 = right - mid;

int LeftArray[] = new int[size1];
int RightArray[] = new int[size2];

for (int i = 0; i < size1; ++i) {
LeftArray[i] = arr[left + i];

}
for (int j = 0; j < size2; ++j) {
RightArray[j] = arr[mid + 1 + j];

}

int i = 0;
int j = 0;
int k = left;

while (i < size1 && j < size2) {
if (LeftArray[i] <= RightArray[j]) {
arr[k] = LeftArray[i];
i++;
} else {
arr[k] = RightArray[j];
j++;
}
k++;
}


while (i < size1) {
arr[k] = LeftArray[i];
i++;
k++;
}

while (j < size2) {
arr[k] = RightArray[j];
j++;
k++;
}

}

public void MergeSort(int arr[], int left, int right, int M) {

if ( left < right ) {

int mid = (left + right) / 2;
MergeSort(arr, left, mid, M);
MergeSort(arr, (mid+1), right, M);
Merge(arr, left, mid, right);
} else if ((right - left + 1) <= M) {
insertion_sort(arr, left, right);
}}


static void printArray(int arr[]) {
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}

static int[] readIntfile(String filename) throws Exception {
// Read file into a byte array, and then combine every group of four bytes to an
// int. (Not
// the standard way, but it works!)
byte[] bytes = Files.readAllBytes(Paths.get(filename));
int[] ints = new int[bytes.length / 4];
for (int i = 0; i < ints.length; i++) {
for (int j = 0; j < 4; j++) {
ints[i] += (bytes[i * 4 + j] & 255) << (3 - j) * 8;
}
}
return ints;

}

public static void insertion_sort(int a[], int left, int right) {
int j;
for (int i = left; i <= right; i++) {
int tmp = a[i];
for (j = i; j > 0 && tmp < a[j - 1]; j--) {
a[j] = a[j - 1];
}
a[j] = tmp;
}
}


public static void main(String args[]) throws Exception {
// I have texfile named this with 1000000 numbers
int arr[] = readIntfile("smallints");

// you can also try with this array for example
// int arr[] = {3, 6, 4, 8, 500, 1, 5, 10, 7, 9, 0, 2, 100, 300, 1000, 20, 13, 17, 55, 93};
InsertionSort insert = new InsertionSort();
long before = System.currentTimeMillis();
insert.MergeSort(arr, 0, arr.length-1, M);
long after = System.currentTimeMillis();
printArray(arr);
System.out.println("\n" + "Done " + ((after - before) / 1000.0 + " sek"));
}
}

最佳答案

public void MergeSort(int arr[], int left, int right, int M) {

if ( left < right ) {

int mid = (left + right) / 2;
MergeSort(arr, left, mid, M);
MergeSort(arr, (mid+1), right, M);
Merge(arr, left, mid, right);
} else if ((right - left + 1) <= M) {
insertion_sort(arr, left, right);
}}

你这里有一个严重的问题。您有一个类全局变量 M,它是您的插入排序阈值,您有一个参数 M,它是您要排序的子数组的长度。该参数将覆盖全局类。基本上,您的 public final static int M = 16; 从未在 MergeSort 方法中出现。

此外,如果 left 不小于 right,则没有可排序的。我想你想要这样的东西:

public final static int insertionThreshold = 16;

public void MergeSort(int arr[], int left, int right, int M) {
if ( left < right ) {
if (left - right <= insertionThreshold) {
insertion_sort(arr, left, right);
else {
int mid = (left + right) / 2;
MergeSort(arr, left, mid, M);
MergeSort(arr, (mid+1), right, M);
Merge(arr, left, mid, right);
}
}
}

关于java - MergeSort 和 insertionSort 组合算法一起运行比单独运行慢但应该运行得更快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53729665/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com