展开全部
我用5个思62616964757a686964616fe78988e69d8331333431366366路的来解决两个非减序数组的中位数.但是成功的只有四个,思路3边界问题太严重.
有时间再来弄好他,他在考试中也不适合使用(边界问题没法快速解决,网上也有人说小数据枚举法确定边界),总的来说费事.
主函数:#include
#include
#include
double findMedianSortedArrays_1(int* nums1, int nums1Size, int* nums2, int nums2Size);
double sss(int a[], int m, int b[], int n, int k);
double findMedianSortedArrays_2(int* nums1, int nums1Size, int* nums2, int nums2Size);
double findMedianSortedArrays_3(int* nums1, int nums1Size, int* nums2, int nums2Size);
double findMedianSortedArrays_4(int* nums1, int nums1Size, int* nums2, int nums2Size);
double findMedianSortedArrays_5(int* nums1, int nums1Size, int* nums2, int nums2Size);
int main() {
clock_t start_t, end_t;
double total_t;
double mid = 0;
// 1 2 3 4 5 6 7 8 9 10 11
int str1[1000] = { 0, 2, 4, 5, 7, 9, 10, 15, 21, 23, 25 }, \
str2[1000] = { 5, 6, 9, 15, 17, 18, 20, 23, 24, 26, 27, 29, 50 };
// 1 2 3 4 5 6 7 8 9 10 11 12 13
start_t = clock();
mid = findMedianSortedArrays_1(str1, 11, str2, 13);
end_t = clock();
total_t = (double)((double)end_t - (double)start_t) / CLOCKS_PER_SEC;
printf("方法1:CPU 占用的总时间:%f ns\n", (total_t * 1000000000));
printf("中位数 %f \r\n", mid);
start_t = clock();
mid = findMedianSortedArrays_2(str1, 11, str2, 13);
end_t = clock();
total_t = (double)((double)end_t - (double)start_t) / CLOCKS_PER_SEC;
printf("方法2:CPU 占用的总时间:%f ns\n", (total_t * 1000000000));
printf("中位数 %f \r\n", mid);
start_t = clock();
mid = findMedianSortedArrays_3(str1, 11, str2, 13);
end_t = clock();
total_t = (double)((double)end_t - (double)start_t) / CLOCKS_PER_SEC;
printf("方法3:CPU 占用的总时间:%f ns\n", (total_t * 1000000000));
printf("中位数 %f \r\n", mid);
start_t = clock();
mid = findMedianSortedArrays_4(str1, 11, str2, 13);
end_t = clock();
total_t = (double)((double)end_t - (double)start_t) / CLOCKS_PER_SEC;
printf("方法4:CPU 占用的总时间:%f ns\n", (total_t * 1000000000));
printf("中位数 %f \r\n", mid);
start_t = clock();
mid = findMedianSortedArrays_5(str1, 11, str2, 13);
end_t = clock();
total_t = (double)((double)end_t - (double)start_t) / CLOCKS_PER_SEC;
printf("方法5:CPU 占用的总时间:%f ns\n", (total_t * 1000000000));
printf("中位数 %f \r\n", mid);
return 0;
}
思路一:/*
方法1:采用归并两个非减数组形成新非减数组,然后求取新数组的中位数.
性能分析:归并两数组的时间复杂度O(n+m),查找中位数时间复杂度O(1).所以时间复杂度O((n+m)*1)&#