Answered step by step
Verified Expert Solution
Question
1 Approved Answer
* I will attach the solution I have used which currently is not sorting the list. As the final script needs to be run in
I will attach the solution I have used which currently is not sorting the list. As the final script needs to be run in OU build is there a way of displaying the solution in this format?
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.
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 with that at position : if they are out of order then you swap them. Then you compare the name at position with that at position : if they are out of order then you swap them, and so on for the names at positions etc. You continue in this way until the name at position 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 will be the first in alphabetical order.
The second pass is similar, except that you begin with the name at position comparing it with the names at positions etc. When pass two is complete the names at and will be the first and second in alphabetical order.
In the third pass you compare the name at position with those at positions etc. When pass three is complete the names at and will be the first, second and third in alphabetical order.
Continuing in this way we will eventually compare the name in the nexttolast 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, as illustrated in Figure
cNext you will write a whengreenflagclicked script that sorts the list computerclub using exchange sort. Your script should implement the following algorithm, and must use the custom block swappos pos and the variables position and otherposition as indicated by the algorithm.
Set position to
Repeat until position equals length of computerclub
Set otherposition to position plus
Repeat until otherposition equals length of computerclub plus
If item at position is greater than item at otherposition
Swap item at position with item at otherposition
Increase otherposition by
Increase position by
Take a screenshot of your whengreenflagclicked script and paste it into your TMA document.
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