Homework 9 Solutons PROBLEM ONE 1 (Exercses from the book, th edton,, 1-) Determne the number of dstnct orderngs of the letters gven: (a) GUIDE Soluton: 5! (b) SCHOOL Soluton:! (c) SALESPERSONS Soluton: 1! 4!! (Exercses from the book, th edton,, 4-5) (a) In how many ways can we order the letters of SALESPER- SONS so that all four S characters are adacent? Soluton: Snce the four Ss must be adacent, we may group them together and move them together Therefore we can magne that we have now 9 obects: SSSS, A, L, E, P, E, R, O, N We permute these obects, notng that now there are only two dentcal obects: both Es Therefore there are 9!! orderngs (b) In how many ways can we order the letters of SALESPER- SONS so that no two S characters are adacent? Soluton: Ths s a very dffcult problem unless you use the method outlned n the book I wll explan ths method n a separate handout Please look for ths on the course webste PROBLEM TWO (Pascal s Trangle) In class, we showed that for any n 1 and k wth 1 k n, we have k n 1 k 1 Recall that ths notaton means ( ) ( ) ( ) n k 1 k + k k 1 k 1 1 ( ) k 1 + + 1 k 1
If we plug n for k we get ( ) 1 + 1 ( ) + + 1 and snce ( ) n n! (n )!! n(n 1), we get n(n 1) 1 Use k to fnd the sum of 1 1 1 + + + (n 1) (1) 1 + + + 10 + + Soluton: Puttng k nto the dentty above, we get ( ) + ( ) + + n(n 1) () ( n 1 Snce ( ) n n(n 1)(n ) and snce for each k, ( ) k k(k 1), we get n(n 1)(n ) 1 + + + + In ths equaton, plug n n + 1 for n to get 1 + + + + n(n 1) ) (n 1)(n ) (n + 1)n(n 1) Can you use both formulas (1) and () to fnd the sum 1 + 4 + + n? Soluton: We can rewrte the answer to the last queston as k(k 1) + 1 + 1 (k k) k + 1 k + 1 k + k
However, we know from (1) that n k n+1 Therefore ( ) ( ) n + 1 n + 1 k + (n + 1)n(n 1) (n + 1)n + [ n 1 (n + 1)n + 1 ] n(n + 1)(n + 1) Gve two arguments (one whch uses factorals and one whch s more heurstc, usng n obects etc) to show that ( ) ( ) n n k n k Soluton: To show usng factorals, we have n k n! (n k)!(n (n k))! n! (n k)!k! n! k!(n k)! k Now we argue usng combnatons etc Suppose that we have n obects and 1 bn We choose k of these obects and place them n bn 1 The number of ways we can do ths s ( n k) Instead of choosng k obects to place n bn 1, we could have chosen n k obects not to place n bn 1 the number of ways to do ths should be exactly the same, and ths number s ( n n k) PROBLEM THREE (Inserton Sort) Exercses for problem three: 1 Trace through ths algorthm, showng lsts wth arrows at each step (as I dd) Use the ntal data 11, 4,, 10, 0, EOL Soluton: Lst A 11 4 10 0 EOL 11 EOL
Lst A 11 4 10 0 EOL 4 11 EOL 4 11 EOL Lst A 11 4 10 0 EOL 4 11 EOL 4 11 EOL Lst A 11 4 10 0 EOL 4 11 EOL 4 11 EOL 4 10 11 EOL 4 10 11 EOL Lst A 11 4 10 0 EOL 0 4 10 11 EOL 0 4 10 11 EOL Lst A 11 4 10 0 EOL Here s some pseudocode whch should perform the nserton sort It assumes that the lst A has sze at least 1 4
lne code 1 1 create the lst B wth two characters: A(1) and EOL 4 whle(a() EOL) { 5 whle(a() B() and B() EOL) { + 1 } 7 place A() drectly before spot n lst B 8 1 9 + 1 } 10 Return B Trace through ths algorthm (show the status of each of the varables at each change) for the ntal data A 11, 4,, 10, 0, EOL Each tme a varable does not change, you may wrte a dash ( ) for that varable n the tracng table Soluton: Lst A 11,4,,10,0,EOL?????? 1 11,EOL 4,11,EOL,4,11,EOL 4,4,10,11,EOL 1 5 0,,4,10,11,EOL Gve a lst A wth elements whch gves the worst-case number of lnes executed How many lnes s ths? Soluton: A worst-case lst wth elements s 1,,, EOL In ths case, the algorthm executes 1 lnes 4 Gve a lst B wth elements whch gves the best-case number of lnes executed How many lnes s ths? Soluton: A best-case lst wth elements s,, 1, EOL In ths case, the algorthm executes 15 lnes 5
5 For a lst of n elements, what s the worst-case number of lnes executed? What s the best-case number of lnes executed? How do these numbers compare to those we found n class for the bubble sort? Are there cases n whch the nserton sort s better? Are there cases n whch t s worse? Soluton: The worst-case number of lnes s n + 4n Ths was computed on the homework hnt sheet The best-case number of lnes s 5n Ths can be seen as follows In the best case, we never enter the nner loop Therefore, durng each pass through the outer loop we execute 5 lnes (lnes 4, 5, 7, 8, and 9) The outer loop s executed as moves from to n Ths s n 1 tmes So we get a total of + 5(n 1) + 5n lnes lnes 1- outer loop fal outer loop and ext The bubble sort executed a number of lnes whch was on the order of n (e ts run-tme was θ(n )), regardless of the nput Therefore the nserton sort s much better for nput lsts whch are already sorted, but n decreasng order (ths was the best-case for nserton sort) For example, the lst 10, 9, 8,, 1, EOL wll sort much faster under nserton sort For some lsts, the nserton sort executes a number of lnes whch s on the order of n (e t s θ(n )) For these lsts, both sort algorthms perform relatvely the same Whether or not there exst a lst whch sorts faster under bubble sort s a queston that I wll have to answer later I dd not wrte down the exact number of lnes executed (whch we computed n class) for bubble sort