CS100J Lecture 16 Previous Lecture This Lecture Programming concepts

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Lecture: Algorithmic complexity
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Quicksort. Quicksort I To sort a[left...right] : 1. if left < right: 1.1. Partition a[left...right] such that: all a[left...p-1] are less than a[p], and.
CS 100Lecture 41 CS100J Lecture 4 n Previous Lecture –Programming Concepts n iteration n programming patterns (templates) –Java Constructs n while-statements.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Data Structures and Algorithms Lecture 5 and 6 Instructor: Quratulain Date: 15 th and 18 th September, 2009 Faculty of Computer Science, IBA.
CS 100Lecture 51 CS100J Lecture 5 n Previous Lecture –Programming Concepts n Rules of thumb –learn and use patterns –inspiration from hand-working problem.
CSC 211 Data Structures Lecture 13
CS 100Lecture 171 CS100J Lecture 17 n Previous Lecture –Programming concepts n Binary search n Application of the “rules of thumb” n Asymptotic complexity.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
0 Introduction to asymptotic complexity Search algorithms You are responsible for: Weiss, chapter 5, as follows: 5.1 What is algorithmic analysis? 5.2.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
Arrays Chapter 7.
CS1010 Programming Methodology
Searching.
Applied Discrete Mathematics Week 2: Functions and Sequences
Integer Programming An integer linear program (ILP) is defined exactly as a linear program except that values of variables in a feasible solution have.
RECURSION.
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
CS100J Lecture 19 Previous Lecture This Lecture
COP 3503 FALL 2012 Shayan Javed Lecture 15
Introduction To Repetition The for loop
Recitation 13 Searching and Sorting.
GC211Data Structure Lecture2 Sara Alhajjam.
CSE 331 Software Design and Implementation
Courtsey & Copyright: DESIGN AND ANALYSIS OF ALGORITHMS Courtsey & Copyright:
CS 583 Fall 2006 Analysis of Algorithms
Enough Mathematical Appetizers!
CS 3343: Analysis of Algorithms
Computation.
Quicksort 1.
Big-Oh and Execution Time: A Review
Developing Loops from Invariants
Algorithm design and Analysis
CSE 331 Software Design and Implementation
CS 201 Fundamental Structures of Computer Science
Analysys & Complexity of Algorithms
Applied Discrete Mathematics Week 6: Computation
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
Parameter Passing in Java
CS100J Lecture 18 Previous Lecture Programming concepts This Lecture
Programming and Data Structure
Sub-Quadratic Sorting Algorithms
Searching, Sorting, and Asymptotic Complexity
CS100J Lecture 3 Previous Lecture This Lecture Programming Concepts
Copyright © Cengage Learning. All rights reserved.
Asymptotic complexity Searching/sorting
Quicksort.
For loops Taken from notes by Dr. Neil Moore
Output Variables {true} S {i = j} i := j; or j := i;
Ch. 2: Getting Started.
Searching.
Binary Search and Loop invariants
CS100J Lecture 14 Previous Lecture
CS November 2010 Developing array algorithms. Reading:
CS100J Lecture 15 Previous Lecture This Lecture Sorting
At the end of this session, learner will be able to:
Quicksort.
CS100J Lecture 18 Previous Lecture Programming concepts This Lecture
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
Inequalities Some problems in algebra lead to inequalities instead of equations. An inequality looks just like an equation, except that in the place of.
Searching and Sorting Hint at Asymptotic Complexity
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
The Selection Problem.
Discrete Mathematics CS 2610
Searching and Sorting Hint at Asymptotic Complexity
Searching and Sorting Hint at Asymptotic Complexity
Quicksort.
CS100A Lect. 10, 1 Oct Input/Output & Program Schema
Presentation transcript:

CS100J Lecture 16 Previous Lecture This Lecture Programming concepts Binary search Application of the “rules of thumb” Asymptotic complexity Java Constructs Conditional Expressions This Lecture Alternative rules of thumb useful when you don’t know an algorithm Discovering an non-obvious algorithm by inventing a suitable loop invariant CS 100 Lecture 16

Three-Way Partitioning of an Array Problem. Characterize the values in an array as “red”, “white”, or “blue”, and sort the array into reds, then whites, then blues. Rule of Thumb. Write a precise specification. /* Given array A[0..N] consisting of “red” “white” and “blue” values in an arbitrary order, permute the values so that all reds precede all white, which precede all blues. */ static void sort(int[] A, int N) { . . . } CS 100 Lecture 16

Alternative Approach 0 1 2 3 4 5=N A +1 -1 +3 -4 -2 0 1 2 3 4 5=N A -2 Disregard the following rules of thumb: Find inspiration from experience. Work sample data by hand. Be introspective. Ask yourself: “What am I doing?” Instead, use the following rules of thumb: Do not seek inspiration from experience. Write down an example to clarify the problem requirements, but ignore how you get the answer. Let negative numbers be “red”, 0 be “white”, and positive numbers be “blue”. Sample input: Sample output (the order within a “color” is arbitrary) 0 1 2 3 4 5=N A +1 -1 +3 -4 -2 0 1 2 3 4 5=N A -2 -1 -4 +3 +1 CS 100 Lecture 16

Loop Pattern Rule of Thumb. If you smell an iteration, write it down. Disregard the following rule: Decide between for (definite) and while (indefinite) Instead, just use the while. /* Given array A[0..N] consisting of “red” “white” and “blue” values in an arbitrary order, permute the values so that all reds precede all white, which precede all blues. */ static void sort(int[] A, int N) { . . . while ( _______________________) } CS 100 Lecture 16

Characterize Initial and Final States Disregard the following rule of thumb. Characterize the state after an arbitrary number of iterations, either in English or in a diagram. Instead: Characterize the initial and final states. Initial: Final: and the final A is a permutation of the original A. 0 N A ? blue 0 N A white red CS 100 Lecture 16

the loop invariant Loop Invariant ? blue white red A A Find a possible “loop invariant”, a characterization of the state after an indeterminate number of iterations. The loop invariant should be a generalization of the characterizations of the initial and final states, i.e., they should be special cases of the loop invariant. Initial: Intermediate Final: 0 N A ? the loop invariant blue 0 N A white red CS 100 Lecture 16

Four Possible Loop Invariants Pick one: 0 N A ? red white blue 0 N A red ? white blue 0 N A red white ? blue 0 N A red white blue ? CS 100 Lecture 16

Identify boundaries with variables Introduce a variable to record the subscript of each boundary expected to change independently during the iteration. Indicate the position of each variable in each of the three characterizations. W Q 0 N B A ? 0 W Q B N A red white ? blue Q blue 0 W B N A white red CS 100 Lecture 16

Identify boundaries with variables Introduce a variable to record the subscript of each boundary expected to change independently during the iteration. Indicate the position of each variable in each of the three characterizations. 0 N A ? 0 N A red white ? blue blue 0 N A white red CS 100 Lecture 16

Initialization and Termination Rule of Thumb Use the characterizations to refine the initialization and termination condition of the loop. /* Given array A[0..N] consisting of “red” “white” and “blue” values in an arbitrary order, permute the values so that all reds precede all white, which precede all blues. */ static void sort(int[] A, int N) { W = 0; Q = 0; B = N + 1; while ( Q != B ) . . . } CS 100 Lecture 16

Specify the Body Rule of Thumb. Use the characterization to specify what the loop body must accomplish. /* Given array A[0..N] consisting of “red” “white” and “blue” values in an arbitrary order, permute the values so that all reds precede all white, which precede all blues. */ static void sort(int[] A, int N) { W = 0; Q = 0; B = N + 1; while ( Q != B ) // Reduce the size of the ? region // while maintaining the invariant. . . . } CS 100 Lecture 16

Refine the Body red white ? blue A // Reduce the size of the ? region // while maintaining the invariant. Case analysis on A[Q] A[Q] is white: Q++; A[Q] is blue: swap A[Q] with A[B-1]; B--; A[Q] is red: swap A[Q] with A[W]; W++; 0 W Q B N A red white ? blue CS 100 Lecture 16

Code the body /* Given array A[0..N] consisting of “red” “white” and “blue” values in an arbitrary order, permute the values so that all reds precede all white, which precede all blues. */ static void sort(int[] A, int N) { W = 0; Q = 0; B = N + 1; while ( Q != B ) // Reduce the size of the ? Region // while maintaining the invariant. if ( A[Q] is red ) { // swap A[Q] with A[W]. int temp = A[Q]; A[Q] = A[W]; A[W] = temp ; W++; Q++; } else if ( A[Q] is white ) else // A[Q] is blue. { // swap A[Q] with A[B-1]. A[Q] = A[B-1]; A[B-1] = temp ; B--; CS 100 Lecture 16

Final Program /* Given array A consisting of integers in an arbitrary order, permute the values so that all negatives precede all zeros, which precede all positives. */ static void sort(int[] A) { int N = A.length – 1; W = 0; Q = 0; B = N + 1; while ( Q != B ) // Reduce the size of the ? Region // while maintaining the invariant. if ( A[Q] < 0 ) { // swap A[Q] with A[W]. int temp = A[Q]; A[Q] = A[W]; A[W] = temp ; W++; Q++; } else if ( A[Q] == 0 ) else // A[Q] is positive. { // swap A[Q] with A[B-1]. A[Q] = A[B-1]; A[B-1] = temp ; B--; CS 100 Lecture 16

Check Boundary Conditions What happens on the first and last iterations? A[Q] is white: Q++; A[Q] is blue: swap A[Q] with A[B-1]; B--; A[Q] is red: swap A[Q] with A[W]; W++; W Q 0 N B A ? 0 W Q B N 0 N A red white ? blue CS 100 Lecture 16

Termination The loop will terminate is there is an integer notion of “distance”, i.e., a formula, that is: necessarily non-negative, and guaranteed to be reduced by at least 1 on each iteration. Q. What is that notion of distance? A. The size of the ? region, i.e., max(0, B-Q). Check. In each of the three cases, this is reduced by 1. CS 100 Lecture 16

Asymptotic Complexity Because the size of the ? region is reduced by 1 on each iteration, the number of iterations is N+1. Thus, because the time spent on each iteration is bounded by a constant number of steps, the total running time is linear in N. In contrast, the total running time of the general sorting algorithm from Lecture 14 is quadratic in N. CS 100 Lecture 16