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

Slides:



Advertisements
Similar presentations
SortingTechniques. Bubble-sort: One of the simplest sorting methods. The basic idea is the weight of the record. The records are kept in an array held.
Advertisements

Summer Computing Workshop. Introduction to Variables Variables are used in every aspect of programming. They are used to store data the programmer needs.
QuickSort Example 13, 21, 15, 3, 12, 9, 14, 7, 6 3, 3, 9, 3, 9, 7, 3, 9, 7, 6, First we use the number in the centre of the list as a ‘pivot’. We then.
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Topic 16 Sorting Using ADTs to Implement Sorting Algorithms.
Introduction to Recursion and Recursive Algorithms
The simple built-in data types of the C language such as int, float, - are not sufficient to represent complex data such as lists, tables, vectors, and.
Bubble Sort Algorithm 1.Initialize the size of the list to be sorted to be the actual size of the list. 2.Loop through the list until no element needs.
Garfield AP Computer Science
Decision Maths 1 Sorting Algorithms Bubble Sort A V Ali : 1.Start at the beginning of the data set. 2.Compare the first two elements,
Algorithms (continued)
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Visual C++ Programming: Concepts and Projects
CSE 373: Data Structures and Algorithms
Simple Sorting Algorithms
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
Complexity (Running Time)
Algorithm Efficiency and Sorting
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Programming Logic and Design Fourth Edition, Comprehensive
Searching and Sorting Arrays
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
CSE 373 Data Structures and Algorithms
Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion.
CSE 373: Data Structures and Algorithms Lecture 6: Sorting 1.
2015 CSE/EGR Summer Camps 1 Computer Science Concepts 1. What is an algorithm? 2. Binary information coding 3. Programming concepts via Scratch Designed.
Chapter 6: Arrays: Lists and Tables
Simple Iterative Sorting Sorting as a means to study data structures and algorithms Historical notes Swapping records Swapping pointers to records Description,
1 Today’s Material Iterative Sorting Algorithms –Sorting - Definitions –Bubble Sort –Selection Sort –Insertion Sort.
BUBBLE SORT. Introduction Bubble sort, also known as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to.
Sorts Tonga Institute of Higher Education. Introduction - 1 Sorting – The act of ordering data Often, we need to order data.  Example: Order a list of.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
Sorting. 2 contents 3 kinds of sorting methods – Selection, exchange, and insertion O(n 2 ) sorts – VERY inefficient, but OK for ≈ 10 elements – Simple.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Sorting an array bubble and selection sorts. Sorting An arrangement or permutation of data An arrangement or permutation of data May be either: May be.
3 – SIMPLE SORTING ALGORITHMS
Sorting and Searching. Selection Sort  “Search-and-Swap” algorithm 1) Find the smallest element in the array and exchange it with a[0], the first element.
ALGORITHMS.
SORTING ALGORITHMS King Saud University College of Applied studies and Community Service CSC 1101 By: Nada Alhirabi 1.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
Chapter 9: Sorting and Searching Arrays
Introduction to Search Algorithms
Chapter 9: Searching, Sorting, and Algorithm Analysis
Simple Sorting Algorithms
Algorithm Analysis CSE 2011 Winter September 2018.
Bubble, Selection & Insertion sort
Analysis of Bubble Sort and Loop Invariant
“Human Sorting” It’s a “Problem Solving” game:
Bubble Sort Key Revision Points.
Principles of Computing – UFCFA3-30-1
Sorting "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Sorting Example Bubble Sort
The structure of programming
Principles of Computing – UFCFA3-30-1
Thinking procedurally
“Human Sorting” It’s a “Problem Solving” game:
CHAPTER 9 SORTING & SEARCHING.
Sorting and Complexity
Presentation transcript:

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

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?

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?

Start over again at the front and get the 3 rd 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.

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.

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?

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.

Exercise: sorting numbers Sort the list of numbers using the bubble sort: {34, 17, 23, 19, 12}

Sorting using selection sort [optional: time permitting] At each pass, find the shortest kid and swap to the front of the 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]

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 2 nd shortest kid will be position at L[2].

What about selection sort? How many kid compares to position the shortest kid at the 1 st position L[1]? How many kid compares to position the 2 nd shortest kid at L[2]? … How many total kid compares for the entire sort algorithm? Is this algorithm better than bubble sort?

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

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

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

Make list L and also a variable for its Length Our list

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

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.

After the input loop is completed, state is shown

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]

Largest number sinks to L[5] The swap

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.

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

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.

Work on your own project! Work on your storyline first, then do your coding. Work on your game idea first, then the coding.