📜  求两个给定向量的中位数

📅  最后修改于: 2022-05-13 01:56:07.373000             🧑  作者: Mango

求两个给定向量的中位数

给定两个不同大小的向量ab ,其中数组 a 有m个元素,数组 b 有n个元素。他们的任务是找到两个向量的中值。这个问题是两个不同大小的排序数组的中位数问题的扩展。

例子:

方法:

  1. 初始化向量 a。
  2. 初始化向量 b。
  3. 创建一个大小为 a + b的新向量。
  4. 使用循环迭代第一个向量并将数据存储到新创建的向量中,并且在迭代第一个向量后对第二个向量进行类似的操作。
  5. 使用 merge() STL函数将两个排序后的向量合并到新创建的向量中。
  6. 找到偶数和奇数大小的中位数并将其返回。

以下是上述方法的 C++ 实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to calculate the median
double findMedianSortedVectors(vector& a,
                               vector& b)
{
    // New Vector of size a + b
    vector c(a.size() + b.size());
    int k = 0;
    double median = 0;
    int size_a = a.size();
    int size_b = b.size();
    for (int i = 0; i < size_a; i++)
    {
        // Store data of first vector
        // in new vector
        c[k++] = a[i];
    }
    for (int i = 0; i < size_b; i++)
    {
        // Store second vector in
        // vector c
        c[k++] = b[i];
    }
 
    merge(a.begin(), a.end(),
          b.begin(), b.end(), c.begin());
 
    // Merge the both sorted vectors
    int n = c.size();
    if (n % 2 == 0)
    {
        // Calculate median for even
        // size vector
        median = c[(n / 2) - 1] + c[n / 2];
        median = median / 2;
    }
    else
    {
        // Calculate median for odd
        // size vector
        median = c[(n - 1) / 2];
    }
    return median;
}
 
// Driver code
int main()
{
    vector v1;
    vector v2;
 
    // Initialize first vector
    v1.push_back(1);
    v1.push_back(4);
 
    // Initialize second vector
    v2.push_back(2);
 
    // Invoke function to calculate
    // median
    double median_vectors =
    findMedianSortedVectors(v1, v2);
 
    // Print median value
    cout << median_vectors << endl;
    return 0;
}


Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.Collections;
import java.util.Vector;
 
class GFG {
  public static double
    findMedianSortedVectors(Vector a,
                            Vector b)
  {
    // New Vector of size a + b
    Vector c = new Vector();
    double median = 0;
    int size_a = a.size();
    int size_b = b.size();
    for (int i = 0; i < size_a; i++) {
      // Store data of first vector
      // in new vector
      c.add(a.get(i));
    }
    for (int i = 0; i < size_b; i++) {
      // Store second vector in
      // vector c
      c.add(b.get(i));
    }
    // sort all the values
    Collections.sort(c);
 
    // Merge the both sorted vectors
    int n = c.size();
    if (n % 2 == 0) {
      // Calculate median for even
      // size vector
      median = c.get((n / 2) - 1) + c.get(n / 2);
      median = median / 2;
    }
    else {
      // Calculate median for odd
      // size vector
      median = c.get((n - 1) / 2);
    }
    return median;
  }
  public static void main(String[] args)
  {
    Vector v1 = new Vector();
    Vector v2 = new Vector();
 
    // Initialize first vector
    v1.add(1);
    v1.add(4);
 
    // Initialize second vector
    v2.add(2);
 
    // Invoke function to calculate
    // median
    double median_vectors
      = findMedianSortedVectors(v1, v2);
 
    // Print median value
    System.out.println(median_vectors);
  }
}
 
// This code is contributed by rj13to.


Python3
# python program to implement
# the above approach
 
# Function to calculate the median
 
 
def findMedianSortedVectors(a, b):
    # New list
    c = []
    for i in range(0, len(a)):
        c.append(0)
    for i in range(0, len(b)):
        c.append(0)
    k = 0
    median = 0
    size_a = len(a)
    size_b = len(b)
 
    for i in range(0, size_a):
        # Store data of first list
        # in new list
        c[k] = a[i]
        k += 1
    for i in range(0, size_b):
        # Store second list in
        #  c
        c[k] = b[i]
        k += 1
    # sort the new list
    c.sort()
 
    n = len(c)
    if (n % 2 == 0):
        # Calculate median for even
        # size list
        median = c[(n // 2) - 1] + c[n // 2]
        median = median / 2
    else:
        # Calculate median for odd
        # size list
        median = c[(n - 1) // 2]
    return median
 
# Driver Code
 
# Initialize first list
v1 = [1, 4]
 
# Initialize second list
v2 = [2]
 
# Invoke function to calculate
# median
median_lists = findMedianSortedVectors(v1, v2)
 
# Print median value
print(median_lists)
 
# This code is contributed by rj13to.


C#
// C# program for the above approach
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG
{
  public static double
    findMedianSortedVectors(ArrayList a,
                            ArrayList b)
  {
    // New Vector of size a + b
    ArrayList c = new ArrayList();
    double median = 0;
    int size_a = a.Count;
    int size_b = b.Count;
    for (int i = 0; i < size_a; i++) {
      // Store data of first vector
      // in new vector
      c.Add(a[i]);
    }
    for (int i = 0; i < size_b; i++) {
      // Store second vector in
      // vector c
      c.Add(b[i]);
    }
    // sort all the values
    c.Sort();
 
    // Merge the both sorted vectors
    int n = c.Count;
    if (n % 2 == 0) {
      // Calculate median for even
      // size vector
      median = (int)c[(n / 2) - 1] + (int)c[n / 2];
      median = median / 2;
    }
    else {
      // Calculate median for odd
      // size vector
      median = (int)c[(n - 1) / 2];
    }
    return median;
  }
  public static void Main()
  {
    ArrayList v1 = new ArrayList();
    ArrayList v2 = new ArrayList();
 
    // Initialize first vector
    v1.Add(1);
    v1.Add(4);
 
    // Initialize second vector
    v2.Add(2);
 
    // Invoke function to calculate
    // median
    double median_vectors
      = findMedianSortedVectors(v1, v2);
 
    // Print median value
    Console.WriteLine(median_vectors);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
2

复杂:

时间复杂度:需要 O(m + n) 时间来合并两个向量 O(m+n) 时间。
空间复杂度: O(1),因为不需要额外的空间。