30 Pages

CS1114-lec5

Course: CS 100, Fall 2008
School: Cornell
Rating:
 
 
 
 
 

Word Count: 1114

Document Preview

and Sorting selection Prof. Noah Snavely CS1114 http://cs1114.cs.cornell.edu Administrivia Assignment 1 due Friday by 5pm Please sign up for a demo slot using CMS If you don't yet have a 100R account, please let me know Assignment 2 out on Friday Quiz 2 next Thursday 2/12 Coverage through next Tuesday Closed book / closed note 2 Administrivia More robots will be working soon If all of the working...

Register Now

Unformatted Document Excerpt

Coursehero >> New York >> Cornell >> CS 100

Course Hero has millions of student submitted documents similar to the one
below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.

Course Hero has millions of student submitted documents similar to the one below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.
and Sorting selection Prof. Noah Snavely CS1114 http://cs1114.cs.cornell.edu Administrivia Assignment 1 due Friday by 5pm Please sign up for a demo slot using CMS If you don't yet have a 100R account, please let me know Assignment 2 out on Friday Quiz 2 next Thursday 2/12 Coverage through next Tuesday Closed book / closed note 2 Administrivia More robots will be working soon If all of the working ones are in use, you can test your code with the robot arena 3 Logical operators && logical "and" || logical "or" if (x == 1 && y == 2) || z == 3 % do something interesting else % do something else end 4 What's the difference between... A(i) = A(i) + 2i; A(i) = A(i) + 2 * i; 5 Plotting in Matlab 6 Recap from last time We looked at the "trimmed mean" problem for locating the lightstick Remove 5% of points on all sides, find centroid This is a version of a more general problem: Finding the kth largest element in an array Also called the "selection" problem We considered an algorithm that repeatedly removes the largest element How fast is this algorithm? 7 Recap from last time BigO notation allows us to reason about speed without worrying about Getting lucky on the input Depending on our hardware BigO of repeatedly removing the biggest element? Worstcase (k = n/2, i.e., median) is quadratic, O(n2) 8 Classes of algorithm speed Constant time algorithms, O(1) Do not depend on the input size Example: find the first element Linear time algorithms, O(n) Constant amount of work for every input item Example: find the largest element Quadratic time algorithms, O(n2) Linear amount of work for every input item Example: repeatedly removing max element 9 How to do selection better? If our input were sorted, we can do better Given 100 numbers in increasing order, we can easily figure out the 5th biggest or smallest Very important principle! (encapsulation) Divide your problem into pieces One person (or group) can provide sort The other person can use sort As long as both agree on what sort does, they can work independently Can even "upgrade" to a faster sort 10 How to sort? Sorting is an ancient problem, by the standards of CS First important "computer" sort used for 1890 census, by Hollerith (the 1880 census took 8 years, 1890 took just one) There are many sorting algorithms 11 How to sort? Given an array of numbers: [10 2 5 30 4 8 19 102 53 3] How can we produce a sorted array? [2 3 4 5 8 10 19 30 53 102] 12 How to sort? A concrete version of the problem Suppose I want to sort all actors by height ... How do I do this? 13 Sorting, 1st attempt Idea: Given n actors 1. Find the shortest actor (D. Devito), put him first 2. Find the shortest actor in the remaining group, put him/her second ... Repeat ... n. Find the shortest actor in the remaining group (one left), put him/her last 14 Sorting, 1st attempt Algorithm 1 1. Find the shortest actor put him first 2. Find the shortest actor in the remaining group, put him/her second ... Repeat ... n. Find the shortest actor in the remaining group put him/her last What does this remind you of? This is called selection sort After round k, the first k entries are sorted 15 Selection sort pseudocode function [ A ] = selection_sort(A) % Returns a sorted version of array A % by applying selection sort % Uses in place sorting n = length(A); for i = 1:n % Find the smallest element in A(i:n) % Swap that element with something (what?) end 16 Filling in the gaps % Find the smallest element in A(i:n) We pretty much know how to do this m = 10000; = m_index -1; for j in i:n if A(j) < m m = A(j); m_index = j; end [ 10 13 41 6 51 11 ] end % After round 1, % m = 6, m_index = 4 17 Filling in the gaps % Swap the smallest element with something % Swap element A(m_index) with A(i) A(i) = A(m_index); A(m_index) = A(i); tmp = A(i); A(i) = A(m_index); A(m_index) = tmp; [ 10 13 41 6 51 11 ] [ 6 13 41 10 51 11 ] 18 Putting it all together function [ A ] = selection_sort(A) % Returns a sorted version of array A len = length(A); for i = 1:len % Find the smallest element in A(i:len) m = 10000; m_index = -1; for j in i:n if A(j) < m m = A(j); m_index = j; end end % Swap element A(m_index) with A(i) tmp = A(i); A(i) = A(m_index); A(m_index) = tmp; end 19 Example of selection sort [ 10 13 41 6 51 11 ] [ 6 13 41 10 51 11 ] [ 6 10 41 13 51 11 ] [ 6 10 11 13 51 41 ] [ 6 10 11 13 51 41 ] [ 6 10 11 13 41 51 ] [ 6 10 11 13 41 51 ] 20 Speed of selection sort Let n be the size of the array How fast is selection sort? O(1) O(n) O(n2) ? How many comparisons (<) does it do? First iteration: n comparisons Second iteration: n 1 comparisons ... nth iteration: 1 comparison 21 Speed of selection sort Total number of comparisons: n + (n 1) + (n 2) + ... + 1 n(n + 1) i = 2 i =1 n Work grows in proportion to n2 selection sort is O(n2) 22 Is this the best we can do? Let's try a different approach Suppose we tell all the actors and all actors shorter than 5.5' to move to the left side of the room taller than 5.5' to move to the right side of the room (actors who are exactly 5.5' move to the middle) [ 6.0 [ 5.4 5.4 5.3 5.5 5.0 6.2 5.5 5.3 6.0 5.0 6.2 5.9 ] 5.9 ] 23 Sorting, 2nd attempt [ 6.0 [ 5.4 5.4 5.3 5.5 5.0 6.2 5.5 5.3 6.0 > 5.5' 5.0 6.2 5.9 ] 5.9 ] < 5.5' Not quite done, but it's a start We've put every element on the correct side of 5.5' (the pivot) What next? Do this again on each side: ask shorter group to pivot on (say) 5.3', taller group to pivot on 6.0' Divide and conquer 24 How do we select the pivot? How did we know to select 5.5' as the pivot? Answer: averageish human height In general, we might not know a good value Solution: just pick some va...

Find millions of documents on Course Hero - Study Guides, Lecture Notes, Reference Materials, Practice Exams and more. Course Hero has millions of course specific materials providing students with the best way to expand their education.

Below is a small sample set of documents:

Cornell - CS - 100
Sorting and selectionProf. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministriviaAssignment 1 due Friday by 5pm Please sign up for a demo slot using CMS If you don't yet have a 100R account, please let me knowAssignment 2 out on Friday
Cornell - CS - 100
Sorting and selection Part 2Prof. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministrivia Assignment 1 due tomorrow by 5pm Assignment 2 will be out tomorrow Two parts: smaller part due next Friday, larger part due in two weeks Qu
Cornell - CS - 100
Sorting and selection Part 2Prof. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministriviaAssignment 1 due tomorrow by 5pm Assignment 2 will be out tomorrow Two parts: smaller part due next Friday, larger part due in two weeksQuiz 2 next
Cornell - CS - 100
Blobs and GraphsProf. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministriviaAssignment 2 is out First part due on Friday by 5pm Second part due next Friday by 5pmQuiz 2 on Thursday Coverage through today (topics include running time,
Cornell - CS - 100
CS1114: Notes on big-O notation(a) An O(1) function(b) An O(n) functionFigure 1: With big-O notation, what matters is how the amount of work grows for &quot;large&quot; n In class we went over the running time of several different algorithms on different
Cornell - CS - 100
Connected components and graph traversalProf. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministriviaAssignment 2 is out First part due tomorrow by 5pm Second part due next Friday by 5pmFirst prelim will be in two weeks Thursday, Febru
Cornell - CS - 100
Breadthfirst and depthfirst traversalProf. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministrivia Assignment 2, Part 2, due Friday Assignment 3 will be out Friday Survey: Please sign up for a Friday slot Should we move lecture closer
Cornell - CS - 100
Breadth-first and depth-first traversalProf. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministriviaAssignment 2, Part 2, due Friday Please sign up for a Friday slotAssignment 3 will be out Friday Survey: Should we move lecture closer t
Cornell - CS - 100
Linked listsProf. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministrivia Assignment 2, Part 2 due tomorrow Please don't wait until the last minute to finish (the last two problems are challenging) Assignment 3 will be posted tomorrow
Cornell - CS - 100
Linked listsProf. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministriviaAssignment 2, Part 2 due tomorrow Please don't wait until the last minute to finish (the last two problems are challenging)Assignment 3 will be posted tomorrow Due
Cornell - CS - 100
Linked lists and memory allocationProf. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministrivia Assignment 3 has been posted Due next Friday, March 6 Prelim 1 Thursday in class Review session this evening at 7:15pm, Upson 315 Review
Cornell - CS - 100
Linked lists and memory allocationProf. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministriviaAssignment 3 has been posted Due next Friday, March 6Prelim 1 Thursday in class Review session this evening at 7:15pm, Upson 315 Review sess
Cornell - CS - 100
CS1114: Study Guide 1In the first part of this course, we have covered the topics in this document. Please refer to the class slides for more details.1Finding the center of things: Bounding boxes, centroids, trimmed means, and medians(x1, y1),
Cornell - CS - 100
PolygonsandtheconvexhullProf.NoahSnavely CS1114 http:/cs1114.cs.cornell.eduAdministrivium Assignment3duethisFridayby5pm PleasesignupforslotsonCMS Thelastproblemhasyoucontrollingtherobots withthecamera;youllneedtousethe robotGetFrame function
Cornell - CS - 100
Polygons and the convex hullProf. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministriviumAssignment 3 due this Friday by 5pm Please sign up for slots on CMS The last problem has you controlling the robots with the camera; you'll need to
Cornell - CS - 100
InterpolationProf. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministrivia Assignment 3 due tomorrow by 5pm Please sign up for a demo slot Assignment 4 will be posted tomorrow Quiz 3 next Thursday2Last time Convex hull the smalles
Cornell - CS - 100
InterpolationProf. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministriviaAssignment 3 due tomorrow by 5pm Please sign up for a demo slotAssignment 4 will be posted tomorrow Quiz 3 next Thursday2Last timeConvex hull the smallest co
Cornell - CS - 100
Image transformationsProf. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministrivia Assignment 4 will be out by tomorrow Due the Friday after spring break Quiz 3 next time Topics: convex hull, interpolation, image transformations2L
Cornell - CS - 100
Image transformationsProf. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministriviaAssignment 4 will be out by tomorrow Due the Friday after spring breakQuiz 3 next time Topics: convex hull, interpolation, image transformations2Last
Cornell - CS - 100
Imagetransformations,Part2Prof.NoahSnavely CS1114 http:/cs1114.cs.cornell.eduAdministrivia Assignment4hasbeenposted DuetheFridayafterspringbreak TAevaluations http:/www.engineering.cornell.edu/TAEval/survey.cfm Midtermcourseevaluations2
Cornell - CS - 100
Image transformations, Part 2Prof. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministriviaAssignment 4 has been posted Due the Friday after spring breakTA evaluations http:/www.engineering.cornell.edu/TAEval/survey.cfmMidterm course e
Cornell - CS - 100
Recognizing objectsProf. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministrivia Assignment 4 due on Friday The first problem is tricky please get started early! Quiz 4 next Tuesday, 3/31 Prelim 2 in two weeks, 4/7 (in class) Covers e
Cornell - CS - 100
Recognizing objectsProf. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministriviaAssignment 4 due on Friday The first problem is tricky please get started early!Quiz 4 next Tuesday, 3/31 Prelim 2 in two weeks, 4/7 (in class) Covers ever
Cornell - CS - 100
Featurebased object recognitionProf. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministrivia Assignment 4 due tomorrow, A5 will be out tomorrow, due in two parts Quiz 4 next Tuesday, 3/31 Prelim 2 in two weeks, 4/7 (in class) Covers eve
Cornell - CS - 100
AdministriviaFeature-based object recognitionAssignment 4 due tomorrow, A5 will be out tomorrow, due in two parts Quiz 4 next Tuesday, 3/31Prof. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduPrelim 2 in two weeks, 4/7 (in class) Covers everyth
Cornell - CS - 100
Computing transformationsProf. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministrivia A5 Part 1 due on Friday, A5 Part 2 out soon Prelim 2 next week, 4/7 (in class) Covers everything since Prelim 1 Review session next Monday (time TBA)
Cornell - CS - 100
Computing transformationsProf. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministriviaA5 Part 1 due on Friday, A5 Part 2 out soon Prelim 2 next week, 4/7 (in class) Covers everything since Prelim 1 Review session next Monday (time TBA)2
Cornell - CS - 100
Optimization and least squaresProf. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministrivia A5 Part 1 due tomorrow by 5pm (please sign up for a demo slot) Part 2 will be due in two weeks (4/17) Prelim 2 on Tuesday 4/7 (in class) Covers
Cornell - CS - 100
Optimization and least squaresProf. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministriviaA5 Part 1 due tomorrow by 5pm (please sign up for a demo slot) Part 2 will be due in two weeks (4/17) Prelim 2 on Tuesday 4/7 (in class) Covers ever
Cornell - CS - 100
CS1114: Study Guide 2This document covers the topics we've covered in the second part of the course. Please refer to the class slides for more details.1Polygons and convex hullsA polygon is a set of 2D points (called vertices, as in a graph) c
Cornell - CS - 100
Markov chainsProf. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduRoadmap for the next month Guest lecture 4/16, Prof. Charles Van Loan Ellipse fitting (this is a much better way to find lightstick shapes) Exams: Assignments: Prelim 3: 4
Cornell - CS - 100
Markov chainsProf. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduRoadmap for the next monthGuest lecture 4/16, Prof. Charles Van Loan Ellipse fitting (this is a much better way to find lightstick shapes)Exams: Prelim 3: 4/30 (Final lecture)
Cornell - CS - 100
Markov chains Part 2Prof. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministrivia Guest lecture on Thursday, Prof. Charles Van Loan Assignments: A5P2 due on Friday by 5pm A6 will be out on Friday Quiz next Thursday, 4/232Administ
Cornell - CS - 100
Markov chains Part 2Prof. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministriviaGuest lecture on Thursday, Prof. Charles Van Loan Assignments: A5P2 due on Friday by 5pm A6 will be out on FridayQuiz next Thursday, 4/232Administrivia
Cornell - CS - 100
Author recognitionProf. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministrivia Quiz 5 this Thursday, 4/23 Focus on Markov chains A6 released, due on Friday There will be demo sessions You will also turn in your code this time Preli
Cornell - CS - 100
Author recognitionProf. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministriviaQuiz 5 this Thursday, 4/23 Focus on Markov chainsA6 released, due on Friday There will be demo sessions You will also turn in your code this timePrelim 3
Cornell - CS - 100
Clustering and greedy algorithmsProf. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministrivia A6 due tomorrow Please sign up for demo sessions You will also turn in your code this time (turnin due Monday) Prelim 3 next Thursday, 4/30
Cornell - CS - 100
Clustering and greedy algorithmsProf. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministriviaA6 due tomorrow Please sign up for demo sessions You will also turn in your code this time (turnin due Monday)Prelim 3 next Thursday, 4/30 (las
Cornell - CS - 100
Clustering and greedy algorithms - Part 2Prof. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministrivia Prelim 3 on Thursday Will be comprehensive, but focused on Markov chains and clustering Review session Wednesday at 7pm, Upson 31
Cornell - CS - 100
Clustering and greedy algorithms - Part 2Prof. Noah Snavely CS1114 http:/cs1114.cs.cornell.eduAdministriviaPrelim 3 on Thursday Will be comprehensive, but focused on Markov chains and clustering Review session Wednesday at 7pm, Upson 315 Ste
Cornell - CS - 100
CS1114: Study Guide 3This document covers the topics we've covered in the final part of the course. Please refer to the class slides for more details.1Markov chainsSequences of things come up all the time when dealing with the real world. The
Cornell - CS - 100
CS1114 Section 2/4 Exercises: Robot arena, recursion, and while loops1Robot arena1. We've set up a virtual robot arena to test your code when all of the physical robots are in use. The first exercise is to start the robot arena, create a robot,
Cornell - CS - 100
CS1114: Assignment 2Assigned: Feb. 6, 2009 First Part Due: Feb. 13, 2007 by 5PM Second Part Due: Feb. 20, 2007 by 5PM1IntroductionIn the first assignment, you had the opportunity to work with a basic image operation (thresholding). Recall that
Cornell - CS - 100
CS1114 Assignment 31 Previously, on Assignment 2out: Feb 20, 2009 due: March 6, 2009 by 5pmIn the last assignment we implemented several robust ways of finding the lightstick center. In this assignment, we will do even better, by using graph tra
Cornell - CS - 100
CS1114 Assignment 41out: March 11th, 2009 due: 5:00PM March 27th, 2009Previously, on Assignment 3..you wrote functions to drive the robot based on the position of the lightstick in the webcam image. Now, wed also like the robot to respond to t
Cornell - CS - 100
CS1114 Assignment 5, Part 1out: Friday, March 27, 2009. due: Friday, April 3, 2009, 5PM.This assignment covers three topics in two parts: interpolation and image transformations (Part 1), and feature-based image recognition (Part 2). This documen
Cornell - CS - 100
CS1114 Assignment 5, Part 2out: Monday, April 6, 2009. due: Friday, April 17, 2009, 5PM.This assignment covers three topics in two parts: interpolation and image transformations (Part 1), and feature-based image recognition (Part 2). This documen
Cornell - CS - 100
CS1114 Assignment 6out: Saturday, April 18, 2009. due: Friday, April 25, 2009, 5PM.In this assignment, you will be implementing an authorship detector which, when given a large sample size of text to train on, can then guess the author of an unkn
Uni. Westminster - RRG - 1024
Prime Realty Author: Date: Purpose: Carol Malloy 6/1/2009 To calculate loan information assuming a constant yearly rateMortgage Analysis WorksheetSummary Information Loan $250,000 Rate 5.00% Years 30 Per Year 12 Total Payments 360 Monthly Payment
Uni. Westminster - RRG - 1024
Current Year Info Sales (in dollars) Variable Cost Fixed Costs Sales (Volume/Unit) Sales Price (each) Variable Cost (each) Forecast Estimates Sales Volume Increase Sales Price Increase Variable Cost Increase Fixed Cost Increase2008 Predicted Info 2
Uni. Westminster - RRG - 1024
Raquel Gilson TechBlog 3 February 6, 2007One thing I am embarrassed to admit is that I am not completely aware of the differences between the World Wide Web and the Internet. Before I come to class again on Thursday February 8th I intend to do more
Uni. Westminster - RRG - 1024
Raquel Gilson TechBlog Notes PageTechBlog 1 Jan. 25, 2007 Ebusiness System of gathering information. TechBlog 2 Jan. 30, 2007 Business Intelligence Systems Systems that disseminate information. MIS - One of the oldest intelligence systems out to
Carnegie Mellon - ANDREW - 213
15-213 Recitation 12Eugene Marinelli Section E 4/16/07Outline Announcements Malloc Course materialAnnouncements No quiz malloc due tomorrowLast day to turn in ThursdayProxy lab out sometime this week (I'm in charge of this one)
Iowa State - CPRE - 211
Assigned: 2/6/04Due: 2/17/04CprE 211 Spring 2004 Homework 2Last Name First Name Section_ __Grading Procedure for Homework: The number of points of each question is given in brackets. You shall turn in your homework in the class when it is
Iowa State - CPRE - 211
Assigned: 2/6/04Due: 2/17/04CprE 211 Spring 2004 Homework 2 SolutionLast Name First Name Section_ __Grading Procedure for Homework: The number of points of each question is given in brackets. You shall turn in your homework in the class wh
Iowa State - CPRE - 211
Assigned: 3/9/04Due: 3/25/04CprE 211 Spring 2004 Homework 4 SolutionLast Name First Name Section _ _ _Grading Procedure for Homework: The number of points of each question is given in brackets. You shall turn in your homework in the class whe
Iowa State - CPRE - 211
Assigned: 4/20/03Completed by: 4/27/03CprE 211 Spring 2004 Homework 6Last Name First Name Section_ __Grading Procedure for Homework: This homework will not be collected and not be graded. The solution will be distributed on-line one week l
University of the West Indies at Mona - GREEN - 4531
San Diego State - DESIGN - 240
Washington - FACULTY - 122
Physics 122 Solutions Ch 2727.1. Visualize:As discussed in Section 27.1, the symmetry of the electric field must match the symmetry of the charge distribution. In particular, the electric field of a cylindrically symmetric charge distribution cann
Washington - FACULTY - 122
Physics 122 Solutions to Chapter 2525.1.Model:Use the charge model.Solve:(a) In the process of charging by rubbing, electrons are removed from one material and transferred to the other because they are relatively free to move. Protons, on the other