Answered step by step
Verified Expert Solution
Question
1 Approved Answer
This question focuses on Part 4 ( repetition ) , Part 5 ( components ) and Part 6 ( sorting ) . The teacher runs
This question focuses on Part repetition Part components and Part sorting
The teacher runs an afterschool 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 Append the second schools unsorted list to the end of the first schools unsorted list, then sort the combined list.
Strategy 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 TMQsb It contains three lists:
school and school 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
comparisoncount, 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 swappingpass and swappingpass and a whenkeypressed 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
aThe swappingpass component and the whenkeypressed script together form an implementation of bubble sort as discussed in Subsection of Block Part The list they will sort is school
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 to of Subsection where we observe that bubble sort will require approximately n 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
Make sure the watcher for comparisoncount is visible on the stage, then press the key to execute the whenkeypressed script.
You should now see that the school list is sorted, and comparisons were needed. This is the expected number: the length of the list is and times
Now create a whenkeypressed that will result in the school list being sorted. You can do this by copying the whenkeypressed script and carefully making the necessary changes.
iTake a screenshot of your whenkeypressed script and paste it into your TMA document.
iiMake sure the watcher for comparisoncount is visible on the stage, then run your whenkeypressed 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 schooltxt and schooltxt and following the procedure described on page of Block Book B Part
marks
bNext 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,
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started