2个不同长度的排序数组的中位数 [英] Median of 2 sorted arrays of different lengths
问题描述
如何找到两个分别为长度m和n的排序数组A和B的中值. 我已经搜索过,但是大多数算法都假设两个数组的大小相同.我想知道如果m!= n我们怎么找到中位数 考虑例子, A = {1、3、5、7、11、15},其中m = 6 B = {2,4,8,12,14},其中n = 5 中位数是7
How can one find a median of 2 sorted arrays A and B which are of length m and n respectively. I have searched, but most the algorithms assume that both arrays are of same size. I want to know how can we find median if m != n consider example, A={1, 3, 5, 7, 11, 15} where m = 6, B={2, 4, 8, 12, 14} where n = 5 and the median is 7
感谢您的帮助. 我正在准备面试,并且现在正在努力使用这种算法.
Any help is appreciated. I am preparing for interviews and i am struggling with this algo right now.
推荐答案
这是JAVA代码,用于查找长度不等的两个排序数组的中位数
Here is the JAVA code to find the median of two sorted arrays of unequal length
package FindMedianBetween2SortedArraysOfUnequalLength;
import java.util.Arrays;
import java.util.Scanner;
public class UsingKthSmallestElementLogic {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
try{
System.out.println("Enter the number of elements in the first SORTED array");
int n = in.nextInt();
int[] array1 = new int[n];
System.out.println("Enter the elements of the first SORTED array");
for(int i=0;i<n;i++)
array1[i]=in.nextInt();
System.out.println("Enter the number of elements in the second SORTED array");
int m = in.nextInt();
int[] array2 = new int[m];
System.out.println("Enter the elements of the second SORTED array");
for(int i=0;i<m;i++)
array2[i]=in.nextInt();
System.out.println("Median of the two SORTED arrays is: "+findMedian(array1,array2,array1.length,array2.length));
}
finally{
in.close();
}
}
private static int findMedian(int[] a, int[] b,
int aLength, int bLength) {
int left = (aLength+bLength+1)>>1;
int right = (aLength+bLength+2)>>1;
return ((findKthSmallestElement(a,b,a.length,b.length,left)+findKthSmallestElement(a,b,a.length,b.length,right))/2);
}
private static int findKthSmallestElement(int[] a, int[] b,
int aLength, int bLength, int k) { // All the 5 parameters passed are VERY VERY IMP
/* to maintain uniformity, we will assume that size_a is smaller than size_b
else we will swap array in call :) */
if(aLength>bLength)
return findKthSmallestElement(b, a, bLength, aLength, k);
/* We have TWO BASE CASES
* Now case when size of smaller array is 0 i.e there is no elemt in one array*/
//BASE CASE 1. If the smallest array length is 0
if(aLength == 0 && bLength > 0)
return b[k-1]; // due to zero based index
/* case where k==1 that means we have hit limit */
//BASE CASE 2. If k==1
if(k==1)
return Math.min(a[0], b[0]);
/* Now the divide and conquer part */
int i = Math.min(aLength, k/2) ; // k should be less than the size of array
int j = Math.min(bLength, k/2) ; // k should be less than the size of array
if(a[i-1] > b[j-1])
// Now we need to find only K-j th element
return findKthSmallestElement(a, Arrays.copyOfRange(b, j, b.length), a.length, b.length -j, k-j);
else
return findKthSmallestElement(Arrays.copyOfRange(a, i, a.length), b, a.length-i, b.length, k-i);
}
}
/*
Analysis:
Time Complexity = O(log(n+m))
Space Complexity = O(1)*/
这篇关于2个不同长度的排序数组的中位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!