Professional Documents
Culture Documents
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
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
1
X
i3 = 13 = 1
i=1
2 2
1 (1 + 1) 2
= =1
2 2
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
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