Page 252 - Learn To Program With Scratch
P. 252

5.  We’ll repeat the bubble sort process until no numbers are swapped dur-
                             ing a pass, meaning that our list is in order. The final three passes of
                             the algorithm are shown below:

                                 No
                               Change        No
                                            Change       Swap
                   Pass 3
                                                                       No
                                                                     Change        No
                                                                                  Change

                                 No
                                             Swap
                               Change
                                                          No
                   Pass 4
                                                        Change         No
                                                                     Change        No
                                                                                  Change

                                 No
                               Change        No
                                            Change        No
                   Pass 5
                                                        Change         No
                                                                     Change        No
                                                                                  Change

                             Now that you’ve seen how bubble sort works, let’s implement it in
                         Scratch. The script, shown in Figure 9-24, has two loops. The inner loop
                         cycles through a list, comparing and swapping as needed, and sets a flag
                         (named done) to 0 when another pass is needed. The outer loop repeats as
                         long as the done flag is 0, because that value means we aren’t done sorting.
                         If the inner loop completes one pass without swapping elements, the outer
                         loop will exit, ending the procedure.
                             Let’s explore this procedure in more detail. Since we haven’t done any
                         sorting yet, it sets done to 0 u. The outer loop uses a repeat until block to
                         pass through the list until it is sorted (that is, until done becomes 1) v. At
                         the beginning of every pass, this loop sets done to 1 w (that is, it assumes
                         that we won’t make any swaps). It also sets pos to 1 to start the sort with the
                         first number.
                             The inner loop then compares each pair of elements in the list. The
                         loop needs to perform N – 1 comparisons x, where N is the number of
                         items in the list.
                             If the item at index pos+1 is greater than the item at pos y, the two
                         need to be swapped. Otherwise, the procedure adds 1 to pos so it can com-
                         pare the next pair of items. If we do need to swap, this procedure does so
                         with the aid of a temporary variable named temp z.
                             Once the current pass through the list ends, the inner loop sets done
                         back to 0 if it swapped numbers or leaves done=1 if it made no changes {.
                         The outer loop will continue until the list is sorted.




           230   Chapter 9
                                           www.it-ebooks.info
   247   248   249   250   251   252   253   254   255   256   257