-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPartitionExperiments.java
More file actions
162 lines (139 loc) · 4.04 KB
/
Copy pathPartitionExperiments.java
File metadata and controls
162 lines (139 loc) · 4.04 KB
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
155
156
157
158
159
160
161
162
import java.io.PrintWriter;
import java.util.Arrays;
/**
* Fun with partitioning and Quicksort.
*/
public class PartitionExperiments {
// +-----------+---------------------------------------------------
// | Constants |
// +-----------+
/**
* The smallest array size.
*/
static final int SMALL = 1000000;
/**
* The largest array size.
*/
static final int LARGE = 20000000;
/**
* The number of tests.
*/
static final int TESTS = 5;
// +---------+-----------------------------------------------------
// | Globals |
// +---------+
/**
* The total number of reads.
*/
static int totalReads;
/**
* The total number of writes.
*/
static int totalWrites;
/**
* The number of reads in the current call.
*/
static int currentReads;
/**
* The number of writes in the current call.
*/
static int currentWrites;
// +-----------+---------------------------------------------------
// | Quicksort |
// +-----------+
/**
* Sort an array using a partition algorithm.
*/
public static void quicksort(IntArray iarr, PartitionAlgorithm alg) {
quicksort(iarr, alg, 0, iarr.length());
} // quicksort
/**
* Sort the subarray at positions [lb..ub) using a partition
* algorithm.
*/
public static void quicksort(IntArray iarr, PartitionAlgorithm alg, int lb, int ub) {
// A subarray of zero or one elements is sorted.
if (ub - lb <= 1) {
return;
}
// Divide and conquer
int pivotLoc = alg.partition(iarr, lb, ub);
quicksort(iarr, alg, lb, pivotLoc);
quicksort(iarr, alg, pivotLoc+1, ub);
}
// +-------------+-------------------------------------------------
// | Experiments |
// +-------------+
/**
* Run partition on a full array.
*/
public static void partitionExperiment(PrintWriter pen, int size) {
int[] vals = new int[size];
for (int i = 0; i < size; i++) {
vals[i] = i;
} // for
IntArray iarr = new IntArray(vals);
iarr.permute();
LomutoPartition.ALGORITHM.partition(iarr.clone(), 0, size);
} // partitionExperiment(PrintWriter, int)
/**
* Run Quicksort on an array of a given size.
*/
public static void qsExperiment(PrintWriter pen, int size) {
// Build an array of the appropriate size.
int[] vals = new int[size];
for (int i = 0; i < size; i++) {
vals[i] = i;
}
IntArray iarr = new IntArray(vals);
iarr.permute();
for (int i = 0; i < 5; i++) {
qsExperiment(pen, iarr.clone(), HoarePartition.ALGORITHM);
}
for (int i = 0; i < 5; i++) {
qsExperiment(pen, iarr.clone(), HoarePartitionAlt.ALGORITHM);
}
for (int i = 0; i < 5; i++) {
qsExperiment(pen, iarr.clone(), LomutoPartition.ALGORITHM);
}
} // qsExperiment(PrintWriter, size)
/**
* Run Quicksort on a particular array using a given partition
* algorithm.
*/
public static void qsExperiment(PrintWriter pen, IntArray iarr, PartitionAlgorithm alg) {
// Run the algorithm
long startTime = System.currentTimeMillis();
quicksort(iarr, alg);
long elapsed = System.currentTimeMillis() - startTime;
// Gather some data
int gets = iarr.gets();
int sets = iarr.sets();
int len = iarr.length();
// Check the result
String result = "OK";
for (int i = 0; i < len; i++) {
if (iarr.get(i) != i) {
result = "FAILED";
break;
} //if
} // for
// Print some message
pen.printf("%s\tQuicksort\t%d\t%d\t%d\t%d\t%s\n",
alg.name(), iarr.length(), gets, sets, elapsed, result);
} // qsExperiment(PrintWriter, IntArray, PartitionAlgorithm
// +------+--------------------------------------------------------
// | Main |
// +------+
/**
* Build some arrays, sort them, display statistics.
*/
public static void main(String[] args) {
PrintWriter pen = new PrintWriter(System.out, true);
pen.println("Algorithm\tContext\tSize\tGets\tSets\n");
for (int i = 0; i < 5; i++) {
qsExperiment(pen, 10000000);
}
pen.close();
} // main
} // class PartitionExperiments