-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickBars.java
154 lines (128 loc) · 4.51 KB
/
QuickBars.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/******************************************************************************
* Compilation: javac QuickBars.java
* Execution: java QuickBars m n
* Dependencies: StdDraw.java
*
* Sort n random real numbers between 0 and 1 using quicksort with
* cutoff to insertion sort and median-of-3 partitioning.
*
* Visualize the results by ploting bars with heights proportional
* to the values.
*
* % java QuickBars 1000 75
*
* Comments
* --------
* - if image is too large, it may not display properly but you can
* still save it to a file
*
******************************************************************************/
public class QuickBars {
private static int rows;
private static int row = 0;
private static final int VERTICAL = 70;
private static final int CUTOFF = 8;
// partition the subarray a[lo .. hi] by returning an index j
// so that a[lo .. j-1] <= a[j] <= a[j+1 .. hi]
private static int partition(double[] a, int lo, int hi) {
int i = lo;
int j = hi + 1;
double v = a[lo];
while (true) {
// find item on lo to swap
while (less(a[++i], v))
if (i == hi) break;
// find item on hi to swap
while (less(v, a[--j]))
if (j == lo) break; // redundant since a[lo] acts as sentinel
// check if pointers cross
if (i >= j) break;
exch(a, i, j);
}
// put v = a[j] into position
exch(a, lo, j);
// with a[lo .. j-1] <= a[j] <= a[j+1 .. hi]
return j;
}
public static void sort(double[] a) {
// StdRandom.shuffle(a);
show(a, 0, 0, -1, a.length-1);
sort(a, 0, a.length - 1);
show(a, 0, 0, -1, a.length-1);
}
// quicksort the subarray from a[lo] to a[hi]
private static void sort(double[] a, int lo, int hi) {
// cutoff to insertion sort
int n = hi - lo + 1;
if (n <= CUTOFF) {
insertionSort(a, lo, hi);
// show(a, lo, -1, -1, hi);
return;
}
int m = median3(a, lo, lo + n/2, hi);
exch(a, m, lo);
int j = partition(a, lo, hi);
show(a, lo, j, j, hi);
sort(a, lo, j-1);
sort(a, j+1, hi);
}
// sort from a[lo] to a[hi] using insertion sort
private static void insertionSort(double[] a, int lo, int hi) {
for (int i = lo; i <= hi; i++)
for (int j = i; j > lo && less(a[j], a[j-1]); j--)
exch(a, j, j-1);
}
// return the index of the median element among a[i], a[j], and a[k]
private static int median3(double[] a, int i, int j, int k) {
return (less(a[i], a[j]) ?
(less(a[j], a[k]) ? j : less(a[i], a[k]) ? k : i) :
(less(a[k], a[j]) ? j : less(a[k], a[i]) ? k : i));
}
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;
}
// draw one row of trace
private static void show(double[] a, int lo, int lt, int gt, int hi) {
double y = rows - row - 1;
for (int k = 0; k < a.length; k++) {
if (k < lo) StdDraw.setPenColor(StdDraw.LIGHT_GRAY);
else if (k > hi) StdDraw.setPenColor(StdDraw.LIGHT_GRAY);
else if (k >= lt && k <= gt) StdDraw.setPenColor(StdDraw.BOOK_RED);
else StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.filledRectangle(k, y + a[k] * 0.25, 0.25, a[k] * 0.25);
}
row++;
}
public static void main(String[] args) {
int m = Integer.parseInt(args[0]);
int n = Integer.parseInt(args[1]);
double[] a = new double[n];
double[] b = new double[n];
for (int i = 0; i < n; i++) {
a[i] = (1 + StdRandom.uniform(m)) / (double) m;
b[i] = a[i];
}
StdDraw.enableDoubleBuffering();
// precompute the number of rows
rows = 0;
sort(b);
rows = row;
row = 0;
StdDraw.clear();
StdDraw.setCanvasSize(800, rows*VERTICAL);
StdDraw.show();
StdDraw.square(0.5, 0.5, 0.5);
StdDraw.setXscale(-1, n);
StdDraw.setYscale(-0.5, rows);
StdDraw.show();
sort(a);
StdDraw.show();
}
}
Copyright © 2002–2016, Robert Sedgewick and Kevin Wayne.
Last updated: Tue Aug 30 10:09:18 EDT 2016.