I am looking for an algorithm to merge two sorted lists, but they lack a comparison operator between elements of one list and elements of the other. The resulting merged list may not be unique, but any result which satisfies the relative sort order of each list will do. More precisely:
Given:
- Lists A =
{a_1, ..., a_m}
, and B ={b_1, ..., b_n}
. (They may be considered sets, as well). - A precedence operator
<
defined among elements of each list such thata_i < a_{i+1}
, andb_j < b_{j+1}
for1 <= i <= m
and1 <= j <= n
. - The precedence operator is undefined between elements of A and B:
a_i < b_j
is not defined for any validi
andj
. - An equality operator
=
defined among all elements of either A or B (it is defined between an element from A and an element from B). - No two elements from list A are equal, and the same holds for list B.
Produce:
A list C = {c_1, ..., c_r}
such that:
C = union(A, B)
; the elements of C are the union of elements from A and B.- If
c_p = a_i
,c_q = a_j
, anda_i < a_j
, thenc_p < c_q
. (The order of elements of the sublists of C corresponding to sets A and B should be preserved. - There exist no
i
andj
such thatc_i = c_j
. (all duplicated elements between A and B are removed).
I hope this question makes sense and that I'm not asking something either terribly obvious, or something for which there is no solution.
Context:
A constructible number can be represented exactly in finitely many quadratic extensions to the field of rational numbers (using a binary tree of height equal to the number of field extensions). A representation of a constructible number must therefore "know" the field it is represented in. Lists A and B represent successive quadratic extensions of the rational numbers. Elements of A and B themselves are constructible numbers, which are defined in the context of previous smaller fields (hence the precedence operator). When adding/multiplying constructible numbers, the quadratically extended fields must first be merged so that the binary arithmetic operations can be performed; the resulting list C is the quadratically extended field which can represent numbers representable by both fields A and B. (If anyone has a better idea of how to programmatically work with constructible numbers, let me know. A question concerning constructible numbers has arisen before, and also here are some interesting responses about their representation.)
Before anyone asks, no, this question does not belong on mathoverflow; they hate algorithm (and generally non-graduate-level math) questions.
In practice, lists A and B are linked lists (stored in reverse order, actually). I will also need to keep track of which elements of C corresponded to which in A and B, but that is a minor detail. The algorithm I seek is not the merge operation in mergesort, because the precedence operator is not defined between elements of the two lists being merged. Everything will eventually be implemented in C++ (I just want the operator overloading). This is not homework, and will eventually be open sourced, FWIW.