See also: http://soj.sysu.edu.cn/show_problem.php?pid=1004&cid=569
Sure enough, the brain is problematic, and the test sample is randomly generated.
Design an efficient Fine_median algorithm of logrithmic running time.
typedef int comparable;
Comparable Find_median (const vector<comparable> &L1, const vector<comparable> &L2);
/*
Pre:the ordered lists L1,l2 is not all empty.
Post:the median of the lists combined in order is returned. If the size of the merged list is even, then the first of the and the medians is returned. For exmaple, L1 = (up), l2= (1,3,4,5), then 2 is returned.
*/
#include <vector>usingStd::vector;typedefintcomparable;//#include <iostream>//#include <cstdlib>//using Std::cout;Comparable Find_median (ConstComparable *arra,intLenA,ConstComparable *ARRB,intLenB) {//static int record = 0;//++record;//if (record% = = 0) {//System ("pause");// }//cout << "Times" << record << "\narra:";//for (int i = 0; i < LenA; ++i) {//cout << arra[i] << ";// }//cout << ' \ n ';//cout << "ARRB:";//for (int i = 0; i < LenB; ++i) {//cout << arrb[i] << ";// }//cout << "\ n"; if(LenA = = LenB && LenA = =1) { return(*arra > *ARRB *ARRB: *ArrA); } intMidA = (LenA-1) /2; intMidB = (LenB-1) /2; if(LenA = =1) { if(LenB%2==1) { if(arra[0] >=ARRB[MIDB]) { returnARRB[MIDB]; } Else if(arra[0] <= ARRB[MIDB-1]) { returnARRB[MIDB-1]; } Else { returnarra[0]; } } Else { if(arra[0] >= Arrb[midb +1]) { returnARRB[MIDB +1]; } Else if(arra[0] <=ARRB[MIDB]) { returnARRB[MIDB]; } Else { returnarra[0]; } } } Else if(LenB = =1) { returnFind_median (ARRB, LenB, ArrA, LenA); } intL = (MidA <= MidB?mida:midb); //cout << "L:" << l << "MidA:" << midA << "MidB:" << midB << ' \ n '; if(LenA = =2|| LenB = =2) { // ++l; } if(Arra[mida] = =ARRB[MIDB]) { returnArra[mida]; } Else if(Arra[mida] <ARRB[MIDB]) { returnFind_median (ArrA + L, lena-l, ARRB, LenB-l); } Else { returnFind_median (ArrA, lena-l, ARRB + L, LenB-l); }}comparable Find_median (ConstVector<comparable> &L1,ConstVector<comparable> &L2) { returnFind_median (&l1[0], L1.size (), &l2[0], l2.size ());}
//The following is an O (n) version, used to detect correctnessusingstd::vector; Comparable find_median2 (ConstVector<comparable> &L1,ConstVector<comparable> &L2) {Vector<Comparable>:: Const_iterator it1, it2; It1=L1.begin (); It2=L2.begin (); unsignedintMid = (l1.size () + l2.size ()-1) /2; unsignedintK =0; while(K < mid && it1! = l1.end () && it2! =L2.end ()) { if(*it1 <= *it2) { ++it1; } Else { ++it2; } ++K; } while(It1! = L1.end () && K <mid) {++it1; ++K; } while(It2! = L2.end () && K <mid) {++it2; ++K; } if(It1 = =L1.end ()) { return*it2; } Else if(It2 = =L2.end ()) { return*it1; } Else { return(*it1 < *it2 *it1: *it2); }}//Test program # include<algorithm>#include<iostream>#include<ctime>#include<cstdlib>usingstd::cout;usingStd::sort;intMain () {//vector<comparable> L1 = {1, 2, 4.2, 7, +;Vector<comparable>L1; Vector<Comparable>L2; Srand (Time (0)); intN = -;//test Sample Count while(n--) {L1= Vector<comparable> (rand ()%4+1); L2= Vector<comparable> (rand ()%4+1); for(Auto &i:l2) {i= rand ()% -;; } for(Auto &i:l1) {i= rand ()% -; } sort (L1.begin (), L1.end ()); Sort (L2.begin (), L2.end ()); intM1 =Find_median (L1, L2); intM2 =find_median2 (L1, L2); cout<< M1 <<'\ n'; cout<< m2 <<'\ n'; if(M1! =m2) {System ("Pause"); } }}
The median number of two ordered arrays