Vous êtes sur la page 1sur 5

Question 1.

9
Estimate
N
X 1
N
i
i=b 2 c

Solution

N
N N b 2 c−1
X 1 X1 X 1
= −
N
i i=1
i i=1
i
i=b 2 c

N
≈ ln N − ln
2
N
≈ ln N
2
≈ ln 2
Question 1.12(a)
Prove the following formula:
N
X
(2i − 1) = N 2
i=1

Solution

N
X N
X N
X
(2i − 1) = 2i − 1
i=1 i=1 i=1
N
X N
X
=2 i− 1
i=1 i=1
 
N (N + 1)
=2 −N
2
= N (N + 1) − N
= N2 + N − N
= N2
Question 1.12(b)
Prove the following formula:

N N
!2
X X
i3 = i (1)
i=1 i=1

Solution The following formula is given in the notes,

N
X N (N + 1)
i= (2)
i=1
2

Subbing the value on the RHS of Equation(2) into the RHS of Equation(1)
gives,

N  2
X
3 N (N + 1)
i = (3)
i=1
2

As Equation(3) is equivalent to Equation(1), if we prove that Equation(3) is


true it follows that Equation(1) is true. Equation(3) can be proved true using
induction.
(Continued on the next page)
Base Case: N = 1

1
X
i3 = 13 = 1
i=1
 2  2
1 (1 + 1) 2
= =1
2 2

There true for N = 1


Assume true for N = k
Assume,
k  2
X k (k + 1)
i3 =
i=1
2
Now prove true for N = k + 1, using above assumption

k+1
X k
X k+1
X
i3 = i3 + i3
i=1 i=1 k+1
 2
k (k + 1) 3
= + (k + 1)
2
 2 2 !
2 k
= (k + 1) + (k + 1)
2
2 
(k + 1) k 2 + 4k + 4
=
4
2 2
(k + 1) (k + 2)
=
22
 2
(k + 1) (k + 2)
=
2

Q.E.D.
In the notes you are asked to write a function called merge parts
1 /∗ This i s a f u n c t i o n m e r g e p a r t s which i s t h e w o r k h o r s e o f
2 ∗ t h e m e r g e s o r t a l g o r i t h m . I t t a k e s an array , and t h e
3 ∗ b o u n d a r i e s w i t h i n which t h e s o r t s h o u l d t a k e p l a c e ,
4 ∗ and s o r t s w i t h i n them
5 ∗ arr [ ] : The a r r a y t o be s o r t e d
6 ∗ lo : The s t a r t o f t h e sub−a r r a y t o be s o r t e d
7 ∗ mid : The s p l i t i n t h e sub−a r r a y
8 ∗ hi : Then end o f t h e sub−a r r a y t o be s o r t e d
9 ∗/
10

11 void m e r g e p a r t s ( i n t a r r [ ] , i n t l o , i n t mid , i n t h i )
12 {
13 // The f o l l o w i n g i n t e g e r s a r e i t e r a t o r s f o r t h e
14 // merge−s o r t i n g a l g o r i t h m
15 i n t l e f t I t r = l o , r i g h t I t r = mid + 1 , t e m p I t r = 0 ;
16 // Get t h e number o f e l e m e n t s and c r e a t e a temporary a r r a y
17 int num of elements = ( hi − l o ) + 1 ;
18 int temp arr [ num of elements ] ;
19

20 //The f o l l o w i n g w h i l e l o o p p e r f o r m s most o f t h e ” merging ”


21 while ( l e f t I t r <= mid && r i g h t I t r <= h i )
22 {
23 // d e p e n d i n g on which s i d e i s t h e s m a l l e r , copy t h e
24 // smaller i nt o the temp arr [ ]
25 if ( a r r [ l e f t I t r ] <= a r r [ r i g h t I t r ] )
26 // we i n t e r a t e i n p l a c e
27 t e m p a r r [ t e m p I t r++] = a r r [ l e f t I t r ++];
28 else
29 t e m p a r r [ t e m p I t r++] = a r r [ r i g h t I t r ++];
30 }
31

32 // Only one o f t h e f o l l o w i n g w h i l e l o o p s i s e x e c u t e d
33 // They e x i s t t o remove any o v e r h a n g
34 while ( l e f t I t r <= mid )
35 t e m p a r r [ t e m p I t r++] = a r r [ l e f t I t r ++];
36

37 while ( r i g h t I t r <= h i )
38 t e m p a r r [ t e m p I t r++] = a r r [ r i g h t I t r ++];
39

40 // Copy from t h e temporary a r r a y b a c k i n t o o r i g i n a l a r r a y


41 // Done t h i s way i t o v e r w r i t e s t h e s e c t i o n o f t h e o r i g i n a l
42 // a r r a y w i t h t h e s o r t e d v e r s i o n
43 f o r ( i n t i = 0 ; i < n u m o f e l e m e n t s ; i ++)
44 arr [ l o + i ] = temp arr [ i ] ;
45 }

Vous aimerez peut-être aussi