📌  相关文章
📜  两个大小不同的排序数组的中位数|套装1(线性)

📅  最后修改于: 2021-04-26 08:00:56             🧑  作者: Mango

这是相等大小问题的两个排序数组的中位数的扩展。在这里,我们也处理大小不等的数组。

例子:

Input: a[] = {1, 12, 15, 26, 38}
       b[] = {2, 13, 24}
Output: 14
Explanation:
After merging arrays the result is
1, 2, 12, 13, 15, 24, 26, 38
median = (13 + 15)/2 = 14.

Input: a[] = {1, 3, 5, 7}
       b[] = {2, 4, 6, 8}
Output: 5
Explanation : 
After merging arrays the result is
1, 2, 3, 4, 5, 6, 7, 8, 9
median = 5 

方法:

  1. 本文讨论的方法类似于中位数相等的方法1。使用合并排序的合并过程。
  2. 比较两个数组的元素时,请保留一个变量以跟踪计数。
  3. 执行两个排序数组的合并。插入新元素时,增加count的值。
  4. 合并两个数组的最有效方法是比较两个数组的顶部并弹出较小的值并增加计数。
  5. 继续比较两个数组的前/后元素,直到任何数组中都没有剩余元素
  6. 现在要找到中位数,有两种情况需要处理。
    • 如果数组大小长度的总和为偶数,则在合并数组中计数为((n1 + n2)/ 2)-1和(n1 + n2)/ 2的值时存储元素,然后将元素相加并除以2返回该值作为中位数。
    • 如果值(大小的总和)为奇数,则返回第(n1 + n2)/ 2个元素(当count的值为(n1 + n2)/ 2)作为中值。
C++
// A C++ program to find median of two sorted arrays of
// unequal sizes
#include 
using namespace std;
  
// A utility function to find median of two integers
/* This function returns median of a[] and b[].
Assumptions in this function: Both a[] and b[]
are sorted arrays  */
float findmedian(int a[], int n1, int b[], int n2)
{
    int i = 0; /* Current index of
             i/p array a[] */
    int j = 0; /* Current index of
                  i/p array b[] */
    int k;
    int m1 = -1, m2 = -1;
    for (k = 0; k <= (n1 + n2) / 2; k++) {
  
        if (i < n1 && j < n2) {
            if (a[i] < b[j]) {
                m2 = m1;
                m1 = a[i];
                i++;
            }
            else {
                m2 = m1;
                m1 = b[j];
                j++;
            }
        }
  
        /* Below is to handle the case where
           all elements of a[] are
           smaller than smallest(or first)
           element of b[] or a[] is empty*/
        else if (i == n1) {
            m2 = m1;
            m1 = b[j];
            j++;
        }
  
        /* Below is to handle case where
           all elements of b[] are
           smaller than smallest(or first)
           element of a[] or b[] is empty*/
        else if (j == n2) {
            m2 = m1;
            m1 = a[i];
            i++;
        }
    }
  
    /*Below is to handle the case where
     sum of number of elements 
     of the arrays is even */
    if ((n1 + n2) % 2 == 0)
        return (m1 + m2) * 1.0 / 2;
  
    /* Below is to handle the case where
       sum of number of elements 
       of the arrays is odd */
    return m1;
}
  
// Driver program to test above functions
int main()
{
    int a[] = { 1, 12, 15, 26, 38 };
    int b[] = { 2, 13, 24 };
  
    int n1 = sizeof(a) / sizeof(a[0]);
    int n2 = sizeof(b) / sizeof(b[0]);
  
    printf("%f", findmedian(a, n1, b, n2));
  
    return 0;
}


Java
// A Java program to find median of two sorted arrays of
// unequal sizes
  
import java.io.*;
  
class GFG {
  
    // A utility function to find median of two integers
    /* This function returns median of a[] and b[].
Assumptions in this function: Both a[] and b[]
are sorted arrays */
    static float findmedian(int a[], int n1, int b[], int n2)
    {
        int i = 0; /* Current index of
            i/p array a[] */
        int j = 0; /* Current index of
                i/p array b[] */
        int k;
        int m1 = -1, m2 = -1;
        for (k = 0; k <= (n1 + n2) / 2; k++) {
  
            if (i < n1 && j < n2) {
                if (a[i] < b[j]) {
                    m2 = m1;
                    m1 = a[i];
                    i++;
                }
                else {
                    m2 = m1;
                    m1 = b[j];
                    j++;
                }
            }
  
            /* Below is to handle the case where
        all elements of a[] are
        smaller than smallest(or first)
        element of b[] or a[] is empty*/
            else if (i == n1) {
                m2 = m1;
                m1 = b[j];
                j++;
            }
  
            /* Below is to handle case where
        all elements of b[] are
        smaller than smallest(or first)
        element of a[] or b[] is empty*/
            else if (j == n2) {
                m2 = m1;
                m1 = a[i];
                i++;
            }
        }
  
        /*Below is to handle the case where
    sum of number of elements 
    of the arrays is even */
        if ((n1 + n2) % 2 == 0) {
            return (m1 + m2) * (float)1.0 / 2;
        }
        /* Below is to handle the case where
    sum of number of elements 
    of the arrays is odd */
        return m1;
    }
  
    // Driver program to test above functions
    public static void main(String[] args)
    {
        int a[] = { 1, 12, 15, 26, 38 };
        int b[] = { 2, 13, 24 };
  
        int n1 = a.length;
        int n2 = b.length;
  
        System.out.println(findmedian(a, n1, b, n2));
    }
}
  
// This code has been contributed by inder_verma.


Python 3
# Python 3 program to find median of 
# two sorted arrays of unequal sizes
  
# A utility function to find median 
# of two integers
''' This function returns median of 
a[] and b[]. Assumptions in this 
function: Both a[] and b[] are sorted
arrays '''
  
def findmedian(a, n1, b, n2):
  
    i = 0 # Current index of i / p array a[] 
    j = 0 # Current index of i / p array b[] 
  
    m1 = -1
    m2 = -1
    for k in range(((n1 + n2) // 2) + 1) :
  
        if (i < n1 and j < n2) :
            if (a[i] < b[j]) :
                m2 = m1
                m1 = a[i]
                i += 1
              
            else :
                m2 = m1
                m1 = b[j]
                j += 1
  
        # Below is to handle the case where
        # all elements of a[] are
        # smaller than smallest(or first)
        # element of b[] or a[] is empty
              
        elif(i == n1) :
            m2 = m1
            m1 = b[j]
            j += 1
              
        # Below is to handle case where
        # all elements of b[] are
        # smaller than smallest(or first)
        # element of a[] or b[] is empty 
        elif (j == n2) :
            m2 = m1
            m1 = a[i]
            i += 1
  
    '''Below is to handle the case 
    where sum of number of elements 
    of the arrays is even '''
    if ((n1 + n2) % 2 == 0):
        return (m1 + m2) * 1.0 / 2
  
    ''' Below is to handle the case 
    where sum of number of elements 
    of the arrays is odd '''
    return m1
  
# Driver Code
if __name__ == "__main__":
      
    a = [ 1, 12, 15, 26, 38 ]
    b = [ 2, 13, 24 ]
  
    n1 = len(a)
    n2 = len(b)
  
    print(findmedian(a, n1, b, n2))
  
# This code is contributed 
# by ChitraNayal


C#
// A C# program to find median
// of two sorted arrays of
// unequal sizes
using System;
  
class GFG {
  
    // A utility function to find
    // median of two integers
    /* This function returns 
   median of a[] and b[].
   Assumptions in this 
   function: Both a[] and b[]
   are sorted arrays */
    static float findmedian(int[] a, int n1,
                            int[] b, int n2)
    {
        int i = 0; /* Current index of
              i/p array a[] */
        int j = 0; /* Current index of
              i/p array b[] */
        int k;
        int m1 = -1, m2 = -1;
        for (k = 0; k <= (n1 + n2) / 2; k++) {
            if (i < n1 && j < n2) {
                if (a[i] < b[j]) {
                    m2 = m1;
                    m1 = a[i];
                    i++;
                }
                else {
                    m2 = m1;
                    m1 = b[j];
                    j++;
                }
            }
  
            /* Below is to handle the case where
    all elements of a[] are
    smaller than smallest(or first)
    element of b[] or a[] is empty*/
            else if (i == n1) {
                m2 = m1;
                m1 = b[j];
                j++;
            }
  
            /* Below is to handle case where
    all elements of b[] are
    smaller than smallest(or first)
    element of a[] or b[] is empty*/
            else if (j == n2) {
                m2 = m1;
                m1 = a[i];
                i++;
            }
        }
  
        /*Below is to handle the case where
sum of number of elements 
of the arrays is even */
        if ((n1 + n2) % 2 == 0) {
            return (m1 + m2) * (float)1.0 / 2;
        }
        /* Below is to handle the case 
where sum of number of elements 
of the arrays is odd */
        return m1;
    }
  
    // Driver Code
    public static void Main()
    {
        int[] a = { 1, 12, 15, 26, 38 };
        int[] b = { 2, 13, 24 };
  
        int n1 = a.Length;
        int n2 = b.Length;
  
        Console.WriteLine(findmedian(a, n1, b, n2));
    }
}
  
// This code is contributed
// by Subhadeep Gupta


PHP


输出:
14.000000

复杂度分析:

  • 时间复杂度:O(n),两个列表都需要遍历,因此时间复杂度为O(n)。
  • 辅助空间: O(1),不需要额外的空间。

效率更高(日志时间复杂度方法)

  1. 两个大小不同的排序数组的中位数。
  2. 以min(Log(n1,n2))为单位的两个大小不同的排序数组的中位数