Data Structures & Alogs HW_Part_13

Data Structures & Alogs HW_Part_13 - 49 2 6 4 0 1 3...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
49 26401357 7.9 (a) Each call to qsort costs Θ( i log i ) . Thus, the total cost is n X i =1 i log i =Θ( n 2 log n ) . (b) Each call to qsort costs Θ( n log n ) for length(L) = n , so the total cost is Θ( n 2 log n ) . 7.10 A l ltha tweneedtodoisrede f ne the comparison test to use strcmp. The quicksort algorithm itself need not change. This is the advantage of paramer- izing the comparator. 7.11 For n = 1000 , n 2 =1 , 000 , 000 , n 1 . 5 = 1000 1000 32 , 000 ,and n log n 10 , 000 . So, the constant factor for Shellsort can be anything less than about 32 times that of Insertion Sort for Shellsort to be faster. The constant factor for Shellsort can be anything less than about 100 times that of Insertion Sort for Quicksort to be faster. 7.12 (a) The worst case occurs when all of the sublists are of size 1, except for one list of size i k +1 . If this happens on each call to SPLITk, then the total cost of the algorithm will be Θ( n 2 ) . (b) In the average case, the lists are split into k sublists of roughly equal length. Thus, the total cost is Θ( n log k n ) . 7.13 (This question comes from Rawlins.) Assume that all nuts and all bolts have a partner. We use two arrays N[1. .n] and B[1. .n] to represent nuts and bolts. Algorithm 1 Using merge-sort to solve this problem. First, split the input into n/2 sub-lists such that each sub-list contains two nuts and two bolts. Then sort each sub-lists. We could well come up with a pair of nuts that are both smaller than either of a pair of bolts. In that case, all you can know is something like:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

Data Structures & Alogs HW_Part_13 - 49 2 6 4 0 1 3...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online