public static void main(String[] args) { int N = Integer.parseInt(args[0]); StdDraw.setCanvasSize(160, 640); StdDraw.setXscale(-1, N + 1); StdDraw.setPenRadius(.006); double[] a = new double[N]; for (int i = 0; i < N; i++) a[i] = Math.random(); sort(a); }
private static void show(double[] a, int i, int j) { StdDraw.setYscale(-a.length + i + 1, i); StdDraw.setPenColor(StdDraw.LIGHT_GRAY); for (int k = 0; k < j; k++) StdDraw.line(k, 0, k, a[k] * .6); StdDraw.setPenColor(StdDraw.BOOK_RED); StdDraw.line(j, 0, j, a[j] * .6); StdDraw.setPenColor(StdDraw.BLACK); for (int k = j + 1; k < i; k++) StdDraw.line(k, 0, k, a[k] * .6); StdDraw.setPenColor(StdDraw.LIGHT_GRAY); for (int k = i + 1; k < a.length; k++) StdDraw.line(k, 0, k, a[k] * .6); }
// display header private static void header(String[] a) { int N = a.length; StdDraw.setPenColor(StdDraw.BLACK); StdDraw.text(N / 2.0, -3, "a[ ]"); for (int i = 0; i < N; i++) StdDraw.text(i, -2, i + ""); StdDraw.text(-2.50, -2, "i"); StdDraw.text(-1.25, -2, "min"); StdDraw.setPenColor(StdDraw.BOOK_RED); StdDraw.line(-3, -1.65, N - .5, -1.65); StdDraw.setPenColor(StdDraw.BLACK); for (int i = 0; i < a.length; i++) StdDraw.text(i, -1, a[i]); }
// test client public static void main(String[] args) { // parse command-line argument as an array of 1-character strings String s = "EASYQUESTION"; int N = s.length(); String[] a = new String[N]; for (int i = 0; i < N; i++) a[i] = s.substring(i, i + 1); // set canvas size StdDraw.setCanvasSize(30 * (N + 3), 30 * (N + 3)); StdDraw.setXscale(-3, N + 1); StdDraw.setYscale(N + 1, -3); StdDraw.setFont(new Font("SansSerif", Font.PLAIN, 13)); // draw the header header(a); // sort the array sort(a); // draw the footer footer(a); }
// exchange a[i] and a[j] private static void draw(String[] a, int row, int ith, int min) { StdDraw.setPenColor(StdDraw.BLACK); StdDraw.text(-2.50, row, ith + ""); StdDraw.text(-1.25, row, min + ""); for (int i = 0; i < a.length; i++) { if (i == min) StdDraw.setPenColor(StdDraw.BOOK_RED); else if (i < ith) StdDraw.setPenColor(StdDraw.LIGHT_GRAY); else StdDraw.setPenColor(StdDraw.BLACK); StdDraw.text(i, row, a[i] + ""); } }
// display footer private static void footer(String[] a) { int N = a.length; StdDraw.setPenColor(StdDraw.BLACK); for (int i = 0; i < a.length; i++) StdDraw.text(i, N, a[i]); }
// draw line between this point and that point to standard drawing public void drawTo(Point that) { /* DO NOT MODIFY */ StdDraw.line(this.x, this.y, that.x, that.y); }
// plot this point to standard drawing public void draw() { /* DO NOT MODIFY */ StdDraw.point(x, y); }