Top Banner
An intro to programming concepts with Scratch Session 8 of 10 sessions Working with lists; sorting a list
26

An intro to programming concepts with Scratch Session 8 of 10 sessions Working with lists; sorting a list.

Dec 14, 2015

Download

Documents

Mattie Marrs
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: An intro to programming concepts with Scratch Session 8 of 10 sessions Working with lists; sorting a list.

An intro to programming concepts with Scratch

Session 8of 10 sessions

Working with lists; sorting a list

Page 2: An intro to programming concepts with Scratch Session 8 of 10 sessions Working with lists; sorting a list.

Session 8 goals

• Learn how to represent a list• Learn how to sort a list of numbers• Learn how to do it in Scratch• Free time to work on your own project

Page 3: An intro to programming concepts with Scratch Session 8 of 10 sessions Working with lists; sorting a list.

Make a list of real students: we will sort the list (line) by height

• Half the class lines up; other half observes • Who is L[1]? L[2]? … L[8]?• If L[1] > L[2], swap positions; else don’t.• If L[2] > L[3], swap positions; else don’t.• Continue• If L[7] > L[8], swap positions; else don’t.• What is the result?

Page 4: An intro to programming concepts with Scratch Session 8 of 10 sessions Working with lists; sorting a list.

Start over at the front of the list and repeat the process

• If L[1] > L[2], swap positions; else don’t.• If L[2] > L[3], swap positions; else don’t.• Continue• If L[6] > L[7], swap positions; else don’t.• Why don’t we consider L[8]?

• What is the result of these steps?

Page 5: An intro to programming concepts with Scratch Session 8 of 10 sessions Working with lists; sorting a list.

Start over again at the front and get the 3rd tallest in L[6]

• If L[1] > L[2], swap positions; else don’t.• If L[2] > L[3], swap positions; else don’t.• Continue• If L[5] > L[6], swap positions; else don’t.

Page 6: An intro to programming concepts with Scratch Session 8 of 10 sessions Working with lists; sorting a list.

Switch the kids in line with the kids who are observing.

• Carefully go over the sorting by height.• Always compare only two adjacent kids. • Count the total number of kid compares.

Page 7: An intro to programming concepts with Scratch Session 8 of 10 sessions Working with lists; sorting a list.

Exercise: sorting 8 kids by height

• How many kid compares to get the tallest kid in list position 8?

• How many kid compares to get the next tallest kid in list position 7?

• How many compares to get all 8 kids in total height order?

Page 8: An intro to programming concepts with Scratch Session 8 of 10 sessions Working with lists; sorting a list.

You have discovered BUBBLE SORT (or SINKING SORT)

• A smaller kid “bubbles” up to the front of the list, one position each pass.

• The tallest kid sinks all the way to the end of the list in a single pass.

Page 9: An intro to programming concepts with Scratch Session 8 of 10 sessions Working with lists; sorting a list.

Exercise: sorting numbers

Sort the list of numbers using the bubble sort:

{34, 17, 23, 19, 12}

Page 10: An intro to programming concepts with Scratch Session 8 of 10 sessions Working with lists; sorting a list.

Sorting using selection sort[optional: time permitting]

At each pass, find the shortest kid and swap to the front of the list.

Page 11: An intro to programming concepts with Scratch Session 8 of 10 sessions Working with lists; sorting a list.

Start with L[1], L[2], …, L[8]

• L[1] walks down list comparing height to L[2], L[3], …

• When L[k] < L[1], put kid L[1] in position k and then kid L[k] keeps going.

• Repeat until the end of the list is reached.• The shortest kid now moves to L[1]

Page 12: An intro to programming concepts with Scratch Session 8 of 10 sessions Working with lists; sorting a list.

Selection sort pass 2

• Pass one puts the shortest kid in position L[1]• We need to sort the remaining 7 kids in

positions L[2], L[3], …, L[8]• Use the same procedure as done in the first

pass, but never consider L[1]• The result is that the 2nd shortest kid will be

position at L[2].

Page 13: An intro to programming concepts with Scratch Session 8 of 10 sessions Working with lists; sorting a list.

What about selection sort?

• How many kid compares to position the shortest kid at the 1st position L[1]?

• How many kid compares to position the 2nd shortest kid at L[2]?

• …• How many total kid compares for the entire sort

algorithm?• Is this algorithm better than bubble sort?

Page 14: An intro to programming concepts with Scratch Session 8 of 10 sessions Working with lists; sorting a list.

Algorithms take computer effort

Bubble sort and selection sort are good for sorting small lists, but there are better

sorts for large lists.

Page 15: An intro to programming concepts with Scratch Session 8 of 10 sessions Working with lists; sorting a list.

Sorting a list in Scratch

Use a list variable;Use an algorithm with nested loops.

Difficult content: time is needed.

Page 16: An intro to programming concepts with Scratch Session 8 of 10 sessions Working with lists; sorting a list.

first pass of bubble sort

Let’s first input a list of numbers;(Then we’ll sink the max of the list to

the end of the list.)

Page 17: An intro to programming concepts with Scratch Session 8 of 10 sessions Working with lists; sorting a list.

Make list L and also a variable for its Length

Our list

Page 18: An intro to programming concepts with Scratch Session 8 of 10 sessions Working with lists; sorting a list.

Ask the user for list length after the user hits key ‘L’

Page 19: An intro to programming concepts with Scratch Session 8 of 10 sessions Working with lists; sorting a list.

Repeat Length times, ask the user for item k of the List

The user has given 3 items and is being asked for item k=4.

Page 20: An intro to programming concepts with Scratch Session 8 of 10 sessions Working with lists; sorting a list.

After the input loop is completed, state is shown

Page 21: An intro to programming concepts with Scratch Session 8 of 10 sessions Working with lists; sorting a list.

The sinking pass

• Repeat 4 times• If kid L[k] > kid L[k+1], swap them• Result is that tallest kid will be at the end L[5]

Page 22: An intro to programming concepts with Scratch Session 8 of 10 sessions Working with lists; sorting a list.

Largest number sinks to L[5]

The swap

Page 23: An intro to programming concepts with Scratch Session 8 of 10 sessions Working with lists; sorting a list.

Swapping cars in 2-car garage

• Move car A to street (the copy)• Move car B to where car A was• Move car A from street to where car B was.• We have to swap computer memory contents

in the same way.

Page 24: An intro to programming concepts with Scratch Session 8 of 10 sessions Working with lists; sorting a list.

Exercise for later (hard)

• Change the sinking loop to be repeated variable M times.

• Wrap the sinking loop in an outer repeat loop.• The outer loop is repeated for k = 1, 2, 3, … ,

Length-1• The inner loop is repeated for M = Length – k

times

Page 25: An intro to programming concepts with Scratch Session 8 of 10 sessions Working with lists; sorting a list.

Exercise: test sorting lists of different lengths

• User inputs the Length• Programs asks for Length items from the user• When ‘s’ pressed, the sort algorithm sinks the

max to the end in Length-1 passes.

Page 26: An intro to programming concepts with Scratch Session 8 of 10 sessions Working with lists; sorting a list.

Work on your own project!

Work on your storyline first, then do your coding. Work on your game idea

first, then the coding.