University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Yet More Array Practice Lecture 24, Mon Mar 15 2010.

Slides:



Advertisements
Similar presentations
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Advertisements

Q-1 University of Washington Computer Programming I Lecture 16: Sorting © 2000 UW CSE.
CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.
Chapter 9: Searching, Sorting, and Algorithm Analysis
CS 106 Introduction to Computer Science I 02 / 29 / 2008 Instructor: Michael Eckmann.
CompSci Searching & Sorting. CompSci Searching & Sorting The Plan  Searching  Sorting  Java Context.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Advanced Class Design Lecture 19, Thu Mar
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
Simple Sorting Algorithms
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Arrays II Lecture 15, Thu Mar
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
Searching and Sorting SLA Computer Science 4/16/08 Allison Mishkin.
Searching and Sorting Copyright Prentice Hall (with modifications by Evan Korth)
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 2D Arrays, Sorting Lecture 16, Tue Mar
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 20, Fri Mar
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
The Efficiency of Algorithms
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Arrays II Lecture 22, Wed Mar
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.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design Lecture 30, Mon Mar
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar
Algorithm Efficiency and Sorting
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Interfaces, Polymorphism Lecture 29, Fri Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 2D Arrays, Sorting Lecture 23, Fri Mar
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Arrays and Class Design Lecture 18, Tue Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Arrays Lecture 21, Mon Mar
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops II Lecture 18, Mon Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design III, Parameter/Scope Review Lecture.
Abstract Data Types (ADTs) Data Structures The Java Collections API
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
{ CS203 Lecture 7 John Hurley Cal State LA. 2 Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Chapter 19 Searching, Sorting and Big O
Searching and Sorting Topics Sequential Search on an Unordered File
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
CPSC 121: Models of Computation Unit 0 Introduction George Tsiknis Based on slides by Patrice Belleville and Steve Wolfman.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
© 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
The median again The steps of our algorithm: Read the size of the list, N. Declare and instantiate an array of integers, "list". Read the elements of list.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
CS 2430 Day 30. Announcements Quiz #5: 4/19 Agenda Big O Searching –Linear search.
1 BUILDING JAVA PROGRAMS CHAPTER 7.2 ARRAY TRAVERSAL ALGORITHMS.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
Q-1 University of Washington Computer Programming I Lecture 16: Sorting © 2000 UW CSE.
Searching Topics Sequential Search Binary Search.
University of Macau Faculty of Science and Technology Programming Languages Architecture SFTW 241 spring 2004 Class B Group 3.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
C++ Programming Lecture 16 Arrays – Part III The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 9: Arrays; Revision Session.
Searching and Sorting Copyright Prentice Hall (with additions / modifications by Evan Korth)
Copyright Prentice Hall Modified by Sana odeh, NYU
EGR 2261 Unit 10 Two-dimensional Arrays
Introduction to Search Algorithms
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Yet More Array Practice Lecture 24, Mon Mar borrowing from slides by Kurt Eiselt

2 Reading: CORRECTION! n This week: Sorting and Searching n 14.1 and 14.3 in 3rd edition n (19.1 and 19.3 in 2nd edition) n so weekly question is indeed required! n course web page has also been updated

3 Midterm 2 n Midterm 2: Mon Mar 22, 6:30pm n FSC 1005 again n hour-long exam, reserve 6:30-8 time slot n for buffer in case of fire alarms etc n coverage: through arrays (Chap 8) n includes/builds on material covered in previous midterm n study tips - same as before! n write and test programs, not just read book n try programming exercises from book

4 Reading through Midterm, 3rd edition n n n n 4 n n n 7.1, n 14.1,14.3 n see course page for 2nd edition list

5 Recap: Arrays of Arrays = 2D Arrays n 2D array often easier to think about n Internally, 2D arrays implemented as arrays of arrays in Java n they’re equivalent rows columns array of arrays 2D array

6 Recap: 2D Array Access Patterns n Print average score for each student n for each row of scores n add up scores n divide by number of quizzes n length of row scores rows: students cols: quizzes for (int row = 0; row < scores.length; row++) { double average = 0; for (int col = 0; col < scores[row].length; col++) { average = average + scores[row][col]; } average = average / scores[row].length; } for (int col = 0; col < scores[0].length; col++) { double average = 0; for (int row = 0; row < scores.length; row++) { average = average + scores[row][col]; } average = average / scores.length; } n Print average score for each quiz n for each column of scores n add up scores n divide by number of students n length of column

7 Recap: Per-Student Averages public class ArrayEx4 { public static void main(String[] args) { double[][] scores = {{95, 82, 13, 96}, {51, 68, 63, 57}, {73, 71, 84, 78}, {50, 50, 50, 50}, {99, 70, 32, 12}}; double average; // here's where we control looping row by row (student by student) for (int row = 0; row < scores.length; row++) { average = 0; // and here's where we control looping through the columns // (i.e., quiz scores) within each row for (int col = 0; col < scores[row].length; col++) { average = average + scores[row][col]; } average = average / scores[row].length; System.out.println("average of row " + row + " is " + average); }

8 Recap: Selection Sort The smallest value so far is 12 Its index is 4 n Start at beginning n Consider unsorted array elements: beyond current spot n Find smallest element n Swap with current spot n Move down by one

9 n now that we know 2D, we can do nD! n any number of dimensions: 3D, 4D… n up to 127D, actually n example: student quiz scores over multiple terms n row: students n col: quiz scores n stack: term n let's try it! Multidimensional Arrays Term Term Term Term2

10 Now for the good stuff Computer science folks don't spend all their time writing programs. They're also concerned with the efficiency of those programs and their underlying algorithms. Efficiency can be expressed in terms of either time or memory needed to complete the task. In the case of sorting algorithms, we're typically interested in how much time it takes to sort. So let's try to get some sense of the time requirements of selection sort. We don't use a stopwatch...instead, we use mathematics. The fundamental operation in sorting is the comparison to see if one value is less than the other, and the time required to sort corresponds to the number of comparisons that must be made to complete the sorting.

11 Estimating time required to sort We can go back to the selection sort example and count the comparisons. The first pass through the array of 5 elements started with 16 being compared to 3, then 3 was compared to 19, 8, and 12. There were 4 comparisons. The value 3 was moved into the location at index 0.

12 Estimating time required to sort We can go back to the selection sort example and count the comparisons. The first pass through the array of 5 elements started with 16 being compared to 3, then 3 was compared to 19, 8, and 12. There were 4 comparisons. The value 3 was moved into the location at index 0. Then the second pass through the array began, starting with index was compared to 19, then 16 was compared to 8, which became the new minimum and was compared to 12. So among 4 elements in the array, there were 3 comparisons.

13 Estimating time required to sort It takes 4 passes through the array to get it completely sorted. There are 4 comparisons on the first pass, 3 comparisons on the second pass, 2 comparisons on the third pass, and 1 comparison on the last pass. That is, it takes = 10 comparisons to sort an array of five values. If you do this same computation on an array with six values, you'll find it takes = 15 comparisons to sort the array. Do you see a pattern? With a little math, you can figure out that the number of comparisons required to perform selection sort on an array of N values is given by the expression: N*(N-1)/2 or (N 2 -N)/2

14 Estimating time required to sort Either way, it should be easy to see that as N, the number of values in the array gets very big, the number of comparisons needed to sort the array grows in proportion to N 2, with the other terms becoming insignificant by comparison. So sorting an array of 1,000 values would require approximately 1,000,000 comparisons. Similarly, sorting an array of 1,000,000 values would take approximately 1,000,000,000,000 comparisons. As the number of values to be sorted grows, the number of comparisons required to sort them grows much faster. Fortunately, there are other sorting algorithms that are much less time-consuming, but we won't be talking about them in this class. In the meantime, here are some real numbers to help you think about just how long it might take to sort some really big arrays...

15 Estimating time required to sort Let's assume that your computer could make 1 billion (1,000,000,000) comparisons per second. That's a lot of comparisons in a second. And let's say your computer was using selection sort to sort the names of the people in the following hypothetical telephone books. Here's some mathematical food for thought. phone book number of N 2 number of people (N) seconds needed to sort

16 Estimating time required to sort Let's assume that your computer could make 1 billion (1,000,000,000) comparisons per second. That's a lot of comparisons in a second. And let's say your computer was using selection sort to sort the names of the people in the following hypothetical telephone books. Here's some mathematical food for thought. phone book number of N 2 number of people (N) seconds needed to sort Vancouver 544, ,284,262, or 5 minutes

17 Estimating time required to sort Let's assume that your computer could make 1 billion (1,000,000,000) comparisons per second. That's a lot of comparisons in a second. And let's say your computer was using selection sort to sort the names of the people in the following hypothetical telephone books. Here's some mathematical food for thought. phone book number of N 2 number of people (N) seconds needed to sort Vancouver 544, ,284,262, or 5 minutes Canada 30,000, ,000,000,000, ,000 or 10.4 days

18 Estimating time required to sort Let's assume that your computer could make 1 billion (1,000,000,000) comparisons per second. That's a lot of comparisons in a second. And let's say your computer was using selection sort to sort the names of the people in the following hypothetical telephone books. Here's some mathematical food for thought. phone book number of N 2 number of people (N) seconds needed to sort Vancouver 544, ,284,262, or 5 minutes Canada 30,000, ,000,000,000, ,000 or 10.4 days People's Republic 1,000,000,000 1,000,000,000,000,000,000 1,000,000,000 or 31.7 years of China

19 Estimating time required to sort Let's assume that your computer could make 1 billion (1,000,000,000) comparisons per second. That's a lot of comparisons in a second. And let's say your computer was using selection sort to sort the names of the people in the following hypothetical telephone books. Here's some mathematical food for thought. phone book number of N 2 number of people (N) seconds needed to sort Vancouver 544, ,284,262, or 5 minutes Canada 30,000, ,000,000,000, ,000 or 10.4 days People's Republic 1,000,000,000 1,000,000,000,000,000,000 1,000,000,000 or 31.7 years of China World 6,000,000,000 36,000,000,000,000,000,000 36,000,000,000 or 1142 years

20 Favorite Colors n record everybody's favorite color n how can we do "averages" per row?