Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The teacher runs an after - school computing club and stores the names of children who belong to this club in a list, which at

The teacher runs an after-school computing club and stores the names of children who belong to this club in a list, which at present is unsorted.
Another school close by would like to offer a computer club, but does not have suitable facilities. However, the two schools have reached an agreement allowing children from the second school to attend the computer club run by the first.
The second school has supplied a list, again unsorted, of the names of children from that school who want to belong to the club.
The teacher who runs the club now wants a program that will take the two lists and combine them to eventually produce a single sorted list containing the names of all the children, from both schools, who belong to the club.
She is considering two possible strategies for producing this list.
Strategy 1. Append the second schools unsorted list to the end of the first schools unsorted list, then sort the combined list.
Strategy 2. Sort the two lists separately, then combine the sorted lists in a way that results in a single sorted list.
The teacher is interested in exploring how many comparisons between names each sorting strategy uses and which of the two requires the fewer comparisons. She has begun writing a program to investigate these questions and has enlisted you to help her complete it and use it to run some experiments.
For simplicity, we will only use the childrens first names and assume no two names are the same.
Open the project TM111_02_Q4.sb2. It contains three lists:
school_1 and school_2, which contain the two unsorted lists from the first and second schools, respectively
merged, which is empty at present; this will hold the final sorted list.
There are also four variables, as follows (the meaning of swapping pass will be described when we come to it):
pass, used to keep track of swapping passes
position, used to keep track of the current position in a swapping pass
temp, used when swapping two adjacent elements
comparison_count, used to count how many pairs of adjacent elements are compared, during sorting and the subsequent merge operation.
In addition there are two components, the custom blocks swapping_pass_1 and swapping_pass_2, and a when[1]key_pressed script. The purpose of these will now be explained.
Note that there is no need for you to modify either of the custom blocks and you should not attempt to do so.
a.The swapping_pass_1 component and the when[1]key_pressed script together form an implementation of bubble sort 2, as discussed in Subsection 6.4.2 of Block 2 Part 6. The list they will sort is school_1.
In a bubble sort we perform a series of swapping passes, where each swapping pass consists of working our way through the list to be sorted, comparing pairs of adjacent items, and swapping any pair that is out of order.
Each swapping pass causes an item to bubble up and stop at its correct position in the sorted list and so after each swapping pass the unsorted portion of the list becomes one item shorter. Since a swapping pass only needs to consider the unsorted portion, each pass can be one comparison shorter than the previous one.
This is illustrated in Figures 6.34 to 6.37 of Subsection 6.4.2, where we observe that bubble sort 2 will require approximately (n 1)2/2 comparisons, where n is the number of items.
It is not too hard to show (but we will just assume this result) that the exact number is n \times (n 1)/2.
Make sure the watcher for comparison_count is visible on the stage, then press the 1 key to execute the when[1]key_pressed script.
You should now see that the school_1 list is sorted, and 66 comparisons were needed. This is the expected number: the length of the list is 12 and 12\times 11/2=66.
Now create a when[2]key_pressed that will result in the school_2 list being sorted. You can do this by copying the when[1]key_pressed script and carefully making the necessary changes.
i.Take a screenshot of your when[2]key_pressed script and paste it into your TMA document.
ii.Make sure the watcher for comparison_count is visible on the stage, then run your when[2]key_pressed script and note how many comparisons were required. Explain what number you were expecting and why, and state whether the result of running the script agrees with your calculation.
Note that you can repopulate the lists for the first and second schools, whenever required using the provided files school_1.txt and school_2.txt and following the procedure described on page 69 of Block 2, Book B, Part 4.
(5 marks)
b.Next you will write a script to merge the two sorted lists. The idea behind the merging algorithm is that we repeatedly compare the item at the front of the first list with the item at the front of the second list: whichever is the smaller is added to the merged list, then removed from its original list. At some point, one list will become empty and we can then simply append the contents of the other list to

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advanced Database Systems

Authors: Carlo Zaniolo, Stefano Ceri, Christos Faloutsos, Richard T. Snodgrass, V.S. Subrahmanian, Roberto Zicari

1st Edition

155860443X, 978-1558604438

More Books

Students also viewed these Databases questions

Question

What are the effects of tricyclic drugs?

Answered: 1 week ago

Question

=+ Why have these changes occurred?

Answered: 1 week ago

Question

What is the purpose of the Salary Structure Table?

Answered: 1 week ago

Question

What is the scope and use of a Job Family Table?

Answered: 1 week ago