<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # O(log(min(n(n,m)))中具有不同大小的兩個排序數組的中位數 > 原文: [https://www.geeksforgeeks.org/median-two-sorted-arrays-different-sizes-ologminn-m/](https://www.geeksforgeeks.org/median-two-sorted-arrays-different-sizes-ologminn-m/) 給定兩個排序數組 a []和 b [],當 n 是第一個數組中的元素數時,任務是在 O(log(min(n(n,m)))中找到這些排序數組的中位數。 m 是第二個數組中的元素數。 **先決條件**: [兩個不同大小的排序數組的中位數。](https://www.geeksforgeeks.org/median-of-two-sorted-arrays-of-different-sizes/) **示例**: ``` Input : ar1[] = {-5, 3, 6, 12, 15} ar2[] = {-12, -10, -6, -3, 4, 10} The merged array is : ar3[] = {-12, -10, -6, -5 , -3, 3, 4, 6, 10, 12, 15} Output : The median is 3. Input : ar1[] = {2, 3, 5, 8} ar2[] = {10, 12, 14, 16, 18, 20} The merged array is : ar3[] = {2, 3, 5, 8, 10, 12, 14, 16, 18, 20} if the number of the elements are even, so there are two middle elements, take the average between the two : (10 + 12) / 2 = 11\. Output : The median is 11. ``` **注意**:如果總數為偶數,并且如果我們要返回合并數組中存在的中位數,則可以返回(n + m)/ 2 或(n + m)/ 2 – 1 位。 在這種情況下,中位數可以是 10 或 12。 **方法**:開始將兩個數組劃分為兩半(不是兩個部分,但兩個分區都應具有相同數量的元素)。 前半部分包含來自第一個和第二個數組的一些第一元素,后半部分包含形成第一個和第二個數組的其余(或最后一個)元素。 由于數組可以具有不同的大小,因此并不意味著要從每個數組中取出每一半。 以下示例闡明了解釋。 達到這樣的條件:上半部分的每個元素都小于或等于下半部分的每個元素。 **如何達到此條件?** 偶數情況下的示例。 假設找到了分區。 因為 A []和 B []是兩個排序的數組,所以 a1 小于或等于 a2,b2 小于或等于 b3。 現在,檢查 a1 是否小于或等于 b3,以及 b2 是否小于或等于 a2。 如果是這種情況,則意味著上半部分的每個元素都小于或等于下半部分的每個元素,因為 a1 大于或等于 A []中的每個元素(a0)和 b2 大于或等于 B []中它之前的每個元素(b1 和 b0)。 如果總數為偶數,則中位數將為 a1,b2 的最大值與 a2,b3 的最小值之間的平均值,但是如果總數為奇數,則中位數將為 a2,b2 的最大值。 但是,如果不是這兩種情況,則有兩種選擇(以偶數示例為例):[b] > a2 表示 b2 > a2 或 a1 > b3 ,在數組的右側搜索,如果 a1 > b3,則意味著在數組的左側搜索,直到找到所需的條件。 ![](https://img.kancloud.cn/81/4f/814fc5cd2f8ec1c6695f949bb0268a9a_715x856.png) **為什么上述情況導致中位數?** 中位數是數組中(n + 1)/ 2 的最小元素,此處,中位數是兩個數組中(n + m + 1)/ 2 的最小元素。 如果上半部分中的所有元素都小于(或等于)下半部分中的所有元素,則在總數為奇數的情況下,只需計算上半部分的最后兩個元素之間的最大值(a2 和 b2 我們的示例),這將導致我們找到兩個數組中的(n + m +1)/ 2 個最小元素,即中位數((7 + 4 +1)/ 2 = 6)。 但是在總數為偶數的情況下,請計算前半部分中最后兩個元素的最大值(在我們的示例中為 a1 和 b2)之間的平均值及其在數組中的連續數,即第二個中前兩個元素的最小值 一半(在我們的示例中為 a2 和 b3)。 **分區的過程**: 要分成兩半,請進行分區,以使分區數組 A []的索引+分區數組 B []的索引等于元素總數 加一除以 2,即(n + m +1)/ 2(如果元素總數為奇數,則為+1)。 首先,定義兩個變量:min_index 和 max_index,并將 min_index 初始化為 0,并將 max_index 初始化為較小數組的長度。 在以下這些示例中,A []是較小的數組。 要對 A []進行分區,請使用公式(min_index + max_index)/ 2 并將其插入變量 i。 要對 B []進行分區,請使用公式(n + m + 1)/ 2 – i 并將其插入變量 j。 變量 i 表示要從 A []插入到前半部分的元素數,而 j 表示要從 B []插入到前半部分的元素數,其余的元素將被插入 進入下半場。 請看以下示例: **示例 1**: ![](https://img.kancloud.cn/94/4d/944d475ff2c881311500f9c58580ff1e_768x484.png) ![](https://img.kancloud.cn/be/72/be7276f2c1d72d563db52f5c15c4fa67_778x369.png) **示例 2** (此示例是指返回合并數組中存在的中值的條件)****: ![](https://img.kancloud.cn/fb/36/fb3653bc423dc17ad3d5311657195c7e_764x450.png) ![](https://img.kancloud.cn/31/b7/31b7ef238d5ef19d7597ab1b4336d98a_774x362.png) ![](https://img.kancloud.cn/0c/ff/0cff9ed2ab5e70d5f882c485537e0088_768x400.png) **以下是上述方法的實現**: ## C++ ```cpp // CPP code for median with case of returning? // double value when even number of elements are? // present in both array combinely #include<bits/stdc++.h> using std::cout; int maximum(int a, int b); int minimum(int a, int b); // Function to find median of two sorted arrays double findMedianSortedArrays(int *a, int n,? ??????????????????????????????int *b, int m) { ????int min_index = 0, max_index = n, i, j, median; ????while (min_index <= max_index) ????{ ????????i = (min_index + max_index) / 2; ????????j = ((n + m + 1) / 2) - i; ????????// If j is negative then the partition is not? ????????// possible having i elements from array i ????????if (j < 0) ????????{ ????????????max_index = i-1; ????????????continue; ????????} ????????// if i = n, it means that Elements from a[] in ????????// the second half is an empty set. and if j = 0, ????????// it means that Elements from b[] in the first ????????// half is an empty set. so it is necessary to ????????// check that, because we compare elements from ????????// these two groups.? ????????// Searching on right ????????if (i < n && j > 0 && b[j - 1] > a[i])???????? ????????????min_index = i + 1; ????????// if i = 0, it means that Elements from a[] in ????????// the first half is an empty set and if j = m, ????????// it means that Elements from b[] in the second ????????// half is an empty set. so it is necessary to ????????// check that, because we compare elements? ????????// from these two groups. ????????// searching on left ????????else if (i > 0 && j < m && b[j] < a[i - 1])???????? ????????????max_index = i - 1; ????????// we have found the desired halves. ????????else ????????{ ????????????// this condition happens when we don't have any ????????????// elements in the first half from a[] so we ????????????// returning the last element in b[] from? ????????????// the first half. ????????????if (i == 0)???????????? ????????????????median = b[j - 1];???????????? ????????????// and this condition happens when we don't ????????????// have any elements in the first half from ????????????// b[] so we returning the last element in? ????????????// a[] from the first half. ????????????else if (j == 0)???????????? ????????????????median = a[i - 1];???????????? ????????????else???????????? ????????????????median = maximum(a[i - 1], b[j - 1]);???????????? ????????????break; ????????} ????} ????// calculating the median. ????// If number of elements is odd there is? ????// one middle element. ????if ((n + m) % 2 == 1) ????????return (double)median; ????// Elements from a[] in the second half is an empty set.???? ????if (i == n) ????????return (median+b[j]) / 2.0; ????// Elements from b[] in the second half is an empty set. ????if (j == m) ????????return (median + a[i]) / 2.0; ????return (median + minimum(a[i], b[j])) / 2.0; } // Function to find max int maximum(int a, int b)? { ????return a > b ? a : b; } // Function to find minimum int minimum(int a, int b)? { ????return a < b ? a : b;? } // Driver code int main() { ????int a[] = {900}; ????int b[] = { 10, 13, 14}; ????int n = sizeof(a) / sizeof(int); ????int m = sizeof(b) / sizeof(int); ????// we need to define the smaller array as the? ????// first parameter to make sure that the? ????// time complexity will be O(log(min(n,m))) ????if (n < m) ????????cout << "The median is : " ?????????????<< findMedianSortedArrays(a, n, b, m); ????else ????????cout << "The median is : " ?????????????<< findMedianSortedArrays(b, m, a, n); ????return 0; } ``` ## Java ```java // Java code for median with? // case of returning double? // value when even number of? // elements are present in? // both array combinely import java.io.*; class GFG { ????static int []a = new int[]{900}; ????static int []b = new int[]{10, 13, 14}; ????// Function to find max ????static int maximum(int a, int b)? ????{ ????????return a > b ? a : b; ????} ????// Function to find minimum ????static int minimum(int a, int b)? ????{ ????????return a < b ? a : b;? ????} ????// Function to find median? ????// of two sorted arrays ????static double findMedianSortedArrays(int n,? ?????????????????????????????????????????int m) ????{ ????????int min_index = 0,? ????????????max_index = n, i = 0, ????????????j = 0, median = 0; ????????while (min_index <= max_index) ????????{ ????????????i = (min_index + max_index) / 2; ????????????j = ((n + m + 1) / 2) - i; ????????????// if i = n, it means that Elements? ????????????// from a[] in the second half is an? ????????????// empty set. and if j = 0, it means? ????????????// that Elements from b[] in the first ????????????// half is an empty set. so it is? ????????????// necessary to check that, because we ????????????// compare elements from these two? ????????????// groups. Searching on right ????????????if (i < n && j > 0 && b[j - 1] > a[i])????? ????????????????min_index = i + 1; ????????????// if i = 0, it means that Elements ????????????// from a[] in the first half is an? ????????????// empty set and if j = m, it means? ????????????// that Elements from b[] in the second ????????????// half is an empty set. so it is? ????????????// necessary to check that, because? ????????????// we compare elements from these two ????????????// groups. searching on left ????????????else if (i > 0 && j < m && b[j] < a[i - 1])????? ????????????????max_index = i - 1; ????????????// we have found the desired halves. ????????????else ????????????{ ????????????????// this condition happens when we? ????????????????// don't have any elements in the? ????????????????// first half from a[] so we ????????????????// returning the last element in? ????????????????// b[] from the first half. ????????????????if (i == 0)????????? ????????????????????median = b[j - 1];????????? ????????????????// and this condition happens when? ????????????????// we don't have any elements in the ????????????????// first half from b[] so we? ????????????????// returning the last element in? ????????????????// a[] from the first half. ????????????????else if (j == 0)????????? ????????????????????median = a[i - 1];????????? ????????????????else???? ????????????????????median = maximum(a[i - 1],? ?????????????????????????????????????b[j - 1]);????????? ????????????????break; ????????????} ????????} ????????// calculating the median. ????????// If number of elements is odd? ????????// there is one middle element. ????????if ((n + m) % 2 == 1) ????????????return (double)median; ????????// Elements from a[] in the? ????????// second half is an empty set.? ????????if (i == n) ????????????return (median + b[j]) / 2.0; ????????// Elements from b[] in the ????????// second half is an empty set. ????????if (j == m) ????????????return (median + a[i]) / 2.0; ????????return (median + minimum(a[i],? ?????????????????????????????????b[j])) / 2.0; ????} ????// Driver code ????public static void main(String args[]) ????{ ????????int n = a.length; ????????int m = b.length; ????????// we need to define the? ????????// smaller array as the? ????????// first parameter to? ????????// make sure that the ????????// time complexity will ????????// be O(log(min(n,m))) ????????if (n < m) ????????????System.out.print("The median is : " +? ???????????????????findMedianSortedArrays(n, m)); ????????else ????????????System.out.print("The median is : " +? ???????????????????findMedianSortedArrays(m, n)); ????} }? // This code is contributed by? // Manish Shaw(manishshaw1) ``` ## Python3 ```py # Python code for median with?? # case of returning double # value when even number? # of elements are present # in both array combinely median = 0 i = 0? j = 0 # def to find max def maximum(a, b) : ????return a if a > b else b # def to find minimum def minimum(a, b) : ????return a if a < b else b # def to find median # of two sorted arrays def findMedianSortedArrays(a, n, b, m) : ????global median, i, j ????min_index = 0? ????max_index = n? ????while (min_index <= max_index) : ????????i = int((min_index + max_index) / 2) ????????j = int(((n + m + 1) / 2) - i) ????????# if i = n, it means that? ????????# Elements from a[] in the ????????# second half is an empty? ????????# set. and if j = 0, it? ????????# means that Elements from? ????????# b[] in the first half is? ????????# an empty set. so it is? ????????# necessary to check that,? ????????# because we compare elements? ????????# from these two groups.? ????????# Searching on right ????????if (i < n and j > 0 and b[j - 1] > a[i]) : ????????????min_index = i + 1 ????????# if i = 0, it means that? ????????# Elements from a[] in the ????????# first half is an empty? ????????# set and if j = m, it means ????????# that Elements from b[] in? ????????# the second half is an empty? ????????# set. so it is necessary to ????????# check that, because we compare? ????????# elements from these two groups. ????????# searching on left ????????elif (i > 0 and j < m and b[j] < a[i - 1]) : ????????????max_index = i - 1 ????????# we have found the ????????# desired halves. ????????else : ????????????# this condition happens when? ????????????# we don't have any elements? ????????????# in the first half from a[]? ????????????# so we returning the last ????????????# element in b[] from the? ????????????# first half. ????????????if (i == 0) : ????????????????median = b[j - 1] ????????????# and this condition happens? ????????????# when we don't have any? ????????????# elements in the first half? ????????????# from b[] so we returning the? ????????????# last element in a[] from the? ????????????# first half. ????????????elif (j == 0) : ????????????????median = a[i - 1]????????? ????????????else : ????????????????median = maximum(a[i - 1], b[j - 1])? ????????????break ????# calculating the median. ????# If number of elements? ????# is odd there is? ????# one middle element. ????if ((n + m) % 2 == 1) : ????????return median ????# Elements from a[] in the? ????# second half is an empty set.? ????if (i == n) : ????????return ((median + b[j]) / 2.0) ????# Elements from b[] in the? ????# second half is an empty set. ????if (j == m) : ????????return ((median + a[i]) / 2.0) ????return ((median + minimum(a[i], b[j])) / 2.0) # Driver code a = [900] b = [10, 13, 14] n = len(a) m = len(b) # we need to define the? # smaller array as the? # first parameter to make? # sure that the time complexity # will be O(log(min(n,m))) if (n < m) : ????print ("The median is : {}".format(findMedianSortedArrays(a, n, b, m))) else : ????echo ("The median is : {}".format(findMedianSortedArrays(b, m, a, n))) # This code is contributed? # by Manish Shaw(manishshaw1) ``` ## C# ```cs // C# code for median with case of returning? // double value when even number of elements? // are present in both array combinely using System; class GFG { ????// Function to find max ????static int maximum(int a, int b)? ????{ ????????return a > b ? a : b; ????} ????// Function to find minimum ????static int minimum(int a, int b)? ????{ ????????return a < b ? a : b;? ????} ????// Function to find median of two sorted ????// arrays ????static double findMedianSortedArrays(ref int []a, ???????????????????????????int n, ref int []b, int m) ????{ ????????int min_index = 0, max_index = n, i = 0, ????????j = 0, median = 0; ????????while (min_index <= max_index) ????????{ ????????????i = (min_index + max_index) / 2; ????????????j = ((n + m + 1) / 2) - i; ????????????// if i = n, it means that Elements? ????????????// from a[] in the second half is an? ????????????// empty set. and if j = 0, it means? ????????????// that Elements from b[] in the first ????????????// half is an empty set. so it is? ????????????// necessary to check that, because we ????????????// compare elements from these two? ????????????// groups. Searching on right ????????????if (i < n && j > 0 && b[j - 1] > a[i])????? ????????????????min_index = i + 1; ????????????// if i = 0, it means that Elements ????????????// from a[] in the first half is an? ????????????// empty set and if j = m, it means? ????????????// that Elements from b[] in the second ????????????// half is an empty set. so it is? ????????????// necessary to check that, because? ????????????// we compare elements from these two ????????????// groups. searching on left ????????????else if (i > 0 && j < m && b[j] < a[i - 1])????? ????????????????max_index = i - 1; ????????????// we have found the desired halves. ????????????else ????????????{ ????????????????// this condition happens when we? ????????????????// don't have any elements in the? ????????????????// first half from a[] so we ????????????????// returning the last element in? ????????????????// b[] from the first half. ????????????????if (i == 0)????????? ????????????????????median = b[j - 1];????????? ????????????????// and this condition happens when? ????????????????// we don't have any elements in the ????????????????// first half from b[] so we? ????????????????// returning the last element in? ????????????????// a[] from the first half. ????????????????else if (j == 0)????????? ????????????????????median = a[i - 1];????????? ????????????????else???????? ????????????????????median = maximum(a[i - 1], b[j - 1]);????????? ????????????????break; ????????????} ????????} ????????// calculating the median. ????????// If number of elements is odd? ????????// there is one middle element. ????????if ((n + m) % 2 == 1) ????????????return (double)median; ????????// Elements from a[] in the second? ????????// half is an empty set.? ????????if (i == n) ????????????return (median+b[j]) / 2.0; ????????// Elements from b[] in the second? ????????// half is an empty set. ????????if (j == m) ????????????return (median + a[i]) / 2.0; ????????return (median + minimum(a[i], b[j])) / 2.0; ????} ????// Driver code ????static void Main() ????{ ????????int []a = new int[]{900}; ????????int []b = new int[]{ 10, 13, 14}; ????????int n = a.Length; ????????int m = b.Length; ????????// we need to define the smaller? ????????// array as the first parameter to? ????????// make sure that the time? ????????// complexity will be O(log(min(n,m))) ????????if (n < m) ????????????Console.Write("The median is : " ????????????+ findMedianSortedArrays(ref a, n,? ???????????????????????????????????ref b, m)); ????????else ????????????Console.Write("The median is : " ????????????+ findMedianSortedArrays(ref b, m,? ???????????????????????????????????ref a, n)); ????} } // This code is contributed by Manish Shaw // (manishshaw1) ``` ## PHP ```php <?php // PHP code for median with?? // case of returning double // value when even number? // of elements are present // in both array combinely $median = 0; $i = 0; $j = 0; // Function to find max function maximum($a, $b)? { ????return $a > $b ? $a : $b; } // Function to find minimum function minimum($a, $b)? { ????return $a < $b ? $a : $b;? } // Function to find median // of two sorted arrays function findMedianSortedArrays(&$a, $n,? ????????????????????????????????&$b, $m) { ????global $median, $i, $j; ????$min_index = 0;? ????$max_index = $n;? ????while ($min_index <= $max_index) ????{ ????????$i = intval(($min_index +? ?????????????????????$max_index) / 2); ????????$j = intval((($n + $m + 1) /? ???????????????????????????2) - $i); ????????// if i = n, it means that? ????????// Elements from a[] in the ????????// second half is an empty? ????????// set. and if j = 0, it? ????????// means that Elements from? ????????// b[] in the first half is? ????????// an empty set. so it is? ????????// necessary to check that,? ????????// because we compare elements? ????????// from these two groups.? ????????// Searching on right ????????if ($i < $n && $j > 0 &&? ????????????$b[$j - 1] > $a[$i])????? ????????????$min_index = $i + 1; ????????// if i = 0, it means that? ????????// Elements from a[] in the ????????// first half is an empty? ????????// set and if j = m, it means ????????// that Elements from b[] in? ????????// the second half is an empty? ????????// set. so it is necessary to ????????// check that, because we compare? ????????// elements from these two groups. ????????// searching on left ????????else if ($i > 0 && $j < $m &&? ?????????????????$b[$j] < $a[$i - 1])????? ????????????$max_index = $i - 1; ????????// we have found the ????????// desired halves. ????????else ????????{ ????????????// this condition happens when? ????????????// we don't have any elements? ????????????// in the first half from a[]? ????????????// so we returning the last ????????????// element in b[] from the? ????????????// first half. ????????????if ($i == 0)? ????????????????$median = $b[$j - 1]; ????????????// and this condition happens? ????????????// when we don't have any? ????????????// elements in the first half? ????????????// from b[] so we returning the? ????????????// last element in a[] from the? ????????????// first half. ????????????else if ($j == 0)????????? ????????????????$median = $a[$i - 1];????????? ????????????else???????? ????????????????$median = maximum($a[$i - 1],? ??????????????????????????????????$b[$j - 1]);? ????????????break; ????????} ????} ????// calculating the median. ????// If number of elements? ????// is odd there is? ????// one middle element. ????if (($n + $m) % 2 == 1) ????????return $median; ????// Elements from a[] in the? ????// second half is an empty set.? ????if ($i == $n) ????????return (($median +? ?????????????????$b[$j]) / 2.0); ????// Elements from b[] in the? ????// second half is an empty set. ????if ($j == $m) ????????return (($median +? ?????????????????$a[$i]) / 2.0); ????return (($median +? ?????????????minimum($a[$i],? ?????????????????????$b[$j])) / 2.0); } // Driver code $a = array(900); $b = array(10, 13, 14); $n = count($a); $m = count($b); // we need to define the? // smaller array as the? // first parameter to make? // sure that the time complexity // will be O(log(min(n,m))) if ($n < $m) ????echo ("The median is : " .? ???????????findMedianSortedArrays($a, $n,? ??????????????????????????????????$b, $m)); else ????echo ("The median is : " .? ???????????findMedianSortedArrays($b, $m,? ??????????????????????????????????$a, $n)); // This code is contributed? // by Manish Shaw(manishshaw1) ?> ``` **輸出**: ``` The median is : 13.5 ``` **另一種方法**:相同的程序,但是返回合并數組中存在的中位數((n + m)/ 2-1 位置): ## C++ ``` // CPP code for finding median of the given two // sorted arrays that exists in the merged array ((n+m) / 2 - 1 position) #include<bits/stdc++.h> using std::cout; int maximum(int a, int b); // Function to find median of given two sorted arrays int findMedianSortedArrays(int *a, int n,? ???????????????????????????int *b, int m) { ????int min_index = 0, max_index = n, i, j; ????while (min_index <= max_index) ????{ ????????i = (min_index + max_index) / 2; ????????j = ((n + m + 1) / 2) - i; ????????// if i = n, it means that Elements from a[] in ????????// the second half is an empty set. If j = 0, it ????????// means that Elements from b[] in the first half ????????// is an empty set. so it is necessary to check that, ????????// because we compare elements from these two groups. ????????// searching on right ????????if (i < n && j > 0 && b[j - 1] > a[i])???????? ????????????min_index = i + 1;???????? ????????// if i = 0, it means that Elements from a[] in the ????????// first half is an empty set and if j = m, it means ????????// that Elements from b[] in the second half is an ????????// empty set. so it is necessary to check that,? ????????// because we compare elements from these two groups. ????????// searching on left ????????else if (i > 0 && j < m && b[j] < a[i - 1])???????? ????????????max_index = i - 1;???????? ????????// we have found the desired halves. ????????else ????????{ ????????????// this condition happens when we don't have ????????????// any elements in the first half from a[] so ????????????// we returning the last element in b[] from ????????????// the first half. ????????????if (i == 0)???????????? ????????????????return b[j - 1];???????????? ????????????// and this condition happens when we don't have any? ????????????// elements in the first half from b[] so we? ????????????// returning the last element in a[] from the first half. ????????????if (j == 0)???????????? ????????????????return a[i - 1];???????????? ????????????else???????????? ????????????????return maximum(a[i - 1], b[j - 1]);??????????? ????????} ????} ????cout << "ERROR!!! " << "returning\n";???? ????return 0; } // Function to find maximum int maximum(int a, int b)? { ????return a > b ? a : b;? } // Driver code int main() { ????int a[] = {900}; ????int b[] = { 10,13,14}; ????int n = sizeof(a) / sizeof(int); ????int m = sizeof(b) / sizeof(int); ????// we need to define the smaller array as the first? ????// parameter to make sure that the time complexity? ????// will be O(log(min(n,m))) ????if (n < m) ????????cout << "The median is: " ?????????????<< findMedianSortedArrays(a, n, b, m); ????else ????????cout << "The median is: "? ?????????????<< findMedianSortedArrays(b, m, a, n); ????return 0; } ``` ## C# ``` // C# code for finding median? // of the given two sorted? // arrays that exists in the? // merged array ((n+m) / 2 - // 1 position) using System; class GFG {? ????// Function to find maximum ????static int maximum(int a, ???????????????????????int b)? ????{ ????????return a > b ? a : b;? ????}????? ????// Function to find median? ????// of given two sorted arrays ????static int findMedianSortedArrays(ref int []a, int n,? ??????????????????????????????????????ref int []b, int m) ????{ ????????int min_index = 0,? ????????????max_index = n, i, j; ????????while (min_index <= max_index) ????????{ ????????????i = (min_index + max_index) / 2; ????????????j = ((n + m + 1) / 2) - i; ????????????// if i = n, it means that? ????????????// Elements from a[] in the? ????????????// second half is an empty? ????????????// set. If j = 0, it means? ????????????// that Elements from b[]? ????????????// in the first half is an? ????????????// empty set. so it is? ????????????// necessary to check that, ????????????// because we compare elements? ????????????// from these two groups.? ????????????// searching on right ????????????if (i < n && j > 0 &&? ????????????????b[j - 1] > a[i])????? ????????????????min_index = i + 1;????? ????????????// if i = 0, it means that? ????????????// Elements from a[] in the ????????????// first half is an empty set? ????????????// and if j = m, it means that ????????????// Elements from b[] in the? ????????????// second half is an empty set. ????????????// so it is necessary to check? ????????????// that, because we compare? ????????????// elements from these two? ????????????// groups. searching on left ????????????else if (i > 0 && j < m &&? ?????????????????????b[j] < a[i - 1])????? ????????????????max_index = i - 1;????? ????????????// we have found the ????????????// desired halves. ????????????else ????????????{ ????????????????// this condition happens? ????????????????// when we don't have any? ????????????????// elements in the first? ????????????????// half from a[] so we? ????????????????// returning the last? ????????????????// element in b[] from ????????????????// the first half. ????????????????if (i == 0)????????? ????????????????????return b[j - 1];????????? ????????????????// and this condition happens? ????????????????// when we don't have any? ????????????????// elements in the first half? ????????????????// from b[] so we returning the ????????????????// last element in a[] from the ????????????????// first half. ????????????????if (j == 0)????????? ????????????????????return a[i - 1];????????? ????????????????else???????? ????????????????????return maximum(a[i - 1], ???????????????????????????????????b[j - 1]);????????? ????????????} ????????} ????????Console.Write ("ERROR!!! " +? ???????????????????????"returning\n");? ????????return 0; ????}????? ????// Driver code ????static void Main() ????{ ????????int []a = new int[]{900}; ????????int []b = new int[]{10, 13, 14}; ????????int n = a.Length; ????????int m = b.Length; ????????// we need to define the smaller? ????????// array as the first parameter? ????????// to make sure that the time? ????????// complexity will be O(log(min(n,m))) ????????if (n < m) ????????????Console.Write ("The median is: " + ????????????????????????????findMedianSortedArrays(ref a, n,? ???????????????????????????????????????????????????ref b, m)); ????????else ????????????Console.Write ("The median is: " +? ????????????????????????????findMedianSortedArrays(ref b, m,? ???????????????????????????????????????????????????ref a, n)); ????} } // This code is contributed by? // Manish Shaw(manishshaw1) ``` ## Python 3 ``` # Python 3 code for finding median? # of the given two sorted arrays that? # exists in the merged array? # ((n+m) / 2 - 1 position) # Function to find median of given # two sorted arrays def findMedianSortedArrays(a, n, b, m): ????min_index = 0 ????max_index = n ????while (min_index <= max_index): ????????i = (min_index + max_index) // 2 ????????j = ((n + m + 1) // 2) - i ????????# if i = n, it means that Elements? ????????# from a[] in the second half is? ????????# an empty set. If j = 0, it means? ????????# that Elements from b[] in the first? ????????# half is an empty set. so it is necessary? ????????# to check that, because we compare elements? ????????# from these two groups. searching on right ????????if (i < n and j > 0 and b[j - 1] > a[i]):????? ????????????min_index = i + 1???? ????????# if i = 0, it means that Elements from? ????????# a[] in the first half is an empty set? ????????# and if j = m, it means that Elements? ????????# from b[] in the second half is an ????????# a[] in the first half is an empty set?? ????????# that, because we compare elements from? ????????# these two groups. searching on left ????????elif (i > 0 and j < m and b[j] < a[i - 1]):????? ????????????max_index = i - 1???? ????????# we have found the desired halves. ????????else: ????????????# this condition happens when we don't have ????????????# any elements in the first half from a[] so ????????????# we returning the last element in b[] from ????????????# the first half. ????????????if (i == 0):? ????????????????return b[j - 1]????????? ????????????# and this condition happens when we don't? ????????????# have any elements in the first half from? ????????????# b[] so we returning the last element in ????????????# a[] from the first half. ????????????if (j == 0):? ????????????????return a[i - 1]????????? ????????????else: ????????????????return maximum(a[i - 1], b[j - 1]) ????print("ERROR!!! " , "returning\n")? ????return 0 # Function to find maximum def maximum(a, b) : ????return a if a > b else b # Driver code if __name__ == "__main__": ????a = [900] ????b = [10, 13, 14] ????n = len(a) ????m = len(b) ????# we need to define the smaller array? ????# as the first parameter to make sure? ????# that the time complexity will be? ????# O(log(min(n,m))) ????if (n < m): ????????print( "The median is: ",??? ????????????????findMedianSortedArrays(a, n, b, m)) ????else: ????????print( "The median is: ",? ????????????????findMedianSortedArrays(b, m, a, n)) # This code is contributed # by ChitraNayal ``` **輸出**: ``` The median is: 13 ``` **時間復雜度**: O(log(min(n(m,m)))),其中 n 和 m 是給定排序數組的大小 * * * * * *
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看