An intro to programming concepts with Scratch - PowerPoint PPT Presentation

About This Presentation
Title:

An intro to programming concepts with Scratch

Description:

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 ... – PowerPoint PPT presentation

Number of Views:96
Avg rating:3.0/5.0
Slides: 27
Provided by: KimGl8
Learn more at: http://www.cse.msu.edu
Category:

less

Transcript and Presenter's Notes

Title: An intro to programming concepts with Scratch


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

2
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

3
Make a list of real students we will sort the
list (line) by height
  • Half the class lines up other half observes
  • Who is L1? L2? L8?
  • If L1 gt L2, swap positions else dont.
  • If L2 gt L3, swap positions else dont.
  • Continue
  • If L7 gt L8, swap positions else dont.
  • What is the result?

4
Start over at the front of the list and repeat
the process
  • If L1 gt L2, swap positions else dont.
  • If L2 gt L3, swap positions else dont.
  • Continue
  • If L6 gt L7, swap positions else dont.
  • Why dont we consider L8?
  • What is the result of these steps?

5
Start over again at the front and get the 3rd
tallest in L6
  • If L1 gt L2, swap positions else dont.
  • If L2 gt L3, swap positions else dont.
  • Continue
  • If L5 gt L6, swap positions else dont.

6
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.

7
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?

8
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.

9
Exercise sorting numbers
  • Sort the list of numbers using the bubble sort
  • 34, 17, 23, 19, 12

10
Sorting using selection sortoptional time
permitting
  • At each pass, find the shortest kid and swap to
    the front of the list.

11
Start with L1, L2, , L8
  • L1 walks down list comparing height to L2,
    L3,
  • When Lk lt L1, put kid L1 in position k and
    then kid Lk keeps going.
  • Repeat until the end of the list is reached.
  • The shortest kid now moves to L1

12
Selection sort pass 2
  • Pass one puts the shortest kid in position L1
  • We need to sort the remaining 7 kids in positions
    L2, L3, , L8
  • Use the same procedure as done in the first pass,
    but never consider L1
  • The result is that the 2nd shortest kid will be
    position at L2.

13
What about selection sort?
  • How many kid compares to position the shortest
    kid at the 1st position L1?
  • How many kid compares to position the 2nd
    shortest kid at L2?
  • How many total kid compares for the entire sort
    algorithm?
  • Is this algorithm better than bubble sort?

14
Algorithms take computer effort
  • Bubble sort and selection sort are good for
    sorting small lists, but there are better sorts
    for large lists.

15
Sorting a list in Scratch
  • Use a list variable
  • Use an algorithm with nested loops.
  • Difficult content time is needed.

16
first pass of bubble sort
  • Lets first input a list of numbers
  • (Then well sink the max of the list to the end
    of the list.)

17
Make list L and also a variable for its Length
Our list
18
Ask the user for list length after the user hits
key L
19
Repeat Length times, ask the user for item k of
the List
The user has given 3 items and is being asked for
item k4.
20
After the input loop is completed, state is shown
21
The sinking pass
  • Repeat 4 times
  • If kid Lk gt kid Lk1, swap them
  • Result is that tallest kid will be at the end L5

22
Largest number sinks to L5
The swap
23
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.

24
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

25
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.

26
Work on your own project!
  • Work on your storyline first, then do your
    coding. Work on your game idea first, then the
    coding.
Write a Comment
User Comments (0)
About PowerShow.com