Page 253 - Learn To Program With Scratch
P. 253
Outer Loop
Inner Loop
Figure 9-24: The BubbleSort procedure
try it out 9-7
Make a list of names instead of numbers and use the bubble sort script to put the
list in order . Does the sort still work as it should? Also, what changes do you need
to make to the procedure to make it sort in ascending order?
Finding the Median
Median .sb2 Now that we know how to sort a list, we can easily find the median value of
any sequence of numbers. Recall that the median is the middle value in a
sorted set of numbers. If we have an odd number of items, we can just take
the middle number. If we have an even number, the median is the average
of the two middle numbers. We can describe the median for a sorted list of
N items as follows:
N +1
item at index 2 if N is odd
median =
averageofitems at N N and N + 1 if N iseven
2 2
A procedure that performs this calculation is shown in Figure 9-25. It
assumes the list is in order.
Lists 231
www.it-ebooks.info

