在 C 中合并和排序两个包含双精度数的已排序数组
Merging and sorting two sorted arrays containing doubles in C
所以我有两个由双数组成的数组,它们是排序的。
我想以最有效的方式将这些组合成一个数组,并对这个数组进行排序。
我的第一个想法是,也许您可以简单地将它们连接在一起,然后对其使用 qsort,但这效率不高。那么也许改用归并排序?但是,我对如何在 C 中实现归并排序有点迷茫,有什么想法吗?
我正在使用其中一个答案中的代码来尝试合并排序。我还没有把它变成自己的方法,等我开始工作之后再做。
double partitions[][5] = {{45.0, 5.0, 88.4, 0.4, 44.0}, {1000.0, 2.0, 3.4, 7.0, 50.0}};
double* res;
int n1 = sizeof(partitions[0])/sizeof(partitions[0][0]);
int n2 = sizeof(partitions[1])/sizeof(partitions[1][0]);
int n3 = n1 + n2;
res = malloc(n3 * sizeof(double));
// Merging starts
int i = 0;
int j = 0;
int k = 0;
while (i < n1 && j < n2) {
if (partitions[0][i] - partitions[1][j] < 0.000001) {
res[k] = partitions[0][i];
i++;
k++;
}
else {
res[k] = partitions[1][j];
k++;
j++;
}
}
/* Some elements in array 'arr1' are still remaining
where as the array 'arr2' is exhausted */
while (i < n1) {
res[k] = partitions[0][i];
i++;
k++;
}
/* Some elements in array 'arr2' are still remaining
where as the array 'arr1' is exhausted */
while (j < n2) {
res[k] = partitions[1][j];
k++;
j++;
}
int m;
for (m = 0; m < n3; m++)
printf("%f \n", res[m]);
由于数组已排序,您只需要合并排序的合并部分,即 O(n1+n2),其中 n1
是一个数组的长度,n2
是长度另一个数组的:
例如:
void merge(int n1, int n2){ //suppose global arrays arr1,arr2 and result array
i = 0;
j = 0;
k = 0;
// Merging starts
while (i < n1 && j < n2) {
if (arr1[i] <= arr2[j]) {
res[k] = arr1[i];
i++;
k++;
}
else {
res[k] = arr2[j];
k++;
j++;
}
}
/* Some elements in array 'arr1' are still remaining
where as the array 'arr2' is exhausted */
while (i < n1) {
res[k] = arr1[i];
i++;
k++;
}
/* Some elements in array 'arr2' are still remaining
where as the array 'arr1' is exhausted */
while (j < n2) {
res[k] = arr2[j];
k++;
j++;
}
}
我还注意到您的数组包含双精度数,因此您需要在比较两个数字时更改条件。例如,您需要为 if (arr1[i] < arr2[j])
编写条件而不是 if (arr1[i] <= arr2[j])
,然后是其他部分。
所以我有两个由双数组成的数组,它们是排序的。
我想以最有效的方式将这些组合成一个数组,并对这个数组进行排序。
我的第一个想法是,也许您可以简单地将它们连接在一起,然后对其使用 qsort,但这效率不高。那么也许改用归并排序?但是,我对如何在 C 中实现归并排序有点迷茫,有什么想法吗?
我正在使用其中一个答案中的代码来尝试合并排序。我还没有把它变成自己的方法,等我开始工作之后再做。
double partitions[][5] = {{45.0, 5.0, 88.4, 0.4, 44.0}, {1000.0, 2.0, 3.4, 7.0, 50.0}};
double* res;
int n1 = sizeof(partitions[0])/sizeof(partitions[0][0]);
int n2 = sizeof(partitions[1])/sizeof(partitions[1][0]);
int n3 = n1 + n2;
res = malloc(n3 * sizeof(double));
// Merging starts
int i = 0;
int j = 0;
int k = 0;
while (i < n1 && j < n2) {
if (partitions[0][i] - partitions[1][j] < 0.000001) {
res[k] = partitions[0][i];
i++;
k++;
}
else {
res[k] = partitions[1][j];
k++;
j++;
}
}
/* Some elements in array 'arr1' are still remaining
where as the array 'arr2' is exhausted */
while (i < n1) {
res[k] = partitions[0][i];
i++;
k++;
}
/* Some elements in array 'arr2' are still remaining
where as the array 'arr1' is exhausted */
while (j < n2) {
res[k] = partitions[1][j];
k++;
j++;
}
int m;
for (m = 0; m < n3; m++)
printf("%f \n", res[m]);
由于数组已排序,您只需要合并排序的合并部分,即 O(n1+n2),其中 n1
是一个数组的长度,n2
是长度另一个数组的:
例如:
void merge(int n1, int n2){ //suppose global arrays arr1,arr2 and result array
i = 0;
j = 0;
k = 0;
// Merging starts
while (i < n1 && j < n2) {
if (arr1[i] <= arr2[j]) {
res[k] = arr1[i];
i++;
k++;
}
else {
res[k] = arr2[j];
k++;
j++;
}
}
/* Some elements in array 'arr1' are still remaining
where as the array 'arr2' is exhausted */
while (i < n1) {
res[k] = arr1[i];
i++;
k++;
}
/* Some elements in array 'arr2' are still remaining
where as the array 'arr1' is exhausted */
while (j < n2) {
res[k] = arr2[j];
k++;
j++;
}
}
我还注意到您的数组包含双精度数,因此您需要在比较两个数字时更改条件。例如,您需要为 if (arr1[i] < arr2[j])
编写条件而不是 if (arr1[i] <= arr2[j])
,然后是其他部分。