1 / 7

CMP 338

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?

zelig
Download Presentation

CMP 338

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. Data Structures and Algorithms I Day 4, 9/8/11 CMP 338

  2. 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

  3. 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?

  4. 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?

  5. 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)

  6. 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]

  7. 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

More Related