Download presentation
Presentation is loading. Please wait.
1
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
2
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
3
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?” = m A CS 100 Lecture 14
4
Algorithm: Selection Sort
A 19 13 20 10 12 18 A 10 13 20 19 12 18 A 10 12 20 19 13 18 A 10 12 13 19 20 18 A 10 12 13 18 20 19 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
5
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
6
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. 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
7
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 ? k m A finished ? m=k A finished CS 100 Lecture 14
8
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
9
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
10
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
11
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
12
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 k i m A ? minLoc k i m A ? > >= minLoc k m i A >= > CS 100 Lecture 14
13
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
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
15
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
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.