使用C ++合并两个排序的数组。
问题陈述
给定2个排序数组列表。编写函数以将给定的两个排序数组合并为一个
Arr1[] = {10,15, 17, 20} Arr2[] = {5, 9, 13, 19} Result[] = {5, 9, 10, 13, 15, 17, 19, 20}
算法
1. Traverse both array 1.1. If arr1[i] < arr2[j] 1.1.1. Add arr[i] to new array 1.1.2. Increment ‘i’ and index of result array ‘k’ 1.2. If arr2[i] < arr1[j] 1.2.1. Add arr[j] to new array 1.2.2. Increment ‘j’ and index of result array ‘k’ 2. Repeat procedure until both arrays are exhausted 3. Return resultant array
示例
#include <iostream> #define SIZE(arr) (sizeof(arr) / sizeof(arr[0])) using namespace std; void mergeSortedArrays(int *arr1, int n1, int *arr2, int n2, int *result){ int i, j, k; i = 0; j = 0; k = 0; while (i < n1 && j < n2) { if (arr1[i] < arr2[j]) { result[k] = arr1[i]; ++k; ++i; } else { result[k] = arr2[j]; ++k; ++j; } } while (i < n1) { result[k] = arr1[i]; ++k; ++i; } while (j < n2) { result[k] = arr2[j]; ++k; ++j; } } void dispalyArray(int *arr, int n){ for (int i = 0; i < n; ++i) { cout << arr[i] << " "; } cout << endl; } int main(){ int arr1[] = {10, 15, 17, 20}; int arr2[] = {5, 9, 7, 13, 19}; int result[SIZE(arr1) + SIZE(arr2)]; cout << "第一个排序数组:" << endl; dispalyArray(arr1, SIZE(arr1)); cout << "第二排序数组:" << endl; dispalyArray(arr2, SIZE(arr2)); mergeSortedArrays(arr1, SIZE(arr1), arr2, SIZE(arr2), result); cout << "最终排序的数组:" << endl; dispalyArray(result, SIZE(result)); return 0; }
输出结果
当您编译并执行上述程序时。它生成以下输出-
第一个排序数组: 10 15 17 20 第二排序数组: 5 9 7 13 19 最终排序的数组: 5 9 7 10 13 15 17 19 20