Answered step by step
Verified Expert Solution
Link Copied!

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 r
TM111_02_Q4
computerclub?1Diya2Aiden3Quinlan4Fatima5Mei6Soledad7Zoltan8Oksana9Isla10Ulrike+length:10
comparison_count
0
position 1
other_position
2
ScriptsCostumesMotionEoundsLooksControlSoundSensingPenOperatorsData?MakeaVariable?gradcomparisoncount?gradotherposition?gradposition?gradtmp?setcomparisoncountgradto0?changecomparisoncountgradby?
r-7.].
N
show variable comparison_count
hide variable comparison_count
Sprites
New sprite:
Make a List
computer_clubreplace item pos1) of computer_club grad with item pos2 of computer_club gradreplace item pos2 of computer_club grad with tmprepeat until (position = length of computer_clubset other_position grad to position +1)repeat until other_position = length of computer_club +1If item position of computer_club grad )> item other_position of computer_club grad ) thenchange position grad by 1add thing to computer_club delete 17 of computer_club gradNew baddrop:insert thing at 1grad of computer_replace item 17 of sqrt" computer_c "item 17 of computer_club 7length of computer_club 7computer_club grad contains thingshow list computer_club 7
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.
i.To count the comparisons, we have provided a variable comparison_count.
The only place where list items are compared is in the when_green_flag_clicked script. Add a block to this script at an appropriate point to increment comparison_count each time two list items are compared.
We are not quite done, because comparison_count needs to be initialised to 0 at the start of the when_green_flag_clicked script, before we start the sort. Add a block to initialise comparison_count at an appropriate point in the script.
Then make sure the watcher for comparison_count is visible on the stage, and run the when_green_flag_clicked script.
You should find that once the script has run the value of comparison_count is 45.
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 =10.
The 1st item will be compared with the 2nd,3rd,4th etc. item, and so on up to the 10th item, a total of 9 comparisons.
The 2nd item will be compared with the 3rd,4th,5th etc. item, and so on up to the 10th item, a total of 8 comparisons.
The 3rd item will be compared with the 4th,5th,6th etc. item, and so on up to the 10th item, a total of 7 comparisons.
The sort continues in this way until the 9th item is reached. This is compared with the 10th item, taking just 1 comparison, and the algorithm will now finish.
This requires a total of 9+8+7+6+5+4+3+2+1 comparisons. We could just add these 9 numbers together but there is a neater way. You can probably see that their average is 10/2=5 and as there are 9 of them the total must be 9\times 5=45, just as we found in part (d)(i).
In the general case with n items the average will be n /2 and there will be n 1 numbers, giving a total of (n 1)\times n /2 comparisons.
i.How does the number of comparisons needed by exchange sort compare with that required by bubble sort 1?
ii. Although in the worst case bubble sort 3 can require (n 1)\times n /2 comparisons, the same as exchange sort, bubble sort 3 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 3 useful for sorting.
image text in transcribed

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

Step: 3

blur-text-image

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

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions

Question

What is cooperative advertising? What sorts of firms use it?

Answered: 1 week ago