DO NOT WRITE CODE! EXPLAIN AN ALGORITHM You have to write an algorithm that solves the following problem: Given a collec
Posted: Fri Jun 10, 2022 11:57 am
DO NOT WRITE CODE! EXPLAIN AN ALGORITHM
You have to write an algorithm that solves the following
problem: Given a collection (unsorted) of n screws and n
corresponding nuts, each screw must be matched to its nut of the
same size. This should be done as fast as possible. All nut and
screws can be pairedand there will be no nuts or screws left over,
butthere may be multiple screws/ nuts of the same size. Screws and
nuts are a pair if they are the same size. For simplicity, we will
assume that the collection of nuts is given as a list ofpart
numbers (implemented as a list of integers). These are not the
sizes of the parts. The collection of screws is given in the same
way. You may also assume an operationsort(l) that sorts a list of
part numbers according to item size and runs in nlog(n) time. The
range of part numbers is not known in advance. You also have
available a functioncomp(x,y) which tests a screw with part numberx
and a nut with part number y and returns 0 if the parts with
numbersx and y have the same size, -1 if the part x is smaller than
y and +1 otherwise. Note: the sort and comp functions do so based
on size, but you do not have access to the sizes directly (i.e. no
algorithm that you write can refer to the size of a given nut or
screw).
You have to write an algorithm that solves the following
problem: Given a collection (unsorted) of n screws and n
corresponding nuts, each screw must be matched to its nut of the
same size. This should be done as fast as possible. All nut and
screws can be pairedand there will be no nuts or screws left over,
butthere may be multiple screws/ nuts of the same size. Screws and
nuts are a pair if they are the same size. For simplicity, we will
assume that the collection of nuts is given as a list ofpart
numbers (implemented as a list of integers). These are not the
sizes of the parts. The collection of screws is given in the same
way. You may also assume an operationsort(l) that sorts a list of
part numbers according to item size and runs in nlog(n) time. The
range of part numbers is not known in advance. You also have
available a functioncomp(x,y) which tests a screw with part numberx
and a nut with part number y and returns 0 if the parts with
numbersx and y have the same size, -1 if the part x is smaller than
y and +1 otherwise. Note: the sort and comp functions do so based
on size, but you do not have access to the sizes directly (i.e. no
algorithm that you write can refer to the size of a given nut or
screw).