[TOC]
-
Calculate the medians m1 and m2 of the input arrays ar1[] and ar2[] respectively. 個別計算input陣列 ar1, ar2 的中間值 m1, m2。
-
If m1 and m2 both are equal then we are done. 當 m1=m2 則完成計算。
return m1 (or m2)
-
If m1 is greater than m2, then median is present in one of the below two subarrays. 當假若m1>m2 則切分出以下2種 (ar1, ar2) 的新陣列。
a. From first element of ar1 to m1 (ar1[0...|n/2|])
b. From m2 to last element of ar2 (ar2[|n/2|...n-1])
-
If m2 is greater than m1, then median is present in one of the below two subarrays. 當m2>m1 則反之亦然。
a. From m1 to last element of ar1 (ar1[|n/2|...n-1])
b. From first element of ar2 to m2 (ar2[0...|n/2|])
-
Repeat the above process until size of both the subarrays becomes 2. 重複上述步驟直到 2. m1, m2相等時回傳。
-
If size of the two arrays is 2 then use below formula to get the median. 此外,當兩input陣列長度為2時最快的方式,就直接找兩陣列下的最大值及最小值即可。
Median = (max(ar1[0], ar2[0]) + min(ar1[1], ar2[1]))/2
- Basis method: O(n)
- Advanced method: O(logn)
- 這邊我先以python隨機產生N = 10^4, 10^5, 10^6的數據資料
- 產生隨機測資以後,進行3種不等測資比較
-
N = 10^4
-
N = 10^5
-
N = 10^6
Method \ N | 10^4 | 10^5 | 10^6 |
---|---|---|---|
Basis | 98 | 693 | 6581 |
Advanced | 20 | 28 | 66 |
(Meas. unit: microseconds)
- 演算法問題-對等長度找中間值 https://www.geeksforgeeks.org/median-of-two-sorted-arrays/
- leetcode經典問題-在不等長陣列找中間值 https://zhuanlan.zhihu.com/p/33106213
- C++檢視程式運算時間 https://www.geeksforgeeks.org/measure-execution-time-function-cpp/
- Python產生亂數陣列:https://codertw.com/程式語言/366152/
- C++讀取檔案:http://www.programmer-club.com.tw/ShowSameTitleN/c/14291.html
:::info Find this document incomplete? Leave a comment! :::