We have $k$ blocks of integer sequences $B\_1,\dots,B\_k$ where $B\_i$ is a sequence $$a\_{i,1},\dots,a\_{i,n\_i}$$ with $a\_{i,j}\leq a\_{i,j+1}$.


Denote the permutation matrix $M\_{\ell,\ell'}$ that merges $B\_\ell$ and $B\_{\ell'}$ (there are more than one sometimes).


Assume for every pair $\ell,\ell'$ in $\{1,\dots,k\}$ we know at least one permutation matrix $M\_{\ell,\ell'}$ that merges $B\_\ell$ and $B\_{\ell'}$.


From the $k(k-1)/2$ matrices it is possible to find the global permutation matrix $M$ that merges all the sequences. However it seems not all $k(k-1)/2$ matrices are needed.


$M$ is $n\times n$ permutation matrix where $n=n\_1+\dots+n\_k$. $M$ sorts every integer in all sequences. It is technically not full sorting here as all blocks are partially sorted. So we are doing merge like in mergesort. Thus I call this matrix $M$ merges all the blocks just as $M\_{\ell,\ell'}$ merges blocks $B\_\ell$ and $B\_{\ell'}$ in sorted order. $M\_{\ell,\ell'}$ is permutation matrix of size $n\_{\ell,\ell'}\times n\_{\ell,\ell'}$ where $n\_{\ell,\ell'}=n\_\ell+n\_{\ell'}$.



> 
> 1. Is it possible that only $O(k\log k)$ or may be just $o(k\log k)$ many $M\_{\ell,\ell'}$ matrices suffice and is there a canonical construction of global $M$ from such minimal information? Or do we need $\Omega(k\log k)$ many $M\_{ij}$?
> 2. Is the central case of $n\_1=\dots=n\_k=\frac{n\_1+\dots+n\_k}k$ any different?
> 
> 
> 



Minimum local permutation data needed to globally merge locally sorted sequences?
1) *Is it possible to choose $O(k\log k)$ matrices beforehand that will allow you to construct the global matrix M (sort all the elements of the $a\_{i, j}$) regardless of the values of $a\_{i, j}$?*


**No.**


Even in the case $n\_1 = n\_2 = \ldots = n\_k = 1$ (so in the setting of the usual sorting problem) if we don't know the order between two elements chosen beforehand we might not be able to sort all the numbers (for instance, those two elements may be the two least numbers in the set).


2)*Is it possible to make an algorithm which sorts the numbers by using only $O(k\log k)$ matrices (but we don't know which ones)? Such an algorithm would be a generalization of the usual quick sorting algorithms.*


**In general, also no.**


To see that, fix $k$, let $m = n\_1 = n\_2 = \ldots = n\_k$ and suppose that each $B\_i$ consists of $m$ numbers chosen (uniformly) randomly from $[0, 1].$ Assume that you've already sorted $A = B\_3 \cup B\_4 \cup \ldots \cup B\_k$ and you also know how to merge $A$ with both $B\_1$ and $B\_2$ separately (in other words you know the odrer in $A \cup B\_1$ and $A \cup B\_2$). I claim that for $m >> k$ that doesn't determine the order on $A \cup B\_1 \cup B\_2$ which would imply that you need matrix $M\_{1,2}$. Indeed, the order is determined if and only if for every pair of elements $b\_1 \in B\_1, b\_2 \in B\_2$ there is $a \in A$ such that either
$$b\_1 \le a \le b\_2$$
or 
$$b\_1 \ge a \ge b\_2.$$
Now, as $m$ tends to infinity, the probability of this event is easily seen to be going to $0$ (since $\frac{|A|}{|B\_1|} = \frac{|A|}{|B\_2|} = k-2$). Therefore, there are some configurations where you need matrix $M\_{1, 2}$ to sort all the elements. Moreover, if the probability of needing each specific matrix $M\_{i, j}$ is at least $1-\epsilon$, then the probability of needing all the matrices is at least $1 - k(k-1)\epsilon$. That implies that for large $m$ you actually need all the matrices $M\_{i, j}$ with high probability.


3)*What would be the best way to sort a set of elements $B\_1 \cup \ldots \cup B\_k$ given that each $B\_i$ is already sorted?*


Probably by applying the regular MergeSort algorithm to this array. It works by sorting parts of the array and then merging them together which plays nicely with the fact that you already have $B\_i$'s sorted.


