Data Str & Algorithm HW Solutions 50

Data Str & Algorithm HW Solutions 50 - n/ 2 . In...

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

View Full Document Right Arrow Icon
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
Background image of page 1
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

This note was uploaded on 12/27/2011 for the course MAP 2302 taught by Professor Bell,d during the Fall '08 term at UNF.

Ask a homework question - tutors are online