Date | May 2019 | Marks available | 4 | Reference code | 19M.2.SL.TZ0.12 |
Level | SL | Paper | 2 | Time zone | no time zone |
Command term | Outline | Question number | 12 | Adapted from | N/A |
Question
An Event
has been instantiated with 2 qualifying heats for a total of 11 swimmers.
Event free100 = new Event("100 m free style",2);
The swimmers were added to the two Race
arrays and after the races, their times were recorded as shown in the table.
(For the purpose of this question, the name represents the full swimmer object.)
The method fillFinals()
will select the 8 fastest swimmers, in ascending order of time, from both swimmer
arrays and copy them to the swimmer
array in the finals
race.
To help with this selection, all entries from races[0]
and races[1]
will be copied into two new parallel arrays of size 16, one array for swimmers and one array for their times.
The two temporary arrays will be sorted using the following code.
int i,j;
Swimmer swapSwimmer;
double swapTime;
for(i = 0; i < 15; i++)
{
for(j = 0; j < 15; j++)
{
if(tempTime[j] > tempTime[j + 1]) // if wrong order then…
{
swapSwimmer = tempSwimmer[j]; // swap the swimmer and…
tempSwimmer[j] = tempSwimmer[j + 1];
tempSwimmer[j + 1] = swapSwimmer;
swapTime = tempTime[j]; // swap the time
tempTime[j] = tempTime[j + 1];
tempTime[j + 1] = swapTime;
}
}
}
Sketch the resulting swimmer
array in finals
.
Construct the code fragment for the given situation that will copy swimmers and times into two parallel arrays named tempSwimmer
and tempTime
.
State the name of this sorting algorithm.
Outline two improvements to this code that would make the algorithm more efficient.
Construct the code fragment that will copy the names of the 8 fastest swimmers in ascending order of time from the array tempSwimmer
to the array swimmers
in the race finals
.
Markscheme
Award [3 max].
Award [1] for array of 8 slots with array name.
Award [1] for correct entries (in any order).
Award [1] for correct order.
Do not penalize the lack of subscripts.
Award [6 max].
Award [1] for correctly declaring 2 arrays of size 16.
Award [1] for correct outer loop.
Award [1] for correct inner loop.
Award [1] for copying swimmer object.
Award [1] for copying time.
Award [1] for incrementing the index of the new arrays.
Example answers:
Swimmer[] tempSwim = new Swimmer[16];
double[] tempTime = new double[16];
int newIndex = 0;
for(int i = 0; i < 2; i++){
for(int j = 0; j < 8; j++){
tempSwim.swimmer[newIndex] = races[i].swimmer[j];
tempTime.time[newIndex] = races[i].time[j];
newIndex++;
}
}
Note: that the question asks for all entries to be copied. However, do not penalize "efficient" solutions that avoid copying the null and 0 entries.
Bubblesort;
Award [4 max].
Award [1] for stating an improvement and [1] for an elaboration up to [2 max].
Mark as [2] and [2].
Include a flag “swapped”;
That can help stop the outer loop if there is a pass through the inner loop with no swap;
Limit the inner loop by deducting the outer loop counter;
So that the sorted elements are no longer compared;
Award [6 max].
Award [1] for correctly instantiating variables.
Award [1] for correct loop condition (count
< 8).
Award [1] for checking for a 0 time (or null entry).
Award [1] for correctly copying.
Award [1] for incrementing count
in the right place.
Award [1] for incrementing k
in the right place.
Example answers:
int k = 0;
int count = 0;
while(count < 8) && (k < 16){
if(tempSwim[k] != 0){
finals.swimmer[count] = tempSwim[k];
count++;
}
k++;
}
Note: Do not penalize for not including loop condition (k < 16
)
Examiners report
This was well-answered.
This was the first significant piece of coding on the paper and, as in previous papers, divided the students into those who were comfortable with coding and those who weren't. From the former came a variety of solutions, some of which were quite innovative. The complexity came from ensuring that the correct indices were used for the different array items and for dealing correctly with private variables outside of their class.
Almost all correctly gave Bubble Sort, but clear details of improvements that could be made were limited to the candidates who were adept at programming.
Almost all correctly gave Bubble Sort, but clear details of improvements that could be made were limited to the candidates who were adept at programming.
Many candidates failed to realise that the 8 fastest swimmers were not the first 8 entries in the sorted array as this would give null entries.