Announcements for This Lecture

Similar documents
Lecture 26. Sequence Algorithms (Continued)

CS 1110: Introduction to Computing Using Python Sequence Algorithms

Lecture 25. Designing Sequence Algorithms

CS1110. Lecture 20: Sequence algorithms. Announcements

Efficient algorithms for for clone items detection

Introduction to Derivatives

REVIEW LAB ANSWER KEY

Numerical Differentiation

Combining functions: algebraic methods

1. (a) 3. (a) 4 3 (b) (a) t = 5: 9. (a) = 11. (a) The equation of the line through P = (2, 3) and Q = (8, 11) is y 3 = 8 6

WYSE Academic Challenge 2004 Sectional Mathematics Solution Set

Lab 6 Derivatives and Mutant Bacteria

Preface. Here are a couple of warnings to my students who may be here to get a copy of what happened on a day that you missed.

Central Algorithmic Techniques. Iterative Algorithms

Searching. Sorting. Lambdas

ASYMPTOTIC COMPLEXITY SEARCHING/SORTING

lecture 26: Richardson extrapolation

Quicksort. Recurrence analysis Quicksort introduction. November 17, 2017 Hassan Khosravi / Geoffrey Tien 1

Printed Name: Section #: Instructor:

SECTION 3.2: DERIVATIVE FUNCTIONS and DIFFERENTIABILITY

MATH 1020 TEST 2 VERSION A FALL 2014 ANSWER KEY. Printed Name: Section #: Instructor:

Printed Name: Section #: Instructor:

2.8 The Derivative as a Function

MAT244 - Ordinary Di erential Equations - Summer 2016 Assignment 2 Due: July 20, 2016

Chapter 4: Numerical Methods for Common Mathematical Problems

AVL trees. AVL trees

Blueprint Algebra I Test

The derivative function

MATH 1020 Answer Key TEST 2 VERSION B Fall Printed Name: Section #: Instructor:

Investigating Euler s Method and Differential Equations to Approximate π. Lindsay Crowl August 2, 2001

Polynomials 3: Powers of x 0 + h

Lecture 15. Interpolation II. 2 Piecewise polynomial interpolation Hermite splines

Time (hours) Morphine sulfate (mg)

Average Rate of Change

Section 2.7 Derivatives and Rates of Change Part II Section 2.8 The Derivative as a Function. at the point a, to be. = at time t = a is

LIMITATIONS OF EULER S METHOD FOR NUMERICAL INTEGRATION

1 The concept of limits (p.217 p.229, p.242 p.249, p.255 p.256) 1.1 Limits Consider the function determined by the formula 3. x since at this point

Review for Exam IV MATH 1113 sections 51 & 52 Fall 2018

Decision Procedures. Jochen Hoenicke. Software Engineering Albert-Ludwigs-University Freiburg. Winter Term 2016/17

CSCE 478/878 Lecture 2: Concept Learning and the General-to-Specific Ordering

Bob Brown Math 251 Calculus 1 Chapter 3, Section 1 Completed 1 CCBC Dundalk

Math 31A Discussion Notes Week 4 October 20 and October 22, 2015

MAT 145. Type of Calculator Used TI-89 Titanium 100 points Score 100 possible points

. Compute the following limits.

Loop Invariants and Binary Search. Chapter 4.4, 5.1

Fundamentals of Concept Learning

Recap: Prefix Sums. Given A: set of n integers Find B: prefix sums 1 / 86

1. AB Calculus Introduction

Teaching Differentiation: A Rare Case for the Problem of the Slope of the Tangent Line

5.1 We will begin this section with the definition of a rational expression. We

HOW TO DEAL WITH FFT SAMPLING INFLUENCES ON ADEV CALCULATIONS

1. State whether the function is an exponential growth or exponential decay, and describe its end behaviour using limits.

A Reconsideration of Matter Waves

CSCE 222 Discrete Structures for Computing

Lecture 14: Nov. 11 & 13

Simpson s 1/3 Rule Simpson s 1/3 rule assumes 3 equispaced data/interpolation/integration points

NUMERICAL DIFFERENTIATION. James T. Smith San Francisco State University. In calculus classes, you compute derivatives algebraically: for example,

Phy 231 Sp 02 Homework #6 Page 1 of 4

Sin, Cos and All That

5. (a) Find the slope of the tangent line to the parabola y = x + 2x

Section 2.1 The Definition of the Derivative. We are interested in finding the slope of the tangent line at a specific point.

Sorting. Chapter 11. CSE 2011 Prof. J. Elder Last Updated: :11 AM

Numerical Analysis MTH603. dy dt = = (0) , y n+1. We obtain yn. Therefore. and. Copyright Virtual University of Pakistan 1

Fractional Derivatives as Binomial Limits

Calculus I, Fall Solutions to Review Problems II

The Krewe of Caesar Problem. David Gurney. Southeastern Louisiana University. SLU 10541, 500 Western Avenue. Hammond, LA

These errors are made from replacing an infinite process by finite one.

Solutions. Prelim 2[Solutions]

Outline. 1 Introduction. 3 Quicksort. 4 Analysis. 5 References. Idea. 1 Choose an element x and reorder the array as follows:

Math 102 TEST CHAPTERS 3 & 4 Solutions & Comments Fall 2006

2.3 Product and Quotient Rules

PHYSICS 1050 Mid-term Test 1 University of Wyoming 15 February 2005

Honors Calculus Midterm Review Packet

Solving Continuous Linear Least-Squares Problems by Iterated Projection

Notes on Neural Networks

Prelim 2[Solutions] Solutions. 1. Short Answer [18 pts]

Chapter 2. Limits and Continuity 16( ) 16( 9) = = 001. Section 2.1 Rates of Change and Limits (pp ) Quick Review 2.1

CS 4407 Algorithms Lecture 3: Iterative and Divide and Conquer Algorithms

7.1 Using Antiderivatives to find Area

Chapter 1. Density Estimation

Hoare Logic (I): Axiomatic Semantics and Program Correctness

3.4 Worksheet: Proof of the Chain Rule NAME

CSE 21 Practice Exam for Midterm 2 Fall 2017

Exercises for numerical differentiation. Øyvind Ryan

Introduction to Machine Learning. Recitation 8. w 2, b 2. w 1, b 1. z 0 z 1. The function we want to minimize is the loss over all examples: f =

Continuity and Differentiability Worksheet

General Physics I. New Lecture 27: Carnot Cycle, The 2nd Law, Entropy and Information. Prof. WAN, Xin

Mathematics 5 Worksheet 11 Geometry, Tangency, and the Derivative

INTRODUCTION TO CALCULUS LIMITS

Announcements. Exam 4 - Review of important concepts

Definition of the Derivative

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms

MVT and Rolle s Theorem

Notes on wavefunctions II: momentum wavefunctions

Physics 207 Lecture 23

3.1 Extreme Values of a Function

160 Chapter 3: Differentiation

Finding and Using Derivative The shortcuts

Name: Answer Key No calculators. Show your work! 1. (21 points) All answers should either be,, a (finite) real number, or DNE ( does not exist ).

3.4 Algebraic Limits. Ex 1) lim. Ex 2)

Transcription:

Lecture 26 Sorting

Announcements for Tis Lecture Prelim/Finals Prelims in andbac room Gates Hall 216 Open business ours Get tem any day tis wee Final: Survey Dec still 17 up t for 2:00-4:30pm A5 Study All new guide questions! by end of wee Eac person must answer Conflict wit Final time A6 due Monday, Dec. 9 Submit to Final Conflict assignment 2.5 wees including on CMS T-Day 2 wees witout te brea Must be in by December 10t Assignments/Lab A6 will be graded by Turs. Will give grade breadown Will review survey too A7 is due next Wednesday One wee left Keep up wit deadlines Lab 13 is optional Good study for te final Consultant ours still open 12/2/14 Sorting 2

Linear Searc Vague: Find first occurrence of v in b[..-1]. 12/2/14 Sorting 3

Linear Searc Vague: Find first occurrence of v in b[..-1]. Better: Store an integer in i to trutify result condition post: post: 1. v is not in b[..i-1] 2. i = OR v = b[i] 12/2/14 Sorting 4

Linear Searc Vague: Find first occurrence of v in b[..-1]. Better: Store an integer in i to trutify result condition post: post: 1. v is not in b[..i-1] 2. i = OR v = b[i] pre: b post: b i v not ere v 12/2/14 Sorting 5

Linear Searc Vague: Find first occurrence of v in b[..-1]. Better: Store an integer in i to trutify result condition post: post: 1. v is not in b[..i-1] 2. i = OR v = b[i] pre: b post: b i v not ere v OR b v not ere i 12/2/14 Sorting 6

Linear Searc pre: b post: b i v not ere v OR i b v not ere inv: b i v not ere 12/2/14 Sorting 7

Linear Searc def linear_searc(b,c,): """Returns: first occurrence of c in b[..]""" # Store in i te index of te first c in b[..] i = # invariant: c is not in b[0..i-1] wile i < len(b) and b[i]!= c: i = i + 1 # post: c is not in b[..i-1] # i >= len(b) or b[i] == c return i if i < len(b) else -1 Analyzing te Loop 1. Does te initialization mae inv true 2. Is post true wen inv is true and condition is false 3. Does te repetend mae progress 4. Does te repetend eep te invariant inv true 12/2/14 Sorting 8

Binary Searc Vague: Loo for v in sorted sequence segment b[..]. 12/2/14 Sorting 9

Binary Searc Vague: Loo for v in sorted sequence segment b[..]. Better: Precondition: b[..-1] is sorted (in ascending order). Postcondition: b[..i] < v and v <= b[i+1..-1] Below, te array is in non-descending order: pre: b post: b i < v >= v 12/2/14 Sorting 10

Binary Searc Loo for value v in sorted segment b[..] pre: b post: b inv: b < v i < v >= v i j >= v New statement of te invariant guarantees tat we get leftmost position of v if found Example b 0 1 2 3 4 5 6 7 8 9 3 3 3 3 3 4 4 6 7 7 if v is 3, set i to 0 if v is 4, set i to 5 if v is 5, set i to 7 if v is 8, set i to 10 12/2/14 Sorting 11

Binary Searc Vague: Loo for v in sorted sequence segment b[..]. Better: Precondition: b[..-1] is sorted (in ascending order). Postcondition: b[..i] <= v and v < b[i+1..-1] Below, te array is in non-descending order: pre: b post: b inv: b < v i >= v i j < v > v Called binary searc because eac iteration of te loop cuts te array segment still to be processed in alf 12/2/14 Sorting 12

Binary Searc pre: b post: b i < v >= v i j New statement of te invariant guarantees tat we get leftmost position of v if found inv: b < v >= v i = ; j = +1; wile i!= j: Looing at b[i] gives linear searc from left. Looing at b[j-1] gives linear searc from rigt. Looing at middle: b[(i+j)/2] gives binary searc. 12/2/14 Sorting 13

Sorting: Arranging in Ascending Order 0 n pre: b post: b 0 n sorted Insertion Sort: 0 i n inv: b sorted i = 0 wile i < n: # Pus b[i] down into its # sorted position in b[0..i] i = i+1 0 i 2 4 4 6 6 7 5 0 i 2 4 4 5 6 6 7 12/2/14 Sorting 14

Insertion Sort: Moving into Position i = 0 wile i < n: pus_down(b,i) i = i+1 def pus_down(b, i):" j = i wile j > 0: if b[j-1] > b[j]: swap(b,j-1,j) j = j-1 swap sown in te lecture about lists 0 i 2 4 4 6 6 7 5 0 i 2 4 4 6 6 5 7 0 i 2 4 4 6 5 6 7 0 i 2 4 4 5 6 6 7 12/2/14 Sorting 15

Te Importance of Helper Functions i = 0 wile i < n: pus_down(b,i) i = i+1 def pus_down(b, i):" j = i wile j > 0: if b[j-1] > b[j]: swap(b,j-1,j) j = j-1 VS i = 0 wile i < n: j = i wile j > 0: Can you understand all tis code below if b[j-1] > b[j]: temp = b[j] b[j] = b[j-1] b[j-1] = temp j = j -1 i = i +1 12/2/14 Sorting 16

Insertion Sort: Performance def pus_down(b, i): """Pus value at position i into sorted position in b[0..i-1]""" j = i wile j > 0: if b[j-1] > b[j]: swap(b,j-1,j) j = j-1 Insertion sort is an n 2 algoritm b[0..i-1]: i elements Worst case: i = 0: 0 swaps i = 1: 1 swap i = 2: 2 swaps Pusdown is in a loop Called for i in 0..n i swaps eac time Total Swaps: 0 + 1 + 2 + 3 + (n-1) = (n-1)*n/2 12/2/14 Sorting 17

Algoritm Complexity Given: a list of lengt n and a problem to solve Complexity: roug number of steps to solve worst case Suppose we can compute 1000 operations a second: Complexity n=10 n=100 n=1000 n 0.01 s 0.1 s 1 s n log n 0.016 s 0.32 s 4.79 s n 2 0.1 s 10 s 16.7 m n 3 1 s 16.7 m 11.6 d 2 n 1 s 4x10 19 y 3x10 290 y Major Topic in 2110: Beyond scope of tis course 12/2/14 Sorting 18

Sorting: Canging te Invariant 0 n pre: b post: b 0 n sorted Insertion Selection Sort: 0 i n inv: b sorted, sorted b[i..] b[0..i-1] i = 0 wile i < n: # Find minimum in b[i..] # Move it to position i i = i+1 First segment always contains smaller values i 2 4 4 6 6 8 9 9 7 8 9 i 2 4 4 6 6 7 9 9 8 8 9 2 4 4 6 6 7 9 9 8 8 9 i n n n 12/2/14 Sorting 19

Sorting: Canging te Invariant 0 n pre: b post: b 0 n sorted Insertion Selection Sort: 0 i n inv: b sorted, sorted b[i..] b[0..i-1] i = 0 wile i < n: j = index of min of b[i..n-1] swap(b,i,j) i = i+1 First segment always contains smaller values 2 4 4 6 6 8 9 9 7 8 9 12/2/14 Sorting 20 i i 2 4 4 6 6 7 9 9 8 8 9 Selection sort also is an n 2 algoritm n n

Partition Algoritm Given a list segment b[..] wit some value x in b[]: pre: b x Swap elements of b[..] and store in j to trutify post: i i+1 post: b <= x x >= x cange: b 3 5 4 1 6 2 3 8 1 i into b 1 2 1 3 5 4 6 3 8 or b i 1 2 3 1 3 4 5 6 8 x is called te pivot value x is not a program variable denotes value initially in b[] 12/2/14 Sorting 21

Sorting wit Partitions Given a list segment b[..] wit some value x in b[]: pre: b x Swap elements of b[..] and store in j to trutify post: i i+1 post: b y <= y <= y x >= y x >= x Partition Recursively Recursive partitions = sorting Called QuicSort (wy) Popular, fast sorting tecnique 12/2/14 Sorting 22

QuicSort def quic_sort(b,, ): """Sort te array fragment b[..]""" if b[..] as fewer tan 2 elements: return j = partition(b,, ) # b[..j 1] <= b[j] <= b[j+1..] # Sort b[..j 1] and b[j+1..] quic_sort (b,, j 1) quic_sort (b, j+1, ) pre: b post: b Worst Case: array already sorted Or almost sorted n 2 in tat case Average Case: array is scrambled n log n in tat case Best sorting time! x i i+1 <= x x >= x 12/2/14 Sorting 23

Algoritm: Final Word About Algoritms Step-by-step way to do someting Not tied to specific language Implementation: An algoritm in a specific language Many times, not te ard part Higer Level Computer Science courses: We teac advanced algoritms (pictures) Implementation you learn on your own List Diagrams Demo Code 12/2/14 Sorting 24