First of all, consider the following trivial algorithm.
Pick any pipe. Compare against all sockets until you find one that matches. Then move on to another pipe, and repeat until all are done.
If there are $n$ pipes and $n$ sockets then this takes at most
So that gives us an upper bound on how long we can take in the worst case. For a lower bound,
suppose we are given some extra information at the outset: we are told the sizes of all the sockets. Our challenge is now to sort the pipes, and the only operation available to us is to compare a pipe's size with one of the numbers (let's say) from $1$ to $n$. There are $n!$ possible orderings and each comparison gives us one of at most 3 outcomes, so distinguishing all the outcomes takes at least $\log_3(n!)$ comparisons, which is of order $n\log n$.
If we are content with good average performance, we can do this:
Pick a random pipe. (Call it P.) Try it against all sockets until you find one it matches. (Call that S.) Then partition pipes into "smaller than S" and "larger than S", and sockets into "smaller than P" and "larger than P", after which we have two smaller subproblems. Solve those by the same method. (Once they get small enough you may actually do better to switch to the trivial algorithm above.)
This takes, on average,
time proportional to $n \log n$, with constant of proportionality not too wretched. (It's kinda like quicksort, except that the partitioning step is about twice as slow.)
I haven't yet thought of an algorithm that
does better than quadratic time in the worst case, but I bet there is one. I suspect we can manage time of order $n \log n$ at worst. One difficulty is that some partitioning schemes are much harder than for sorting. For instance, a natural idea is to look for something like mergesort, but that would require us to find $n/2$ matching pipes and sockets at the start.
Perhaps we can do it by
solving a harder problem. We have $n$ pipes and $n$ sockets that don't necessarily match; we have the same comparison-making ability as in the original problem; we want to sort them into an order so that we never have $a$ before $b$ when $a,b$ are of different types and $a$ is larger than $b$. Can we do this in time $O(n \log n)$?
The point here is that
the harder problem may make a better "induction hypothesis". So, let's begin by picking any $\lfloor n/2\rfloor$ pipes and any $\lfloor n/2\rfloor$ sockets. Sort these, and sort the remaining $\lceil n/2\rceil$ pipes and sockets. If we can now combine these in time $O(n)$ then we will have a solution to the original problem in time $O(n\log n)$. Unfortunately we can't. Suppose we happened to pick the $n/2$ biggest pipes and the $n/2$ smallest sockets; then to finish the job requires us to sort the pipes, which surely can't be done in time smaller than $O(n \log n)$. But it seems like something along these lines might be workable.