PROBLEM ONE Homework 9: due Monday, 7 October, 008. (Exercses from the book, 6 th edton, 6.6, -3.) Determne the number of dstnct orderngs of the letters gven: (a) GUIDE (b) SCHOOL (c) SALESPERSONS. (Exercses from the book, 6 th edton, 6.6, 4-5) (a) In how many ways can we order the letters of SALESPERSONS so that all four S characters are adacent? (b) In how many ways can we order the letters of SALESPERSONS so that no two S characters are adacent? PROBLEM TWO (Pascal s Trangle) In class, we showed that for any n and k wth k n, we have ( ) n k Recall that ths notaton means ( ) ( ) n k k k If we plug n for k we get ( ) n ( ) + n k and snce ( ) n n! (n )!! n(n ), we get n(n ). Use k 3 to fnd the sum of ( ). k ( ) k + +... + k ( ) +... + ( ) n. k ( ) n + +... + (n ). () + 3 + 6 + 0 +... +. Can you use both formulas () and () to fnd the sum + 4 +... + n? n(n ). ()
3. 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 PROBLEM THREE (Inserton Sort) In ths problem, you wll nvestgate the nserton sort algorthm. Here s the dea. We start wth a lst let us take as an example the lst A 0,, 4, 0,, EOL. We follow these steps (here we assume the lst has at least one number n t): A We start at the left wth an arrow, whch we wll call. B Next we start a new lst, whch we wll call B, whch conssts of only the lst element to whch ponts: B 0, EOL and we place an arrow, whch we wll call, at the begnnng of ths lst. 0 EOL C Move to the rght by one. If s at EOL, then qut. D Insert the element to whch ponts nto the lst B. To do ths, follow these steps: a Move to the rght untl we reach ether (a) the character EOL or (b) an element whch s greater than that to whch ponts (ths s the number n our case). Notce that we do not need to move n our case. b Insert the element drectly before the place ponted to by :
0 EOL c Put back at the begnnng of the lst B. 0 EOL E Go to step C. I wll contnue wth the algorthm. When we return to step 3, we move to the rght: and we nsert ths element n the lst B. Snce ponts to and ths s less than or equal to the number to whch ponts (the number 4), we move to the rght. 0 EOL Snce the number to whch ponts s greater than 4, we nsert 4 drectly before t: 4 0 EOL and return to the begnnng: 4 0 EOL We now return to step 3 and move to the rght: 3
and nsert ths element nto the lst B: returnng to the begnnng: Move to the rght: and nsert ths number nto B. To do ths, move to the rght untl we encounter a number whch s greater than the one to whch ponts: Place the number 0 4 0 EOL and return to the front. 4
0 4 0 EOL Lastly, move to the rght: Snce t ponts to EOL, we qut. The lst B s a sorted verson of A. Exercses for problem three:. Trace through ths algorthm, showng lsts wth arrows at each step (as I dd). Use the ntal data, 4,, 0, 0, EOL.. Here s some pseudocode whch should perform the nserton sort. It assumes that the lst A has sze at least. lne code 3 create the lst B wth two characters: A() and EOL 4 whle(a() EOL) { 5 whle(a() B() and B() EOL) { 6 + } 7 place A() drectly before spot n lst B 8 9 + } 0 Return B Trace through ths algorthm (show the status of each of the varables at each change) for the ntal data A, 4,, 0, 0, EOL. Each tme a varable does not change, you may wrte a dash ( ) for that varable n the tracng table. I have started you off (the frst lne s before anythng happens):,4,,0,0,eol?????,eol? 5
3. Gve a lst A wth 3 elements whch gves the worst-case number of lnes executed. How many lnes s ths? 4. Gve a lst B wth 3 elements whch gves the best-case number of lnes executed. How many lnes s ths? 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? 6