Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Screenshot from my working attached please add scripts which should be added to the script from a screenshot. If we just had a list of
Screenshot from my working attached please add scripts which should be added to the script from a screenshot.
If we just had a list of the three names
Tallulah, Juan, Ivan
the algorithm would compare Tallulah with Juan and because Juan comes before Tallulah alphabetically, they would be swapped, giving
Juan, Tallulah, Ivan OUBuild
File
TMQ
comparisoncount
position
otherposition
r
N
show variable comparisoncount
hide variable comparisoncount
Sprites
New sprite:
Make a List
computerclubreplace item pos of computerclub grad with item pos of computerclub gradreplace item pos of computerclub grad with tmprepeat until position length of computerclubset otherposition grad to position repeat until otherposition length of computerclub If item position of computerclub grad item otherposition of computerclub grad thenchange position grad by add thing to computerclub delete of computerclub gradNew baddrop:insert thing at grad of computerreplace item of sqrt computerc "item of computerclub length of computerclub computerclub grad contains thingshow list computerclub
Juan would then be compared with Ivan, which would need another swap. This would complete the first pass and we would have
Ivan, Tallulah, Juan
For the second pass we would start with Tallulah and compare it with Juan. Juan comes before Tallulah in alphabetical order, so they would be swapped. At the end of the second pass we would have
Ivan, Juan, Tallulah
and the list would now be sorted.
In this part you will investigate how many comparisons between list items exchange sort requires to sort some lists of different lengths.
iTo count the comparisons, we have provided a variable comparisoncount.
The only place where list items are compared is in the whengreenflagclicked script. Add a block to this script at an appropriate point to increment comparisoncount each time two list items are compared.
We are not quite done, because comparisoncount needs to be initialised to at the start of the whengreenflagclicked script, before we start the sort. Add a block to initialise comparisoncount at an appropriate point in the script.
Then make sure the watcher for comparisoncount is visible on the stage, and run the whengreenflagclicked script.
You should find that once the script has run the value of comparisoncount is
Take a screenshot
In this part you will explore the number of comparisons needed for exchange sort from a theoretical perspective.
Let n be the number of items to sort and consider the case n
The st item will be compared with the ndrdth etc. item, and so on up to the th item, a total of comparisons.
The nd item will be compared with the rdthth etc. item, and so on up to the th item, a total of comparisons.
The rd item will be compared with the ththth etc. item, and so on up to the th item, a total of comparisons.
The sort continues in this way until the th item is reached. This is compared with the th item, taking just comparison, and the algorithm will now finish.
This requires a total of comparisons. We could just add these numbers together but there is a neater way. You can probably see that their average is and as there are of them the total must be times just as we found in part di
In the general case with n items the average will be n and there will be n numbers, giving a total of n times n comparisons.
iHow does the number of comparisons needed by exchange sort compare with that required by bubble sort
ii Although in the worst case bubble sort can require n times n comparisons, the same as exchange sort, bubble sort has a particular feature that means it will generally take fewer comparisons. Explain what this feature is and describe what kind of data it makes bubble sort useful for sorting.
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