Title: CS100J Lecture 16
1CS100J 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 dont
know an algorithm - Discovering an non-obvious algorithm by inventing
a suitable loop invariant
2Three-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 A0..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)
-
- . . .
-
3Alternative 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)
4Loop 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 A0..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 ( _______________________)
-
- . . .
-
- . . .
5Characterize 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.
6Loop 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
7Four Possible Loop Invariants
0 N
?
red
white
blue
A
0 N
red
?
white
blue
A
0 N
red
white
?
blue
A
0 N
red
white
blue
?
A
8Identify 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
9Identify 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
10Initialization and Termination
- Rule of Thumb
- Use the characterizations to refine the
initialization and termination condition of the
loop. - / Given array A0..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 )
-
- . . .
-
- . . .
11Specify the Body
- Rule of Thumb.
- Use the characterization to specify what the loop
body must accomplish.
/ Given array A0..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. . . .
12Refine the Body
- // Reduce the size of the ? region
- // while maintaining the invariant.
- Case analysis on AQ
- AQ is white
- Q
- AQ is blue
- swap AQ with AB-1
- B--
- AQ is red
- swap AQ with AW
- W
- Q
0 W Q B N
A
red
white
?
blue
13Code the body
/ Given array A0..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 ( AQ is
red ) // swap AQ with AW.
int temp AQ AQ
AW AW temp
W Q
else if ( AQ is white ) Q
else // AQ is blue. // swap AQ
with AB-1. int temp AQ
AQ AB-1 AB-1
temp B--
14Final 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 ( AQ lt 0 )
// swap AQ with AW. int
temp AQ AQ AW
AW temp W
Q else if (
AQ 0 ) Q else // AQ
is positive. // swap AQ with
AB-1. int temp AQ
AQ AB-1 AB-1
temp B--
15Check Boundary Conditions
- What happens on the first and last iterations?
- AQ is white
- Q
- AQ is blue
- swap AQ with AB-1
- B--
- AQ is red
- swap AQ with AW
- W
- Q
W
Q
0 N
0 W Q B N
A
red
white
?
blue
16Termination
- 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.
17Asymptotic Complexity
- Because the size of the ? region is reduced by 1
on each iteration, the number of iterations is
N1. - 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.