Algorithmic Thinking and Structured Programming (in Greenfoot) Teachers: Renske Smetsers-Weeda Sjaak Smetsers Ana Tanase

Similar documents
Algorithmic Thinking and Structured Programming (in Greenfoot) Teachers: Renske Smetsers-Weeda Sjaak Smetsers

Algorithmic Thinking and Structured Programming (in Greenfoot) Teachers: Renske Smetsers-Weeda Sjaak Smetsers Ana Tanase

Algorithmic Thinking and Structured Programming (in Greenfoot) Teachers: Renske Smetsers-Weeda Sjaak Smetsers Ana Tanase

Algorithmic Thinking and Structured Programming (in Greenfoot) Teachers: Renske Smetsers-Weeda Sjaak Smetsers Ana Tanase

Algorithmic Thinking and Structured Programming (in Greenfoot) Teachers: Renske Smetsers-Weeda Sjaak Smetsers

Algorithmic Thinking and Structured Programming (in Greenfoot) Teachers: Renske Smetsers Sjaak Smetsers

Assignment 4: Dodo gets smarter

Assignment 3: Optimizing solutions

Assignment 8: Sokoban

Creating Java Programs with Greenfoot

CSCI 2212: Intermediate Programming / C Recursion

Sorting. Task Description. Selection Sort. Should we worry about speed?

To become familiar with array manipulation, searching, and sorting.

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 206 Introduction to Computer Science II

Lecture 6 Sorting and Searching

Topics Recursive Sorting Algorithms Divide and Conquer technique An O(NlogN) Sorting Alg. using a Heap making use of the heap properties STL Sorting F

October 6, SET-UP (Activate Prior Knowledge & Connect to Challenge Question)

Assignment 6: Dodo keeps getting smarter

CSE Winter 2015 Quiz 2 Solutions

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Algorithm for siftdown(int currentposition) while true (infinite loop) do if the currentposition has NO children then return

About this exam review

Assignment 7: Dodo s Race

Measuring algorithm efficiency

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

COMP2012H Spring 2014 Dekai Wu. Sorting. (more on sorting algorithms: mergesort, quicksort, heapsort)

CS 206 Introduction to Computer Science II

Assignment 1: Meet Dodo

Topic 14 Searching and Simple Sorts

QuickSort. CIS 15 : Spring 2007

Merge Sort. Algorithm Analysis. November 15, 2017 Hassan Khosravi / Geoffrey Tien 1

Unit-2 Divide and conquer 2016

Homework Assignment #3. 1 (5 pts) Demonstrate how mergesort works when sorting the following list of numbers:

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

LECTURE 5 Control Structures Part 2

Recurrences and Divide-and-Conquer

ITEC2620 Introduction to Data Structures

Assignment 1: Meet Dodo

Lesson 2.4 Arraylist

Information Science 1

S O R T I N G Sorting a list of elements implemented as an array. In all algorithms of this handout the sorting of elements is in ascending order

Divide & Conquer. 2. Conquer the sub-problems by solving them recursively. 1. Divide the problem into number of sub-problems

Data Structures And Algorithms

Active Learning: Sorting

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 17: Divide and Conquer Design examples.

CPSC 327, Spring 2019 Sample Answers to Homework #3

CS102 Sorting - Part 2

Control Structures II. Repetition (Loops)

Assignment 4: Question 1b omitted. Assignment 5: Question 1b clarification

CS61BL. Lecture 5: Graphs Sorting

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

Programming II (CS300)

Divide and Conquer Algorithms: Advanced Sorting. Prichard Ch. 10.2: Advanced Sorting Algorithms

Physics 2660: Fundamentals of Scientific Computing. Lecture 5 Instructor: Prof. Chris Neu

Data structures. More sorting. Dr. Alex Gerdes DIT961 - VT 2018

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

Errata for C++ and Algorithmic Thinking for the Complete Beginner

CMSC 341 Lecture 7 Lists

Merge Sort. Biostatistics 615/815 Lecture 11

Sorting. CSE 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation. CSE143 Au

CS 171: Introduction to Computer Science II. Quicksort

100 points total. CSE 3353 Homework 2 Spring 2013

2.3 Recursion. Overview. Greatest Common Divisor. Greatest Common Divisor. What is recursion? When one function calls itself directly or indirectly.

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

! New mode of thinking. ! Powerful programming paradigm. ! Mergesort, FFT, gcd. ! Linked data structures.

2.3 Recursion. Overview. Greatest Common Divisor. Greatest Common Divisor. What is recursion? When one function calls itself directly or indirectly.

07 B: Sorting II. CS1102S: Data Structures and Algorithms. Martin Henz. March 5, Generated on Friday 5 th March, 2010, 08:31

Computers and FORTRAN Language Fortran 95/2003. Dr. Isaac Gang Tuesday March 1, 2011 Lecture 3 notes. Topics:

CSCI-1200 Data Structures Spring 2017 Lecture 15 Problem Solving Techniques, Continued

COMP Data Structures

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

Chapter 1 Divide and Conquer Algorithm Theory WS 2014/15 Fabian Kuhn

ISY00245 Principles of Programming. Module 7

Definition: A data structure is a way of organizing data in a computer so that it can be used efficiently.

Chapter 12 Supplement: Recursion with Java 1.5. Mr. Dave Clausen La Cañada High School

Sorting. CSC 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation CSC Picture

Do you remember any iterative sorting methods? Can we produce a good sorting method by. We try to divide and conquer: break into subproblems

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

BM267 - Introduction to Data Structures

Linear Search. Sorting Algorithms. linear search code. Sorting in Ascending Order Selection Sort. Selection sort algorithm

8/2/10. Looking for something COMP 10 EXPLORING COMPUTER SCIENCE. Where is the book Modern Interiors? Lecture 7 Searching and Sorting TODAY'S OUTLINE

We will stamp HW Block day:

CSE 143 Lecture 14. Sorting

Better sorting algorithms (Weiss chapter )

CS61B Lecture #2. Public Service Announcements:

Announcements. Lab 11 is due tomorrow. Quiz 6 is on Monday. Ninja session tonight, 7-9pm. The final is in two weeks!

C/C++ Programming Lecture 18 Name:

Information Science 1

Searching for Information. A Simple Method for Searching. Simple Searching. Class #21: Searching/Sorting I

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

Divide and Conquer Sorting Algorithms and Noncomparison-based

School of Computing National University of Singapore CS2010 Data Structures and Algorithms 2 Semester 2, AY 2015/16. Tutorial 2 (Answers)

CS64 Week 5 Lecture 1. Kyle Dewey

Lecture Notes on Quicksort

Transcription:

Algorithmic Thinking and Structured Programming (in Greenfoot) Teachers: Renske Smetsers-Weeda Sjaak Smetsers Ana Tanase

Today s Lesson plan (7) Mid-Task Flowcharts (Real world example) Unplugged activity: sorting algorithms and efficiency Retrospective Theory: counter in while loop Assignments Next week (Feb 5 th ): Quiz

Flowcharts: Real world example

Unplugged Sorting algorithms and efficiency

Sort cards: Bogo Sort

Sort algorithms Goal: Sort cups using only a balance

Sort algorithms (in pairs, 3 minutes) Goal: Sort cups using only a balance order: lightest to heaviest nr of steps? Describe an algorithm (with a flowchart) using basic instructions which a 4-year-old should be able to follow: getcup ( thirdcup ) determinelightestcup ( thirdcup, seventhcup )

Sort algorithms: efficiency (2 minutes) Efficiency: Write down how many steps if you have: 10 cups 20 cups 100 cups

Sort algorithms Share: What did you come up with? Efficiency

Quick sort: divide and conquer 1) Select a card at random 2) Divide collection into two groups: A) larger than selected card B) smaller than selected card 3) Give each pile of cards to another team & sit back and relax 4) Other teams repeat steps 1-3 When are we done?

Quick sort: divide and conquer 0) If you have 0 or 1 card, then STOP 1) Select a card at random 2) Divide collection into two groups: A) larger than selected card B) smaller than selected card 3) Give each pile of cards to another team Other teams repeat steps 1-3 Result: cards sorted from smallest to largest Method: divide and conquer (recursive algorithm)

Quick sort summary Divide and conquer: Recursive programming Simple instructions Complexity n*log(n))

Quick sort summary Complexity O(n*log(n)): purple curve

How much better is QuickSort? https://www.youtube.com/watch?v=axxwxz5rf64

Computational thinking Working in a structured manner: Breaking problems down into subproblems Design, solve and test solutions to subproblems Combining these (sub)solutions to solve problem Analyzing the quality of a solution Reflecting about the solution chosen and proces Generalizing and re-use of existing solutions

Today s Lesson plan (7) Mid-Task Flowcharts (Real world example) Unplugged activity: algorithm efficiency Retrospective Theory: counter in while loop Assignments Next week (Feb 5 th ): Quiz

Retrospective Variables and Operators: Assignment: =, +=, Arithmetic: +,-,*, ++, Comparisons: <, ==, <= Tracing code

Variables and Values Assigning values eggsperbasket = 6; totaleggs = eggsperbasket + 3; eggsperbasket = eggsperbasket - 2; eggsperbasket++; //increase value by 1 Comparing values if ( totaleggs <= 6 ){ }

Variable Swapping strategy CODE VALUE OF a VALUE OF b VALUE of temp 12 4 12 4 4 12 4 12 12

Topics for today (finish assignment 5) Counter in while-loop Algorithms & efficiency

Variables and repetitions Use variables to remember things. to repeat something several times to remember how many times you already did it (or how times you still have to do it)

Repetition

Example: Mimi moves random times GetRandomNumber(N) will give a random number between 0 and N (N not included) Sketch how would you make Mimi move forward a random number of 0-9 cells (jumprandomly method) using: getrandomnumber(10) a variable to remember how many moves must be made Dodo s move() method

Nested if then else statements set nrcellstojump to getrrandomnumber(10) nrcellstojump equals 1 True move getrandomnumber(10) returns a number that has to be remembered False nrcellstojump equals 2 False nrcellstojump equals 3 False True True move move move move move End

Move a random number of times We use a (local) int variable with name nrcellstojump to store the random number getrandomnumber(10) returns a number that has to be remembered public void jumprandomly () { int nrcellstojump = Greenfoot.getRandomNumber(10); if (nrcellstojump == 1){ move(); } else if (nrcellstojump == 2){ } move(); move(); } else if (nrcellstojump == 3){ move(); move(); move(); }... Mind the difference: = (assignment) == (comparison)

alternative with while and counter set nrcellstojump to getrrandomnumber(10) Set nrofcellsmoved to 0 New variable nrofcellsmoved stores how many moves have been made so far False nrofcellsmoved is less than nrcellstojump move True increase nrofcellsmoved by 1 End

alternative with counter and while public void jumprandomly () { int nrcellstojump = Greenfoot.getRandomNumber(10); int nrcellsmoved = 0; while ( nrcellsmoved < nrcellstojump ){ move (); nrcellsmoved = nrcellsmoved + 1; } To store how many moves have been made so far. } The current value of nrcellsmoved...... incremented and assigned to nrcellsmoved

Comparing with(out) counter & while public void jumprandomly () { int nrcellstojump = Greenfoot.getRandomNumber(10); if (nrcellstojump == 1){ move(); } else if (nrcellstojump == 2){ move(); move(); } else if (nrcellstojump == 3){ move(); move(); move(); }... } public void jumprandomly () { int nrcellstojump = Greenfoot.getRandomNumber(10); int nrcellsmoved = 0; while ( nrcellsmoved < nrcellstojump ){ move (); nrcellsmoved = nrcellsmoved + 1; } }

Questions?

Quiz Next week Date: Feb 5 th Topics: Operators Conditions Return statements Nesting Decomposition/abstraction Flowcharts Variables Tracing code

Wrapping up Quiz on Feb 5 th Homework for Wednesday 8:30 Feb 3 rd : Assignment 5: FINISH assignment 5 ZIP code and IN and email to Renske.weeda@gmail.com