1 / 17

CS100J Lecture 16

CS100J Lecture 16. Previous Lecture Programming concepts Binary search Application of the “rules of thumb” Asymptotic complexity Java Constructs Conditional Expressions This Lecture Programming concepts Alternative rules of thumb useful when you don’t know an algorithm

nealb
Download Presentation

CS100J Lecture 16

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS100J Lecture 16 • Previous Lecture • Programming concepts • Binary search • Application of the “rules of thumb” • Asymptotic complexity • Java Constructs • Conditional Expressions • This Lecture • Programming concepts • Alternative rules of thumb useful when you don’t know an algorithm • Discovering an non-obvious algorithm by inventing a suitable loop invariant 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. */ staticvoid sort(int[] A, int N) { . . . } Lecture 16

  3. 0 1 2 3 4 5=N A +1 -1 0 +3 -4 -2 0 1 2 3 4 5=N A -2 -1 -4 0 +3 +1 Alternative Approach • 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) 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. */ staticvoid sort(int[] A, int N) { . . . while ( _______________________) { . . . } . . . } Lecture 16

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

  6. 0 N ? A 0 N blue red white A Loop Invariant • 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: the loop invariant Lecture 16

  7. Four Possible Loop Invariants • Pick one: 0 N ? red white blue A 0 N red ? white blue A 0 N red white ? blue A 0 N red white blue ? A Lecture 16

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

  9. 0 N ? A 0 N blue red white A 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 red white ? blue 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. */ staticvoidsort(int[] A, int N) { W = 0; Q = 0; B = N + 1; while ( Q != B ) { . . . } . . . } 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. */ staticvoidsort(int[] A, int N) { W = 0; Q = 0; B = N + 1; while ( Q != B ) { // Reduce the size of the ? region // while maintaining the invariant. . . . } } Lecture 16

  12. Refine the Body // 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++; Q++; 0 W Q B N A red white ? blue 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. */ staticvoidsort(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++; } elseif ( A[Q] is white ) Q++; else // A[Q] is blue. { // swap A[Q] with A[B-1]. int temp = A[Q]; A[Q] = A[B-1]; A[B-1] = temp ; B--; } } } 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. */ staticvoidsort(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++; } elseif ( A[Q] == 0 ) Q++; else // A[Q] is positive. { // swap A[Q] with A[B-1]. int temp = A[Q]; A[Q] = A[B-1]; A[B-1] = temp ; B--; } } } Lecture 16

  15. 0 NB ? A 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++; Q++; W Q 0 N 0 W QB N A red white ? blue 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. 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. Lecture 16

More Related