1 CS 177 Week 12 Recitation Slides Running Time and Performance.

Slides:



Advertisements
Similar presentations
Announcements You survived midterm 2! No Class / No Office hours Friday.
Advertisements

CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
Recursion CS 367 – Introduction to Data Structures.
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
Arrays and ArrayLists Ananda Gunawardena. Introduction Array is a useful and powerful aggregate data structure presence in modern programming languages.
Zhang Hongyi CSCI2100B Data Structures Tutorial 2
CS 106 Introduction to Computer Science I 02 / 29 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
Introduction to Analysis of Algorithms
1 CS 177 Week 12 Recitation Slides Running Time and Performance.
CS 177 Recitation Week 8 – Methods. Questions? Announcements  Project 3 milestone due next Thursday 10/22 9pm  Turn in with: turnin –c cs177=xxxx –p.
Arrays Recitation – 10/(9,10)/2008 CS 180 Department of Computer Science, Purdue University.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Chapter 8 Search and Sort Asserting Java ©Rick Mercer.
1 CS 177 Week 15 Recitation Slides Review. Announcements Final Exam on Sat. May 8th  PHY 112 from 8-10 AM Complete your online review of your classes.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Abstract Data Types (ADTs) Data Structures The Java Collections API
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
Files COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
Lecture 2 Computational Complexity
Week 5 - Monday.  What did we talk about last time?  Linked list implementations  Stacks  Queues.
Sorting with Heaps Observation: Removal of the largest item from a heap can be performed in O(log n) time Another observation: Nodes are removed in order.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
COMP Recursion, Searching, and Selection Yi Hong June 12, 2015.
MIS215 Module 0 - Intro 1 MIS 215 Module 0 Intro to Data Structures.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Working with arrays (we will use an array of double as example)
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Recap form last time How to do for loops map, filter, reduce Next up: dictionaries.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Chapter 8 Search and Sort ©Rick Mercer. Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement.
Algorithms and data structures: basic definitions An algorithm is a precise set of instructions for solving a particular task. A data structure is any.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
Algorithms, Complexity and Sorting academy.zariba.com 1.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Week 12 - Friday.  What did we talk about last time?  Finished hunters and prey  Class variables  Constants  Class constants  Started Big Oh notation.
CS321 Data Structures Jan Lecture 2 Introduction.
Week 12 - Monday.  What did we talk about last time?  Defining classes  Class practice  Lab 11.
Multidimensional Arrays Computer and Programming.
CMPT 238 Data Structures Instructor: Tina Tian. General Information Office: RLC 203A Office Hour: Tue and Fri 12:30 - 2:00PM.
CSC 143T 1 CSC 143 Highlights of Tables and Hashing [Chapter 11 p (Tables)] [Chapter 12 p (Hashing)]
Problem of the Day  On the next slide I wrote today’s problem of the day. It has 3 possible answers. Can you guess which 1 of the following is the solution?
Loop Design What goes into coding a loop. Considerations for Loop Design ● There are basically two kinds of loops: ● Those that form some accumulated.
Complexity Analysis (Part I)
Introduction to Analysis of Algorithms
Week 13: Searching and Sorting
Analysis of Algorithms
Recitation 13 Searching and Sorting.
Introduction to Algorithms
CS 177 Week 15 Recitation Slides
What is CS 253 about? Contrary to the wide spread belief that the #1 job of computers is to perform calculations (which is why the are called “computers”),
CS 201 Fundamental Structures of Computer Science
Searching CLRS, Sections 9.1 – 9.3.
Data Structures (CS212D) Week # 2: Arrays.
Searching, Sorting, and Asymptotic Complexity
Revision of C++.
Complexity Analysis (Part I)
Algorithms and data structures: basic definitions
Complexity Analysis (Part I)
Presentation transcript:

1 CS 177 Week 12 Recitation Slides Running Time and Performance

2 Announcements Project 4 is due on Nov. 19th 9pm There will be an office hour next week in case that you have any question about your grade of project 3.

3 QUESTIONS???

Exam 2 Review Question 10 and 25’s correctness rates are less than 25%. Question 1, 3, 9, 15’s are less than 50%.

10. Consider the following method: public static int changeItUp(int x, int[] y) { int z; x *= x; y[0] /= 3; z = x - y[1]; return z; } Given the method call below, what does the code print? int x = 5; int[] y = {12, 7}; int z = changeItUp(x, y); System.out.println("(x, y0, y1, z) are (" + x + ", " + y[0] + ", " + y[1] + ", " + z + ")"); (a) (x, y0, y1, z) are (25, 12, 8, 18) (b) (x, y0, y1, z) are (25, 4, 7, 18) (c) (x, y0, y1, z) are (5, 12, 7, 18) (d) (x, y0, y1, z) are (5, 4, 7, 18)

25. Let char[][] board = new char[3][3]; represent the tic tac toe game board. Given the following code fragment that prints out the winning message when ’X’ wins, which pair of board content and print out message is NOT correct. for( int i = 0; i < board.length; i++) { if((board[i][0] == ’X’) && (board[i][1] == ’X’) && (board[i][2] == ’X’)) { System.out.println("\’X\’ wins with the " + (i+1) + "th row!"); } else if((board[0][i] == ’X’) && (board[1][i] == ’X’) && (board[2][i] == ’X’)) { System.out.println("\’X\’ wins with the " + (i+1) + "th column!"); } if( (board[0][0] == ’X’) && (board[1][1] == ’X’) && (board[2][2] == ’X’)) { System.out.println("\’X\’ wins with the \\ cross"); } else if ( (board[0][2] == ’X’) && (board[1][1] == ’X’) && (board[2][0] == ’X’)) { System.out.println("\’X\’ wins with the / cross"); }

(a) Board: X - - O X O - - X Winning message: ’X’ wins with the \\ cross (b) Board: X X X O O - Winning message: ’X’ wins with the 1th row! (c) Board: - X O - X - O X - Winning message: ’X’ wins with the 2th column! (d) All are correct

1. Consider the following code: String[] rand_days = {"Monday", "Friday", "Sunday"}; for(int i = 0; i < rand_days.length(); i++) { System.out.print(rand_days.length + ", "); } Output will be: (a) 3, 3, 3, (b) There is no output due to run time error (c) There is no output due to compile time error (d) 6, 6, 6,

3. Why does the code below cause compile time error? int[] list = {1, 2, 3, 4}; int sum = 0; for (int i = 0; i < list.length; i++); sum += list[i]; System.out.println(sum); (a) The variable list is reference outside its scope (b) The variable i is referenced outside its scope (c) The variable list should have been initialized using the new operator (d) All of the above

9. Given the method, mystery(), defined below, what does the integer b contain if we assign mystery(36) to it? public static int mystery(int n) { int i = 0; for (int j = 2; j <= n/2; j++) { if (n != j && n%j == 0) { i++; } return i; } (a) 0 (b) 5 (c) 7 (d) 6

15. Given the code below: public class Person { String name; } Which of the following is correct about the variable name? (a) name is a local variable and an instance of the String class. (b) name is a local variable and a primitive data type String. (c) name is a member of the Person class and a primitive data type String. (d) name is a member of the Person class and an instance of the String class.

Algorithm Design Get the right answer  Obviously, the most important thing Use a reasonable amount of memory  Not so important because of the rapidly growing memory sizes  But we still only have limited resources, esp. small electronic devices like cell phones have very limited memory Run as quickly as possible  0.1 sec and 0.3 sec make little difference  What about 10 min and 30 min?  10 years and 30 years?

Data Structure In early days, algorithm + data structure = program Although not really these days, they are still the most important parts of programs The graph is a typical and extremely useful data structure Actually data structures are everywhere: strings, arrays, linked lists, classes There are more complicated data structures like trees and hash tables which you will learn later

O Big Oh analysis is not precise estimation It only gives an idea how slow the program will become when n is larger and larger A simple way to determine the Big Oh value is to check the number of embedded loops for x = 1..n for y = 1..n O(n 2 ) for x = for y = O(1) for x = 1..n 3 for y = 1..n 0.5 for z = 1..n O(n 4.5 )

Sorting In general, sorting algorithms are O(n 2 ) because they typically involve comparing each value to each other value (a loop inside a loop) to get each value into the proper position. (like bubble sort) But the best sorting algorithms are O(n logn). (like quick sort) It makes difference when n is large enough. For example, 1 million.

Runtime Analysis It’s also possible to test your program’s performance by running it many times. It’s hard to get the exact Big Oh value (why?) but you can get a curve about how rapidly your program slows down when the data size increases  Because Big Oh is not precise, especially when n is not big enough You can learn more sophisticated ways to do run time analysis in algorithm courses

Space Analysis Usually, the memory sizes nowadays are sufficient for you program. However, sometimes, the space cost is extremely large.  What about sorting all bytes in your hard drive? Before you start designing “fast” algorithms, please consider the space cost first Java is convenient for its garbage collector, which saves you a lot of effort by helping clean useless objects, arrays, etc. Sometimes, there’s a tradeoff between time and space.

Example for Algorithm Design Search an integer in a sorted array of integers with ascending order Obvious solution: check the integers one by one until you find the wanted one  Time cost: O(n)  Space cost: O(n) Better solution: binary search  Time cost: O(log n) (why?)  Space cost: O(n)

19 Final QUESTIONS???