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.
She is considering ways to sort the list and is interested in a sorting algorithm she has recently heard about and which is new to her. The algorithm is called exchange sort, and she explains it like this.
In the first pass through the list you compare the name at position 1 with that at position 2: if they are out of order then you swap them. Then you compare the name at position 1 with that at position 3: if they are out of order then you swap them, and so on for the names at positions 4,5 etc. You continue in this way until the name at position 1 has been compared with the name at all other positions and swapped whenever necessary. When the first pass is complete the name that ends up at position 1 will be the first in alphabetical order.
The second pass is similar, except that you begin with the name at position 2, comparing it with the names at positions 3,4 etc. When pass two is complete the names at 1 and 2 will be the first and second in alphabetical order.
In the third pass you compare the name at position 3 with those at positions 4,5 etc. When pass three is complete the names at 1,2 and 3 will be the first, second and third in alphabetical order.
Continuing in this way we will eventually compare the name in the next-to-last position with that in the last position, swapping them if necessary, and at that point the entire list will be sorted.
This might sound a bit like bubble sort at first but if you look at the pairs that are compared in the first pass you can see that the pattern of comparisons is quite different.
So for example 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
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.
a.Suppose we want to sort the following list of names alphabetically:
Ira, Noah, Jabari, Thor, Ari
Write what the list will look like after:
o the first pass
o the second pass
o the third pass
o the fourth pass.
o
b. Next you will write a when_green_flag_clicked script that sorts the list computer_club using exchange sort. Your script should implement the following algorithm, and must use the custom block swap[pos1, pos2] and the variables position and other_position as indicated by the algorithm.
Set position to 1
Repeat until position equals length of computer_club
Set other_position to position plus 1
Repeat until other_position equals length of computer_club plus 1
If item at position is greater than item at other_position
Swap item at position with item at other_position
Increase other_position by 1
Increase position by 1
Take a screenshot of your when_green_flag_clicked script .

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 would you do?

Answered: 1 week ago