CS100J Lecture 14 Previous Lecture Additional material on the use of arrays and alternative implementations of a class The use of auxiliary private methods searching This Lecture Sorting Loop invariants more Rules of Thumb Reading Lewis & Loftus Section 6.2 Savitch Section 6.4 CS 100 Lecture 14
Sorting Problem. Sort a given array that contains m+1 integers into non-decreasing order, i.e., rearrange the values in the array so they never decrease. Rule of Thumb. Write a precise specification /* Sort A[0..m] into non-decreasing order. */ public void sort(int[] A, int m) { . . . } Implementation Constraint: Sort array in place (in situ), i.e., do not use a second array. CS 100 Lecture 14
Inspiration from Example Rule of Thumb. Find inspiration from experience, e.g., sorting a hand in a card game. Rule of Thumb. Work sample data by hand. Be introspective. Ask yourself: “What am I doing?” 0 1 2 3 4 5 = m A CS 100 Lecture 14
Algorithm: Selection Sort 0 1 2 3 4 5 A 19 13 20 10 12 18 0 1 2 3 4 5 A 10 13 20 19 12 18 0 1 2 3 4 5 A 10 12 20 19 13 18 0 1 2 3 4 5 A 10 12 13 19 20 18 0 1 2 3 4 5 A 10 12 13 18 20 19 0 1 2 3 4 5 A 10 12 13 18 19 20 What am I doing? Swap smallest of A[0..m] with A[0]. Swap smallest of A[1..m] with A[1]. Swap smallest of A[2..m] with A[2]. . . . Swap smallest of A[m-1..m] with A[m-1]. CS 100 Lecture 14
Iterative Refinement Rule of Thumb. If you smell an iteration, write it down. Decide between definite iteration and indefinite iteration Write down an appropriate pattern for the iteration. Do not fill in the pattern yet. Example: /* Sort A[0..m] into non-decreasing order. */ public void sort(int[] A, int m) { . . . for ( ________; __________; _________) } CS 100 Lecture 14
Loop Invariant Rules of Thumb 0 k m A ? ? where Characterize the state after an arbitrary number of iterations, either in English of in a diagram. Introduce a variable to record the subscript of each boundary expected to change independently during the iteration. 0 k m A finished ? ? where finished means that the array elements contain the correct final values, ? means that the array elements may or may not contain the correct final values, k contains the subscript of the first element in the ? section, and A[0..m] is a rearrangement of the original values A[0..m] CS 100 Lecture 14
Loop Invariant, cont. Rules of Thumb, continued 0=k m A 0 k m A ? Characterize the initial and final states, i.e., the state before the iteration begins and after the iteration is expected to stop. The characterization of the initial and final states should be special cases of the general characterization. Initial: Intermediate: Final: 0=k m A ? 0 k m A finished ? 0 m=k A finished CS 100 Lecture 14
Loop Invariant, cont. Rule of Thumb. Use the characterization to specify what the loop body must accomplish. Use the characterization to refine the initialization, termination condition, and increment of the loop. /* Sort A[0..m] into non-decreasing order. */ public void sort(int[] A, int m) { . . . for ( int k = ______; k < _____; k++ ) /* Given that A[0..k-1] is finished, finish A[0..k]. */ } CS 100 Lecture 14
Refine the Loop Body /* Sort A[0..m] into non-decreasing order. */ public void sort(int[] A, int m) { . . . for ( int k = 0; k < m; k++ ) /* Given that A[0..k-1] is finished, finish A[0..k]. */ int minLoc; // subscript of // smallest in A[k..m] /* Set minLoc so A[minLoc] is smallest in A[k..m]. */ /* Exchange A[k] and A[minLoc] */ } CS 100 Lecture 14
Refine One Subpart /* Sort A[0..m] into non-decreasing order. */ public void sort(int[] A, int m) { . . . for ( int k = 0; k < m; k++ ) /* Given that A[0..k-1] is finished, finish A[0..k]. */ int minLoc; // subscript of // smallest in A[k..m] /* Set minLoc so A[minLoc] is smallest in A[k..m]. */ /* Exchange A[k] and A[minLoc] */ } CS 100 Lecture 14
Refine Other Subpart /* Sort A[0..m] into non-decreasing order. */ public void sort(int[] A, int m) { . . . for ( int k = 0; k < m; k++ ) /* Given that A[0..k-1] is finished, finish A[0..k]. */ int minLoc; // subscript of // smallest in A[k..m] /* Set minLoc so A[minLoc] is smallest in A[k..m]. */ for ( _______; _______; ______) } /* Exchange A[k] and A[minLoc] */ CS 100 Lecture 14
Loop Invariant (for Inner Loop) Initial: Intermediate: Final: where A[minLoc] is smallest in A[k..i-1] All values in > are greater than A[minLoc] No value in >= is smaller than A[minLoc] All values in ? are unknown. minLoc 0 k i m A ? minLoc 0 k i m A ? > >= minLoc 0 k m i A >= > CS 100 Lecture 14
Final Function /* Sort A[0..m] into non-decreasing order. */ public void sort(int[] A, int m) { for ( int k = 0; k < m; k++ ) /* Given that A[0..k-1] is finished, finish A[0..k]. */ int minLoc; // subscript of // smallest in A[k..m] /* Set minLoc so A[minLoc] is smallest in A[k..m]. */ minLoc = k; for (int i=k+1; i <= m; i++) if (A[i] < A[minLoc]) minLoc = i; /* Exchange A[k] and A[minLoc] */ int temp = A[k]; A[k] = A[minLoc]; A[minLoc] = temp; } CS 100 Lecture 14
Better Final Function /* Sort A into non-decreasing order. */ public void sort(int[] A) { int m = A.length - 1; for ( int k = 0; k < m; k++ ) /* Given that A[0..k-1] is finished, finish A[0..k]. */ int minLoc; // subscript of // smallest in A[k..m] /* Set minLoc so A[minLoc] is smallest in A[k..m]. */ minLoc = k; for (int i=k+1; i <= m; i++) if (A[i] < A[minLoc]) minLoc = i; /* Exchange A[k] and A[minLoc] */ int temp = A[k]; A[k] = A[minLoc]; A[minLoc] = temp; } CS 100 Lecture 14
Rearrange Related Data at Same Time /* Given arrays A and B of equal length, sort A into non-decreasing order and rearrange B similarly. */ public void sort(int[] A, int[] B) { int m = A.length - 1; for ( int k = 0; k < m; k++ ) /* Given that A[0..k-1] is finished, finish A[0..k]; rearrange B similarly. */ int minLoc; // subscript of // smallest in A[k..m] /* Set minLoc so A[minLoc] is smallest in A[k..m]. */ minLoc = k; for (int i=k+1; i <= m; i++) if (A[i] < A[minLoc]) minLoc = i; /* Exchange A[k] and A[minLoc] */ int temp = A[k]; A[k] = A[minLoc]; A[minLoc] = temp; } /* Exchange B[k] and B[minLoc] */ int temp = B[k]; B[k] = B[minLoc]; B[minLoc] = temp; CS 100 Lecture 14