{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Data Str &amp; Algorithm HW Solutions 50

# Data Str &amp; Algorithm HW Solutions 50 - n 2 In that...

This preview shows page 1. Sign up to view the full content.

50 Chap. 7 Internal Sorting N7 N8 N9 B10 B11 Again, at each line, there is no information available about the relationships of those objects. To merge two such lists, we can do a normal merge, until we reach the point were we either compare an element (say a nut) against a list of undifferen- tiated bolts (which requires a simple pass through the list), or else a set of undifferentiated nuts and another set of undifferentiated bolts. This would require a recursive call to the sorting program. Unfortunately, in the worst case, one sublist will contain nuts all smaller than bolts, and the other will contain bolts all smaller than nuts. Thus, merging the two sublists will require solving two more subproblems of size
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: n/ 2 . In that case, the of this algorithm (and any similar divide-and-conquer algorithm) is: T ( n ) = 4 T ( n/ 2) + O ( n ) = O ( n 2 ) . 7.14 (a) For 3 values, use the following series of if statements (based on the decision tree concept of Figure 8.16, and optimized for swaps). void Sort3(ELEM A) { // Assume A has 3 elements if (A[1] < A[0]) if (A[2] < A[0]) if (A[2] < A[1]) // ZYX swap(A[0], A[2]); else { // YZX swap(A[0], A[1]); swap(A[1], A[2]); } else // YXZ swap(A[0], A[1]); else if (A[2] < A[1]) if (A[2] < A[1]) { // ZXY swap(A[0], A[2]); swap(A[1], A[2]); } else // XZY swap(A[1], A[2]); else // XYZ -- Do nothing } Cost:...
View Full Document

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern