Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS100J Lecture 16 Previous Lecture This Lecture Programming concepts

Similar presentations


Presentation on theme: "CS100J Lecture 16 Previous Lecture This Lecture Programming concepts"— Presentation transcript:

1 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

2 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

3 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) =N A +1 -1 +3 -4 -2 =N A -2 -1 -4 +3 +1 CS 100 Lecture 16

4 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

5 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. N A ? blue N A white red CS 100 Lecture 16

6 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: N A ? the loop invariant blue N A white red CS 100 Lecture 16

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

8 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 N B A ? W Q B N A red white ? blue Q blue W B N A white red CS 100 Lecture 16

9 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. N A ? N A red white ? blue blue N A white red CS 100 Lecture 16

10 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

11 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

12 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++; W Q B N A red white ? blue CS 100 Lecture 16

13 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

14 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

15 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 N B A ? W Q B N N A red white ? blue CS 100 Lecture 16

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

17 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


Download ppt "CS100J Lecture 16 Previous Lecture This Lecture Programming concepts"

Similar presentations


Ads by Google