Bob the builder has a set N of n nuts and a set B of n bolts
Solution
Consider the following algorithm:
Step 1: Take any nut at random and using it to partition the bolts into two groups. One group has diameter smaller than the chosen nut and the other group has the diagram larger than the chosen nut.
This results in finding out the bolt that matches with the nut and forming two sub-groups.
------------------------------------------------------------------------------------------------------------------
Step 2: Next use the bolt and partition the nuts into two groups. One group has diameter smaller than the bolt and the other group has diameter larger than the bolt.
------------------------------------------------------------------------------------------------------------------
Step 3: It is given that every bolt has an unique matching nut. So, the size of the group of bolts with smaller diameter is equal to the size of the group of nuts with smaller diameter. Same result holds for the bigger group also.
Therefore, repeat the above method on each of the 2 matching groups. This method is repeated till all matching is done.
------------------------------------------------------------------------------------------------------------------
Time Complexity of this algorithm:
Each round needs a maximum of O(n) comparisons in order to sub divide the groups into two smaller groups. And there are a total of O(log n) such groups.
So, the total comparisons = O(n log n). This is the time complexity.
