Algorithms
Algorithms
60 B = A
A = A + 1
Is B a n in te g e r?
N o
Yes P rin t A
Is A = 6 0 ? Yes End
N o
Implement the algorithm given by the flow chart above and state what the algorithm actually produces.
(Total 5 marks)
60 Is B = A an integer?
A 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Churston Ferrers Grammar School
Yes or No
A 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
Yes or No
26 27 28 29 30
56 57 58 59 60
2.
25
22
30
18
29
21
27
21
The list of numbers above is to be sorted into descending order. (a) (i) (ii) Perform the first pass of a bubble sort, giving the state of the list after each exchange. Perform further passes, giving the state of the list after each pass, until the algorithm terminates.
(5)
The numbers represent the lengths, in cm, of pieces to be cut from rods of length 50 cm. (b) (i) (ii) Show the result of applying the first fit decreasing bin packing algorithm to this situation. Determine whether your solution to (b) (i) has used the minimum number of 50 cm rods.
(4) (Total 9 marks)
3.
The following list gives the names of some students who have represented Britain in the International Mathematics Olympiad. Roper (R), Palmer (P), Boase (B), Young (Y), Thomas (T), Kenney (K), Morris (M), Halliwell (H), Wicker (W), Garesalingam (G). (a) (b) Use the quick sort algorithm to sort the names above into alphabetical order.
(5)
D1 Algorithms Assessment B
4. Nine pieces of wood are required to build a small cabinet. The lengths, in cm, of the pieces of wood are listed below. 20, 20, 20, 35, 40, 50, 60, 70, 75
Planks, one metre in length, can be purchased at a cost of 3 each. (a) The first fit decreasing algorithm is used to determine how many of these planks are to be purchased to make this cabinet. Find the total cost and the amount of wood wasted.
(5)
Planks of wood can also be bought in 1.5 m lengths, at a cost of 4 each. The cabinet can be built using a mixture of 1 m and 1.5 m planks. (b) Find the minimum cost of making this cabinet. Justify your answer.
(4) (Total 9 marks)
5. STA RT
In p u t a
L e t b = F irs t n u m b e r in L is t P
L et c = L is t P : 2 , 3 , 5 , 7 , 1 1 , 1 3 ,
a b
Is c a n in te g e r? Y ES O u tp u t b
N O
In c re a s e b to n e x t in te g e r in L is t P
Is a = b? Y ES EN D
N O
L et a = c
The diagram above describes an algorithm in the form of a flow chart, where a is a positive integer. List P, which is referred to in the flow chart, comprises the prime numbers 2, 3, 5, 7, 11, 13, 17, ...
(a)
Starting with a = 90, implement this algorithm. Show your working in the table below. You may not need to use all the rows in this table.
Integer?
Output List
a = b?
(7)
(b)
(c)
6. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. Glasgow Newcastle Manchester York Leicester Birmingham Cardiff Exeter Southampton Plymouth
A binary search is to be performed on the names in the list above to locate the name Newcastle. (a) Explain why a binary search cannot be performed with the list in its present form.
(1)
(b)
Using an appropriate algorithm, alter the list so that a binary search can be performed. State the name of the algorithm you use.
(4)
(c)
Use the binary search algorithm on your new list to locate the name Newcastle.
(4) (Total 9 marks)
D1 Algorithms Assessment C
7. (a) 650 431 245 643 455 134 710 234 162 452
The list of numbers above is to be sorted into descending order. Perform a Quick Sort to obtain the sorted list, giving the state of the list after each pass, indicating the pivot elements.
(5)
The numbers in the list represent the lengths, in mm, of some pieces of wood. The wood is sold in one metre lengths. (b) Use the first-fit decreasing bin packing algorithm to determine how these pieces could be cut from the minimum number of one metre lengths. (You should ignore wastage due to cutting.)
(4)
(c)
Determine whether your solution to part (b) is optimal. Give a reason for your answer.
(2) (Total 11 marks)
8.
52
48
50
45
64
47
53
The list of numbers above is to be sorted into descending order. Perform a bubble sort to obtain the sorted list, giving the state of the list after each completed pass.
(Total 4 marks)
9. S ta rt
L et A = 0
In p u t x , y
Is x even? Yes
N o
A = A + y
x = x - 1
x = x 2
N o
Is x = 0 ?
Yes y = 2y O u tp u t A
(a)
Given that x = 54 and y = 63, complete the table below to show the results obtained at each step when the algorithm is applied. You may not need to use all of these rows. It may not be necessary to complete all boxes in each row. A x y x even? x=0?
(7)
(b)
D1 Algorithms Assessment A
1.
60 Is B = A an integer?
A 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 Yes or No Yes Yes Yes Yes Yes Yes No No No Yes No Yes No No Yes No No No No Yes No No A 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 Yes or No No No No No No No No No No No No No No No No No No No No No No No
23 24 25 26 27 28 29 30
No No No No No No No Yes
53 54 55 56 57 58 59 60
No No No No No No No Yes M1 A3 (1 eeoo)
What the algorithm produces: The algorithm produces all the factors of 60 B18
[8]
2.
(a)
(i)
25 25 25 25 25 22 30 30 30 30
le ft to right
30 22 22 22 22 18 18 29 29 29 29 29 18 21 21 21 21 21 18 27 27 27 27 27 18 21 21 21 21 21
or
25 25 25 25 30 22 22 22 30 25 30 30 30 22 22
right to le ft
18 18 29 29 29 29 29 18 18 18 21 27 27 27 27 27 28 21 21 21 21 21 21 21 21
M1
A1 (pass)
(ii)
25 30 30 30 30
30 25 29 29 29
22 29 25 27 27
29 22 27 25 25
21 27 22 22 22
27 21 21 21 21
21 21 21 21 21
18 18 18 18 18
30 30 30 30
29 29 29 29
25 27 27 27
22 25 25 25
27 22 22 22
18 21 21 21
21 18 21 21
21 21 18 18
(b)
(i)
rod
A1
(ii)
3. R B B B B
(a) P H G G G B G H H H
e.g. Y K K K K T R R M M K P P P P M Y M R R H T T T T W M Y W W G W W Y Y M1 A1 A1 ft A1 ft A1 ft
(b)
5 + 1 2 = 3 Halliwell; reject Boase Halliwell 4 + 5 2 = 5 Morris; reject Morris List reduces to Kenney name found, search complete
A1
A1 4
[9]
D1 Algorithms Assessment B
4. (a) Bin 1 75+20 Bin 2 70 +20 M1 Bin 3 60+40 Bin 4 50+35 Bin 5 20 5 Planks needed: cost 15 Wastage = 5+10+0+15+80 = 110cm A1 A1 A1 A1 5
(b)
Bin 1 (1.5m) 75 + 70
Bin 1 (1m) 75 + 20
M1 Bin 2 (1.5m) 60 + 50 + 40 or Bin 2 (1.5m) 70 + 60 + 20 Bin 3 (1m) 35 + 20 + 20 + 20 Bin 3 (1.5m) 50 + 40 + 35 + 20 Cost 11 1.5m lengths better value than 1m lengths to use as many as possible
A1 A1 A1 4
[9]
5.
(a) a 90 45 45 15 15 5 5 5
b 2 2 3 2 3 2 3 1 5
Output list 2 3 3
a = b? No No No M1 A1 A1ft M1 A1 M1
Yes
(b)
2 B1 1
[10]
6.
(a) (b)
The list is not in alphabetical order Use of Bubble Sort or Quick Sort M1 e.g. Bubble sort G B B B B B B N G C C C C C M N G E E E E Y M N G G G G L Y M N L L L B L Y M N M M C C L Y M N N E E E L Y P P S P P P P Y S P S S S S S Y
B1 1
1 2 3 4 5 6
st nd rd th th th
1 st 2 nd 3 rd 4 th 5 th 6 th
No sublists > 2 and no more changes A1 A1ft No more changes No sublists > 2 + no more changes A1cso 4
(c)
1 B
2 C
3 E
4 G
5 L
6 M
7 N
8 P
9 S
10 Y
[10 + 1] 2 =6 [7 + 10] 2 =9 [7 + 8] 2 =8
Manchester M1 A1
Southampton discard last half of list and pivot Plymouth discard last half of list and pivot
D1 Algorithms Assessment C
7. (a) E.g. M1 A1 A1ft A1ft A1 650 431 245 650 643 710 650 710 643 710 650 643 710 650 643 5 64 45 45 45 45 3 5 5 5 5 45 43 43 43 45 5 1 1 1 2 710 245 245 452 431 234 234 452 245 245 162 162 234 234 234 452 452 162 162 162 134 134 134 134 134
(b)
Bin 5 431
(c)
8.
e.g.
52 M1 52 52 52 64 64
48 50 50 54
50 48 54 50
45 54 48 53 50 50
64 47 53 48 48 48
47 53 47 47 47 47
53 45 45 45 45 45 A1 A1
52 53 A1ft 53 4 52
M1 Bubble sort 1st pass complete end term 64 or 45, consistent L -> R or R -> L shuffle, Quick etc gets M0
Churston Ferrers Grammar School
A1 First 2 passes correct} A1ft Next 2 passes correct (if L -> R next pass} condone shrinking list
A1 Final pass and final statement/rewritten list cso must see whole list
[4]
Misreads sorting into ascending order (note if candidates reverse list full credit is gained. L -> R (ascending misread) MR 52 48 50 45 64 47 53 M1 48 48 45 45 50 45 48 47 45 50 47 48 52 47 50 50 47 52 52 52 53 53 53 53 64 64 64 64 A1 (4-2 for misread) A1 A1
R -> L 52 45 45 48 M1 52 47 50 48 52 45 50 48 64 47 50 47 64 53 53 53 64 A1
45 45
47 47
48 48
52 50
50 52
53 53
64 64 A1 A1 (4-
9.
(a) A 0 126 378 x 54 27 26 13 12 6 3 1386 3402 2 1 0 2016 N Y A1 504 1008 Y N N A1ft 252 N N M1 y 63 126 x even? Y N N A1ft x = 0? M1A1
A = 3402
B1ft7
(b)
B2,1,02
[9]