Systemcurrent Time Millis long t 0 System current

  • Slides: 31
Download presentation
時間の計測 • Systemクラスのcurrent. Time. Millis()という スタティック・メソッドを用いることができる。 long t 0 = System. current. Time. Millis();

時間の計測 • Systemクラスのcurrent. Time. Millis()という スタティック・メソッドを用いることができる。 long t 0 = System. current. Time. Millis(); . . . long t 1 = System. current. Time. Millis(); System. out. println(t 1 -t 0);

演習 • Sortingにおいて、 long t 0 = System. current. Time. Millis(); bubblesort(a, 0, 19999);

演習 • Sortingにおいて、 long t 0 = System. current. Time. Millis(); bubblesort(a, 0, 19999); long t 1 = System. current. Time. Millis(); System. out. println(t 1 -t 0); のようにして、ソーティングにかかる時間を 計測してみよ。

挿入 3 5 6 9 15 7 10 11 8 12

挿入 3 5 6 9 15 7 10 11 8 12

挿入 3 5 6 9 15 7 10 11 8 3 12

挿入 3 5 6 9 15 7 10 11 8 3 12

挿入 3 5 6 9 15 3 10 11 8 7 12

挿入 3 5 6 9 15 3 10 11 8 7 12

挿入 3 3 6 9 15 5 10 11 8 7 12

挿入 3 3 6 9 15 5 10 11 8 7 12

deletemin 3 5 4 9 15 7 10 11 6 8

deletemin 3 5 4 9 15 7 10 11 6 8

deletemin 3 5 4 9 15 7 10 11 6 8

deletemin 3 5 4 9 15 7 10 11 6 8

deletemin 3 11 5 9 15 4 7 10 6 8

deletemin 3 11 5 9 15 4 7 10 6 8

deletemin 3 4 5 9 15 11 7 10 6 8

deletemin 3 4 5 9 15 11 7 10 6 8

deletemin 3 4 5 9 15 6 7 10 11 8

deletemin 3 4 5 9 15 6 7 10 11 8

decrease key 3 5 4 9 15 7 10 11 6 8

decrease key 3 5 4 9 15 7 10 11 6 8

decrease key 3 5 4 9 15 7 10 4 6 8

decrease key 3 5 4 9 15 7 10 4 6 8

decrease key 3 5 4 9 15 4 10 7 6 8

decrease key 3 5 4 9 15 4 10 7 6 8

decrease key 3 4 4 9 15 5 10 7 6 8

decrease key 3 4 4 9 15 5 10 7 6 8

配列による実装 3 0 5 4 1 2 (j-1)/2 (k-1)/2 i 9 7 6 8

配列による実装 3 0 5 4 1 2 (j-1)/2 (k-1)/2 i 9 7 6 8 3 4 5 6 2*i+1 j 2*i+2 k 15 10 11 7 8 9

static int deletemin() { int m = a[0]; n--; a[0] = a[n]; . .

static int deletemin() { int m = a[0]; n--; a[0] = a[n]; . . . return m; } public void main(String[] args) { a = new int[100]; n = 0; insert(3); insert(2); System. out. println(deletemin()); } }

最短経路への適用 • insertはindexも変更する必要があるので以下のようになる。 static void insert(int v) { a[n] = v; index[v] = n;

最短経路への適用 • insertはindexも変更する必要があるので以下のようになる。 static void insert(int v) { a[n] = v; index[v] = n; for (int j = n; j>0 && dist[a[j]]<dist[a[(j-1)/2]]; j = (j-1)/2) { int w = a[j]; a[j] = a[(j-1)/2]; a[(j-1)/2] = w; index[a[j]] = j; index[a[(j-1)/2] = (j-1)/2; } n++; }

二次元グラフィクス import java. awt. *; import javax. swing. *; class Graph. Panel extends JPanel

二次元グラフィクス import java. awt. *; import javax. swing. *; class Graph. Panel extends JPanel { int[][] edges; int[] x; int[] y; int[] prev; int start; int goal;

Graph. Panel(int[][] edges, int[] x, int[] y, int[] prev, int start, int goal) {

Graph. Panel(int[][] edges, int[] x, int[] y, int[] prev, int start, int goal) { this. edges = edges; this. x = x; this. y = y; this. prev = prev; this. start = start; this. goal = goal; set. Background(Color. white); set. Minimum. Size(new Dimension(900, 500)); set. Preferred. Size(new Dimension(900, 500)); }

public void paint. Component(Graphics g){ super. paint. Component(g); g. set. Color(Color. BLACK); for (int

public void paint. Component(Graphics g){ super. paint. Component(g); g. set. Color(Color. BLACK); for (int i = 0; i < edges. length; i++) for (int j = 0; j < edges[i]. length; j++) { g. draw. Line(x[i], y[i], x[edges[i][j]], y[edges[i][j]]); } if (prev != null) { g. set. Color(Color. RED); for (int i = goal; i != start; i = prev[i]) { g. draw. Line(x[i], y[i], x[prev[i]], y[prev[i]]); } } }

public static void main(String[] args) { N 01_07 L_13. init(); JFrame f = new

public static void main(String[] args) { N 01_07 L_13. init(); JFrame f = new JFrame("Draw Example"); Graph. Panel example = new Graph. Panel(N 01_07 L_13. edges, N 01_07 L_13. x, N 01_07 L_13. y, null, 0, 0); f. get. Content. Pane(). add(example, Border. Layout. CENTER); f. pack(); f. set. Visible(true); } }

演習 • Graph. Panel. javaおよびN 01_07 L_13. javaを コンパイルして、Graph. Panel. mainを実行 してみよ。

演習 • Graph. Panel. javaおよびN 01_07 L_13. javaを コンパイルして、Graph. Panel. mainを実行 してみよ。

public static void main(String[] args) { N 01_07 L_13. init(); edges = N 01_07

public static void main(String[] args) { N 01_07 L_13. init(); edges = N 01_07 L_13. edges; lengths = N 01_07 L_13. lengths; x = N 01_07 L_13. x; y = N 01_07 L_13. y; n = edges. length; dist = new int[n]; prev = new int[n]; shortest(100, 1500); JFrame f = new JFrame("Draw Example"); Graph. Panel example = new Graph. Panel(edges, x, y, prev, 100, 1500); f. get. Content. Pane(). add(example, Border. Layout. CENTER); f. pack(); f. set. Visible(true); }