70 likes | 254 Views
Data Structures and Algorithms I Day 4, 9/8/11. CMP 338. Programming Homework 3. Download algs4.jar, add it too CLASSPATH env var Measure time to do N x N x N Matrix multAdd For N = 1, 2, 4, 8, 16, … 256 Can you do N = 512 or 1024 on your computer?
E N D
Data Structures and Algorithms I Day 4, 9/8/11 CMP 338
Programming Homework 3 Download algs4.jar, add it too CLASSPATH env var Measure time to do N x N x N Matrix multAdd For N = 1, 2, 4, 8, 16, … 256 Can you do N = 512 or 1024 on your computer? Use StdDraw to make a lg x lg plot of your data Measure the slope of a line through your points (ignore point with time < 1 second) Extra credit: automate most of the above Create a Measurable interface with two methods static createInstance(int size) and timeInstance() write a driver that uses repeated doubling
Programming Homework 4 Implement the “good pivot” algorithm (day 1) Implement two median methods (using “good pivot” and “poor pivot”) Implement four select methods Pivot: poor-pivot, good-pivot, pp-median, gp-median Measure select performance on same random arrays N = 1, 2, 4, 8, 16 ...220, k = (int) sqrt(N) Using StdDraw, plot lg-lg and lg-normal Do the performance ratios approach constants What are they?
Programming Homework 5 Implement 4 quicksorts (with different pivots) Poor-pivot, good-pivot, poor-pivot-median, good-pivot-median Measure sort preformance on same random arrays N = 1, 2, 4, 8, 16 .. 220 Plot performances with StdDraw Do the performance ratios appear to approach constants? What are they?
Selection Algorithm Item select(double[] a, int k, int lo, int hi) if (k == 0 && lo == hi) return a[lo] Item p = pivot(a, lo, hi) // e.g. a[lo+(lo+hi)/2] int j =partition(a, p, lo, hi) if (k <= j) return select(a, k, lo, j) else return select(a, k+lo-j-1, j+1, hi)
Good Pivot double goodPivot(double[] a, int lo, int hi) double[] b = new double[N/5] for (int j=0; j<N/5; j++) b[j] = median5(a, j*5) double p = select(b, N/10, 0, (N/5)-1) return p double median5(double[a], int lo) for (int i=lo; i<lo+3; i++) for (int j=i+1; j<lo+5; j++) if (a[j] < a[i]) exch(a, i, j) return a[lo+2]
Poor Pivot double poorPivot(double[] a, int lo, int hi) return a[lo] double okayPivot(double[]a, int lo, int hi) double x=a[lo], y=a[(lo+hi)/2], z=a[hi] if (y < x) swap(x, y) if (z < x) swap(x, z) if (z < y) swap(y, z) a[lo]=x, a[(lo+hi)/2]=y, a[hi]=z return y