Algs4-2.1.18可視軌跡-選擇排序
2.1.18可視軌跡。修改你為上一題給出的解答,為插入排序和選擇排序生成和正文中類似的可視軌跡。提示:使用setYscale()函數是一個明智的選擇。附加題:添加必要的代碼,與正文中的圖片一樣用紅色和灰色強調不同角色的元素。
/******************************************************************************
* Compilation: javac SelectionBars.java
* Execution: java SelectionBars n
* Dependencies: StdDraw.java
*
* Selection sort n random real numbers between 0 and 1, visualizing
* the results by ploting bars with heights proportional to the values.
*
* % java SelectionBars 20
*
******************************************************************************/
public class SelectionBars {
public static void sort(double[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
int min = i;
for (int j = i+1; j < n; j++)
if (less(a[j], a[min])) min = j;
show(a, i, min);
exch(a, i, min);
}
}
private static void show(double[] a, int i, int min) {
StdDraw.setYscale(-a.length + i + 1, i);
StdDraw.setPenColor(StdDraw.LIGHT_GRAY);
for (int k = 0; k < i; k++)
StdDraw.line(k, 0, k, a[k]*0.6);
StdDraw.setPenColor(StdDraw.BLACK);
for (int k = i; k < a.length; k++)
StdDraw.line(k, 0, k, a[k]*0.6);
StdDraw.setPenColor(StdDraw.BOOK_RED);
StdDraw.line(min, 0, min, a[min]*0.6);
}
private static boolean less(double v, double w) {
return v < w;
}
private static void exch(double[] a, int i, int j) {
double t = a[i];
a[i] = a[j];
a[j] = t;
}
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
StdDraw.setCanvasSize(160, 640);
StdDraw.setXscale(-1, n+1);
StdDraw.setPenRadius(0.006);
double[] a = new double[n];
for (int i = 0; i < n; i++)
a[i] = StdRandom.uniform(0.0, 1.0);
sort(a);
}
}
引用:教材官網.
Algs4-2.1.18可視軌跡-選擇排序