Question

Bob has a set A of n nuts and a set B of n bolts, such that each nut in A has a unique matching bolt in B. Unfortunately, the nuts in A all look alike, and the bolts in B all look alike as well. The only kind of a comparison that Bob can make is to take a nut-bolt pair (a, b), such that a is in A and b is in B, and test to see if the threads of a are larger, smaller, or a perfect match with the Bob to match up all of his nuts and bolts Here efficiency is measured in terms of the number of nut-bolt comparisons. Note that we cannot have any nut-nut or bolt-bolt type comparisons. We can obviously compare every nut against every bolt, but that would require 0(n2) comparisons. Can we do better? Hint: think about QuickSort.]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

import java.util.Arrays;

public class NutsAndBoltsProblem {

public static void main(String[] args) {

int bolts[]={3,2,9,5,7,1,6,8,10,4};

int nuts[]={6,1,8,2,9,10,3,5,4,7};

quickSort(bolts,nuts,0,bolts.length-1);

System.out.println("BOLTS[] = "+Arrays.toString(bolts));

System.out.println("NUTS[] = "+Arrays.toString(nuts));

}

public static void quickSort(int bolts[],int nuts[],int bstart,int bend)

{

if(bstart>bend)

return ;

int pivot=bolts[bend];

pivot=partition(nuts,bstart,bend,pivot);//based on bolt comparison

pivot=partition(bolts,bstart,bend,pivot);//based on nut comparison

quickSort(bolts,nuts,bstart,pivot-1);

quickSort(bolts,nuts,pivot+1,bend);

}

public static int partition(int ar[],int start,int end,int pivot)

{

int pindex=start;

int loc=end;

for(int i=start;i<=end;i++)

{

if(ar[i]<pivot)

{

swap(ar,pindex,i);

pindex++;

}

if(pivot==ar[i])

loc=i;

}

swap(ar,loc,pindex);

return pindex;

}

public static void swap(int ar[],int x,int y)

{

int temp=ar[x];

ar[x]=ar[y];

ar[y]=temp;

}

}

OUPUT:

EXPLAINATION:

In the quicksort function, what i am doing is taking the end of bolts array as the pivot .

now , use the partition logic to partition the NUTS array based on this pivot. I have added a location variable in the partition method, that will set the location as i , where Nut[i]==pivot (comparing nut with bolt). swapping the pindex with this location and returning the pindex.

now use this nut pivot (pindex from nut) to partition the bolts array as done above.

else remain the same logic of quicksort.

っ indee on- and HuA

Add a comment
Know the answer?
Add Answer to:
Bob has a set A of n nuts and a set B of n bolts, such...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 8. You are given two boxes, one contains nuts and the other contains bolts. Below is...

    8. You are given two boxes, one contains nuts and the other contains bolts. Below is a picture of a bolt. The D indicates Below right is a side and overhead picture of a nut. The the diameter of the bolt D indicates the diameter of the hole INSIDE the nut. ATI RODI c ISO METRIC AND WASHERS A bolt is supposed to fit inside a nut. On the right is a picture of a bolt properly fitting inside a...

  • please just do #1 1. The lid on a pressure vessel is held down with 10...

    please just do #1 1. The lid on a pressure vessel is held down with 10 bolts that pass through the lid and a flange on the pressure vessel (similar to example 8-4, in the text). The bolts are secured with nuts under the flange. There are no washers. The thickness of the lid is 28 mm and the thickness of the flange is 28 mm. The vessel and lid are both made of steel. The vessel is pressurized to...

  • Please write full justification for (a) and (b). Will uprate/vote! 4. K-means The goal of K-means clustering is to divide a set of n points into k< n subgroups of points that are "close" t...

    Please write full justification for (a) and (b). Will uprate/vote! 4. K-means The goal of K-means clustering is to divide a set of n points into k< n subgroups of points that are "close" to each other. Each subgroup (or cluster) is identified by the center of the cluster, the centroid (μι, μ2' ··· ,14k) In class, we have seen a brute force approach to solve this problem exactly. Each of the k clusters is represented by a color, e.g.,...

  • I Do We Have the Complete Solution Set? A differential operator in R[D] has order n can be writte...

    I Do We Have the Complete Solution Set? A differential operator in R[D] has order n can be written out in the form o(n-1) with the last coefficient cn (at least) not equal to zero. The key to determining the dimension of these solution spaces is the following existence and uniqueness theorem for initial value problems. 'So it can be efficiently described by giving a basis. ethciently described by giving a basis Theorem 1 (Existence and Unique ness Theorem for...

  • Question 8 A = 23n + 36n2        B = 6 + nlog2(n) + n      C =...

    Question 8 A = 23n + 36n2        B = 6 + nlog2(n) + n      C = log2n + 36n2 Which one of the following is correct? A. TA = O(n2)    TB = O(n)   TC = O(log2n) B. TA = O(n2)      TB = O(nlog2(n))  TC = O(n2) C. TA = O(n2)      TB = O(+ nlog2(n))       TC = O(log2n) D. TA = O(n2)      TB = O(n)      TC = O(n2) 0.5 points Question 9 Three criteria are used to determine whether a data structure is...

  • Which of the following set of quantum numbers (ordered n,l,ml,ms) are possible for an electron in...

    Which of the following set of quantum numbers (ordered n,l,ml,ms) are possible for an electron in an atom? Quantum Number Rules Learning Goal: To learn the restrictions on each quantum number. Quantum numbers can be thought of as labels for an electron. Every electron in an atom has a unique set of four quantum numbers. The principal quantum number n corresponds to the shell in which the electron is located. Thus n can therefore be any integer. For example, an...

  • CASE 14 SAM PLING DISTRIBUTORS "We have to think more about our acceptance sampling decisions," said Buddy Abbot, head of the production department of Sam Pling Distributors. Sam Pling Dist...

    CASE 14 SAM PLING DISTRIBUTORS "We have to think more about our acceptance sampling decisions," said Buddy Abbot, head of the production department of Sam Pling Distributors. Sam Pling Distributors is a large company providing over twenty percent of the tire nuts used by American automobile manufacturers Listening attentively was Louis Costello, Abbot's assistant, who was charged with the sampling procedures used to ensure that out-going shipments conformed to the proper specifications noted on the order form. The key quality...

  • Python Program Eratosthenes of Cyrene lived approximately 275-195 BC. He was the first to accurately estimate...

    Python Program Eratosthenes of Cyrene lived approximately 275-195 BC. He was the first to accurately estimate the diameter of the earth. For several decades he served as the director and chief librarian of the famous library in Alexandria. He was highly regarded in the ancient world, but unfortunately only fragments of his writing have survived. The algorithm described for this assignment is known as the Sieve of Eratosthenes. The algorithm is designed to find all prime numbers within a given...

  • DI Question 2 1.5 pts Which situations allow the client main() in the file my_program.py to access a method name, say f...

    DI Question 2 1.5 pts Which situations allow the client main() in the file my_program.py to access a method name, say func(), alone, as in x-func) without dereferencing it using prepended name like modname.func or x = some object. func () or x-SomeClass.func() O func) is defined in some module, say, "modname.py" and modname is imported into my_program.py using: from modname import func() is an instance method or a class method in a class, say SomeClass, defined in the same...

  • Here is a data set (n = 117) that has been sorted. 56.8 69.8 71.2 73.7...

    Here is a data set (n = 117) that has been sorted. 56.8 69.8 71.2 73.7 75.5 77. 4 78.7 80.3 81. 8 84. 5 87 88.6 92.4 59.9 70.4 72.2 74 75.6 77.5 78.7 80.5 8 2 85 87.1 88.6 92.7 61.2 70.4 72.3 74.1 75.9 77.7 78.7 80.8 82.2 85.3 87.6 88.9 92.8 62.2 68.4 70.5 70.6 72.4 72.5 74.2 74.3 76.1 76.2 77.8 77. 8 78.9 79.1 | 81 | 81.1 82.2 82.2 86.1 86.5 87.8 87.9...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT