SCRATCH MODULE 2: SORTING - MacEwan University

SCRATCH MODULE 2: SORTING

INTRODUCTION

The purpose of this module is to experiment with repetition, variables, and lists in Scratch. We will be exploring these ideas while implementing two sorting algorithms. Our program will create a list of 10 randomly selected numbers and allow the user to modify the list by adding additional elements, changing existing elements, or deleting elements. When the list is ready, the user will select either `Selection sort' or `Bubble sort'. At that point, the original list is copied to a secondary list and the chosen sort is performed. A timer is used to indicate how long the sort took. After this, the user may choose to perform the other sorting algorithm or modify the original list and repeat the process once again.

It is assumed that students have successfully completed the `Scratch Tutorial' and `Scratch ? Module 1' prior to beginning this module. Students must have sufficient knowledge of the Scratch working environment to create, modify, and execute scripts as well locate blocks within categories and modify them according to directions and examples provided.

It is recommended that students follow these instructions closely to achieve the desired end product. Personalization of the project is encouraged but better done once these instructions have been completed to ensure all outcomes have been accomplished.

NEW CONCEPTS COVERED

Variables:

Create / Hide / Show / Set / Change / Scope Using random numbers Counting

Page 1

Lists:

Create / Hide / Show / Scope Add items to a list / Insert into list Replace items in a list / Delete items from a list Retrieve a single item from a list Determine the number of items in a list Use a counter variable to access individual items in a list

Repetition:

Repeat / Repeat until Comparison operators: < / > / =

Timer:

Reset timer Use a timer variable to keep track of elapsed time

STARTING SCRATCH

Start Scratch. Create a new Scratch project by selecting `File' > `New'. Before we do anything, let's save the file by selecting `File' > `Save as'. Save your file as `Module 2 ? XY' where X is the initial of your first name and Y is the initial of your last name. Remember to save to your network drive! If this doesn't work, please ask your Lab Instructor for assistance.

PART 1: SETTING THE STAGE

The best way to learn Scratch is through experimentation. As it is an interactive environment, feel free to stop and experiment as you work through this module.

CREATING THE SPRITES

Let's get started ...

Sprite1 (the cat): At the moment, our current sprite is Sprite1 (the cat). For the

moment, we don't need him. Double-click the category to hide him.

block under the

Sprite2 (title): We need to create a title for our game. Select the first option to `Paint new sprite'. Select

the Text tool

in the Paint Editor screen and

type in a title such as `Sorting Algorithms'. If you can't see your text, grab the

little black box to the left of the text and drag the text to where you can see it.

You can change the text color (perhaps to orange) and size (to something like

24) if you want. When ready, click `OK'.

Position the title properly in the top center of the stage. Use Figure 1a to help you. Adjust the coordinates if necessary.

Figure 1a

Page 2

Sprite3 (instructions): We need to give the user instructions for operating our game. Once again, select the first option to `Paint new sprite'. Using the Text tool, type in the instructions `Finalize the original list. Click a sort button to begin.' Grab the little black box to the left of the text and drag the text to where you can see it. If you can't quite see all of it, click on the magnifying glass with the minus sign in it to zoom out.

Once you can read all of it, use the `Enter' key between the two sentences to separate them so they appear on two different lines. You may also want to use the spacebar to center the first one above the second. Change the size to something smaller (like 12) before continuing. When done, click `OK'.

Build the script in Figure 1a but use x = 10, y = -140. Adjust the coordinates as needed to center the instructions just above the bottom of the screen.

Sprite4 (button): We need to give the user a button for the first sort. Once again, select the first option to `Paint new sprite'. Using the Text tool, type `Selection Sort'. Change the size to something smaller (like 12). Then click on the Rectangle

tool

and draw a rectangle over `Selection Sort'. Click on the Fill tool

and use the eyedropper to select a color from the palette (like light blue) and then

click on the rectangle to change its color. You should now see the words clearly.

If the words are not centered, you can reselect the Text tool and reposition the

words within the rectangle accordingly. When done, click `OK'.

To position the button to the left and slightly underneath the title, use the script from Figure 1a with x = -157, y = 131.

Sprite5 (button): Now we need a button for the second sort. Since this button is almost identical to the previous one, right click on the previous button in the Sprite List and `Duplicate' it. Click on the `Costumes' tab for Sprite5 and `Edit' costume1 so it says `Bubble Sort' instead. When finished, click `OK'. Finish up by going to the `Scripts' tab and editing the position to x = 175, y = 131. Click the green flag to position the sprites on stage.

Page 3

PART 2: PREPARING THE DATA

Before we can start sorting, obviously we need something to sort. The main source of data for our program will be a list we'll call `Original'. The program should fill it with 10 random numbers to start with but allow the user to modify those numbers, add extra numbers, and delete numbers already present.

Accessing information in a list can be a bit tricky. Since a list can store multiple items, retrieving an item means specifying where the item is in the list. Consider our `Original' list with a maximum of 10 items. The first item would be in position 1, the second in position 2, and so on with the last item in position 10. If I wanted to retrieve the fourth item in the list, I would need to indicate that I wanted the item in position 4. If I wanted to retrieve all items in a list, I would need to first retrieve the item in position 1, then the one from position 2, and so on until all items have been retrieved. When we wish to access all items in a list one by one, we will typically use a loop with a counter variable. You will see this often throughout the remainder of this module.

Lists, just like variables, are not sprites in Scratch. Instead, they belong to all sprites at once. We will use different Sprites to manage the lists. Time to set up our data ...

CREATING THE LISTS

Original: Select the Stage from the Sprite List to make the Stage active. We are going to have the Stage set up the `Original' list before the sorts commence. From the Block

Descriptions, select

.

Anything pertaining to a variable or

list will be found in this category.

Choose

and fill in the

information as shown in Figure 2a.

Click on `OK' when ready.

Figure 2a

Now you should see the `Original' list in the upper right corner of the stage. It is empty and has shows `length: 0' because there are no elements in the list. Grab the `Original' list and move it so it appears centered directly below the title. Now it's ready to use.

List A: We will use a separate list when we perform `Selection Sort' called `List A'.

Choose

and name it `List A' when prompted. Reposition the list so it

appears centered underneath the `Selection Sort' button.

List B: You can probably guess what happens next. `List B' is the list we'll use when

we perform `Bubble Sort'. Once again, choose

and name it `List B'.

Reposition the list so it appears centered underneath the `Bubble Sort' button.

Page 4

CREATING A VARIABLE

Size: To keep track of how many items are in the current list, we'll make a variable called `Size'. From the Block Descriptions, select

. Choose

and fill in the information as shown in Figure 2b. Click on `OK' when ready.

Figure 2b

Now you should see

in the upper left corner of the stage. It appears

because

has the check mark selected. By clicking the check mark, you

can unselect it and the variable will disappear from the stage. Do this now. We

don't want the user to see Size.

FILLING THE LIST WITH RANDOM NUMBERS

Now it's time to fill the `Original' list with 10 random numbers. The random numbers can be anything between 1 and 100. We will do this by picking a random number between 1 and 100 and then inserting it somewhere in the `Original' list and then repeating this

process 10 times. Use blocks from the

,

, and

categories for the next section.

Setting a value: Re-select the Stage from the Sprite List if it is not currently active. We'll start by setting the desired amount of elements allowed in the list to 10. Create the script shown in Figure 2c.

Figure 2c

Deleting / Inserting elements:

Now we will delete all

elements in the list (a good

habit to ensure the list is empty) and insert a random

Figure 2d

number between 1 and 100. Expand the script from Figure 2c with that of

Figure 2d. Be certain that you are changing the `Original' list.

Each time you click the green flag, you will see your `Original' list change. One number will be shown and the list `length' will show `1'. To put all 10 elements in the list, we need to perform the insert process 10 times. What we need is a loop...

Page 5

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download