数据库全部代码将两个递增的有序顺序表合并为一个递增的有序顺序表
时间: 2024-03-26 15:22:28 浏览: 49
//定义一个函数来合并两个有序顺序表
void merge(int arr1[], int m, int arr2[], int n, int result[]) {
int i = 0, j = 0, k = 0;
while (i < m && j < n) {
if (arr1[i] <= arr2[j]) {
result[k++] = arr1[i++];
}
else {
result[k++] = arr2[j++];
}
}
while (i < m) {
result[k++] = arr1[i++];
}
while (j < n) {
result[k++] = arr2[j++];
}
}
int main() {
int arr1[] = { 1, 3, 5, 7, 9 };
int arr2[] = { 2, 4, 6, 8, 10 };
int m = sizeof(arr1) / sizeof(arr1[0]);
int n = sizeof(arr2) / sizeof(arr2[0]);
int result[m + n];
merge(arr1, m, arr2, n, result);
printf("合并后的数组为:\n");
for (int i = 0; i < m + n; i++) {
printf("%d ", result[i]);
}
return 0;
}
阅读全文