Debian Codegraph @

src/dr/util/HeapSort.java

raw symbols SCIP
1 /*
2 * HeapSort.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.util;
27
28 import java.util.AbstractList;
29 import java.util.Comparator;
30 import java.util.Random;
31 import java.util.Vector;
32
33 /**
34 * sorts numbers and comparable objects by treating contents of array as a binary tree.
35 * KNOWN BUGS: There is a horrible amount of code duplication here!
36 *
37 * @author Alexei Drummond
38 * @author Korbinian Strimmer
39 * @version $Id: HeapSort.java,v 1.7 2006/02/20 17:36:23 rambaut Exp $
40 */
41 public class HeapSort {
42
43 //
44 // Public stuff
45 //
46
47 /**
48 * Sorts an array of indices to vector of comparable objects
49 * into increasing order.
50 */
51 public static void sort(AbstractList array, int[] indices) {
52
53 // ensures we are starting with valid indices
54 for (int i = 0; i < indices.length; i++) {
55 indices[i] = i;
56 }
57
58 int temp;
59 int j, n = array.size();
60
61 // turn input array into a heap
62 for (j = n / 2; j > 0; j--) {
63 adjust(array, indices, j, n);
64 }
65
66 // remove largest elements and put them at the end
67 // of the unsorted region until you are finished
68 for (j = n - 1; j > 0; j--) {
69 temp = indices[0];
70 indices[0] = indices[j];
71 indices[j] = temp;
72 adjust(array, indices, 1, j);
73 }
74 }
75
76 /**
77 * Sorts a vector of comparable objects into increasing order.
78 */
79 public static void sort(AbstractList array) {
80
81 Object temp;
82 int j, n = array.size();
83
84 // turn input array into a heap
85 for (j = n / 2; j > 0; j--) {
86 adjust(array, j, n);
87 }
88
89 // remove largest elements and put them at the end
90 // of the unsorted region until you are finished
91 for (j = n - 1; j > 0; j--) {
92 temp = array.get(0);
93 array.set(0, array.get(j));
94 array.set(j, temp);
95 adjust(array, 1, j);
96 }
97 }
98
99 /**
100 * Sorts an array of comparable objects into increasing order.
101 */
102 public static void sort(Comparable[] array) {
103
104 Comparable temp;
105 int j, n = array.length;
106
107 // turn input array into a heap
108 for (j = n / 2; j > 0; j--) {
109 adjust(array, j, n);
110 }
111
112 // remove largest elements and put them at the end
113 // of the unsorted region until you are finished
114 for (j = n - 1; j > 0; j--) {
115 temp = array[0];
116 array[0] = array[j];
117 array[j] = temp;
118 adjust(array, 1, j);
119 }
120 }
121
122 /**
123 * Sorts an array of objects into increasing order given a comparator.
124 */
125 public static void sort(Object[] array, Comparator c) {
126
127 Object temp;
128 int j, n = array.length;
129
130 // turn input array into a heap
131 for (j = n / 2; j > 0; j--) {
132 adjust(array, c, j, n);
133 }
134
135 // remove largest elements and put them at the end
136 // of the unsorted region until you are finished
137 for (j = n - 1; j > 0; j--) {
138 temp = array[0];
139 array[0] = array[j];
140 array[j] = temp;
141 adjust(array, c, 1, j);
142 }
143 }
144
145 /**
146 * Sorts an array of doubles into increasing order.
147 */
148 public static void sort(double[] array) {
149
150 double temp;
151 int j, n = array.length;
152
153 // turn input array into a heap
154 for (j = n / 2; j > 0; j--) {
155 adjust(array, j, n);
156 }
157
158 // remove largest elements and put them at the end
159 // of the unsorted region until you are finished
160 for (j = n - 1; j > 0; j--) {
161 temp = array[0];
162 array[0] = array[j];
163 array[j] = temp;
164 adjust(array, 1, j);
165 }
166 }
167
168 /**
169 * Sorts an array of doubles into increasing order, ingoring sign.
170 */
171 public static void sortAbs(double[] array) {
172
173 double temp;
174 int j, n = array.length;
175
176 // turn input array into a heap
177 for (j = n / 2; j > 0; j--) {
178 adjustAbs(array, j, n);
179 }
180
181 // remove largest elements and put them at the end
182 // of the unsorted region until you are finished
183 for (j = n - 1; j > 0; j--) {
184 temp = array[0];
185 array[0] = array[j];
186 array[j] = temp;
187 adjustAbs(array, 1, j);
188 }
189 }
190
191 /**
192 * Sorts an array of indices into an array of doubles
193 * into increasing order.
194 */
195 public static void sort(double[] array, int[] indices) {
196
197 // ensures we are starting with valid indices
198 for (int i = 0; i < indices.length; i++) {
199 indices[i] = i;
200 }
201
202 int temp;
203 int j, n = indices.length;
204
205 // turn input array into a heap
206 for (j = n / 2; j > 0; j--) {
207 adjust(array, indices, j, n);
208 }
209
210 // remove largest elements and put them at the end
211 // of the unsorted region until you are finished
212 for (j = n - 1; j > 0; j--) {
213 temp = indices[0];
214 indices[0] = indices[j];
215 indices[j] = temp;
216 adjust(array, indices, 1, j);
217 }
218 }
219
220
221 /**
222 * test harness for heapsort algorithm
223 */
224 public static void main(String[] args) {
225
226 int testSize = 100;
227
228 // test array of Comparable objects
229
230 ComparableDouble[] test = new ComparableDouble[testSize];
231
232 Random random = new Random();
233
234 for (int i = 0; i < test.length; i++) {
235 test[i] = new ComparableDouble(random.nextInt(testSize * 10));
236 }
237
238 sort(test);
239 for(ComparableDouble aTest : test) {
240 System.out.print(aTest + " ");
241 }
242 System.out.println();
243
244 // test index to Vector of Comparable objects
245
246 Vector testv = new Vector();
247 int[] indices = new int[testSize];
248
249 for (int i = 0; i < testSize; i++) {
250 testv.addElement(new ComparableDouble(random.nextInt(testSize * 10)));
251 }
252
253 sort(testv, indices);
254 for (int i = 0; i < test.length; i++) {
255 System.out.print(testv.elementAt(indices[i]) + " ");
256 }
257 System.out.println();
258
259
260 // test index to array of doubles
261
262 double[] testd = new double[testSize];
263 //int[] indices = new int[testSize];
264
265 for (int i = 0; i < testSize; i++) {
266 testd[i] = random.nextInt(testSize * 10);
267 }
268
269 sort(testd, indices);
270 for (int i = 0; i < test.length; i++) {
271 System.out.print(testd[indices[i]] + " ");
272 }
273 System.out.println();
274
275 }
276
277 // PRIVATE STUFF
278
279 /**
280 * helps sort an array of indices into a vector of comparable objects.
281 * Assumes that array[lower+1] through to array[upper] is
282 * already in heap form and then puts array[lower] to
283 * array[upper] in heap form.
284 */
285 private static void adjust(AbstractList<Comparable> array, int[] indices, int lower, int upper) {
286
287 int j, k;
288 int temp;
289
290 j = lower;
291 k = lower * 2;
292
293 while (k <= upper) {
294 if ((k < upper) && (array.get(indices[k - 1]).compareTo(array.get(indices[k])) < 0)) {
295 k += 1;
296 }
297 if (array.get(indices[j - 1]).compareTo(array.get(indices[k - 1])) < 0) {
298 temp = indices[j - 1];
299 indices[j - 1] = indices[k - 1];
300 indices[k - 1] = temp;
301 }
302 j = k;
303 k *= 2;
304 }
305 }
306
307 /**
308 * helps sort an vector of comparable objects.
309 * Assumes that array[lower+1] through to array[upper] is
310 * already in heap form and then puts array[lower] to
311 * array[upper] in heap form.
312 */
313 private static void adjust(AbstractList array, int lower, int upper) {
314
315 int j, k;
316 Object temp;
317
318 j = lower;
319 k = lower * 2;
320
321 while (k <= upper) {
322 if ((k < upper) && (((Comparable) array.get(k - 1)).compareTo(array.get(k)) < 0)) {
323 k += 1;
324 }
325 if (((Comparable) array.get(j - 1)).compareTo(array.get(k - 1)) < 0) {
326 temp = array.get(j - 1);
327 array.set(j - 1, array.get(k - 1));
328 array.set(k - 1, temp);
329 }
330 j = k;
331 k *= 2;
332 }
333 }
334
335 /**
336 * Assumes that array[lower+1] through to array[upper] is
337 * already in heap form and then puts array[lower] to
338 * array[upper] in heap form.
339 */
340 private static void adjust(Comparable[] array, int lower, int upper) {
341
342 int j, k;
343 Comparable temp;
344
345 j = lower;
346 k = lower * 2;
347
348 while (k <= upper) {
349 if ((k < upper) && (array[k - 1].compareTo(array[k]) < 0)) {
350 k += 1;
351 }
352 if (array[j - 1].compareTo(array[k - 1]) < 0) {
353 temp = array[j - 1];
354 array[j - 1] = array[k - 1];
355 array[k - 1] = temp;
356 }
357 j = k;
358 k *= 2;
359 }
360 }
361
362 /**
363 * Assumes that array[lower+1] through to array[upper] is
364 * already in heap form and then puts array[lower] to
365 * array[upper] in heap form.
366 */
367 private static void adjust(Object[] array, Comparator c, int lower, int upper) {
368
369 int j, k;
370 Object temp;
371
372 j = lower;
373 k = lower * 2;
374
375 while (k <= upper) {
376 if ((k < upper) && (c.compare(array[k - 1], array[k]) < 0)) {
377 k += 1;
378 }
379 if (c.compare(array[j - 1], array[k - 1]) < 0) {
380 temp = array[j - 1];
381 array[j - 1] = array[k - 1];
382 array[k - 1] = temp;
383 }
384 j = k;
385 k *= 2;
386 }
387 }
388
389 /**
390 * helps sort an array of doubles.
391 * Assumes that array[lower+1] through to array[upper] is
392 * already in heap form and then puts array[lower] to
393 * array[upper] in heap form.
394 */
395 private static void adjust(double[] array, int lower, int upper) {
396
397 int j, k;
398 double temp;
399
400 j = lower;
401 k = lower * 2;
402
403 while (k <= upper) {
404 if ((k < upper) && (array[k - 1] < array[k])) {
405 k += 1;
406 }
407 if (array[j - 1] < array[k - 1]) {
408 temp = array[j - 1];
409 array[j - 1] = array[k - 1];
410 array[k - 1] = temp;
411 }
412 j = k;
413 k *= 2;
414 }
415 }
416
417 /**
418 * helps sort an array of doubles.
419 * Assumes that array[lower+1] through to array[upper] is
420 * already in heap form and then puts array[lower] to
421 * array[upper] in heap form.
422 */
423 private static void adjustAbs(double[] array, int lower, int upper) {
424
425 int j, k;
426 double temp;
427
428 j = lower;
429 k = lower * 2;
430
431 while (k <= upper) {
432 if ((k < upper) && (Math.abs(array[k - 1]) < Math.abs(array[k]))) {
433 k += 1;
434 }
435 if (Math.abs(array[j - 1]) < Math.abs(array[k - 1])) {
436 temp = array[j - 1];
437 array[j - 1] = array[k - 1];
438 array[k - 1] = temp;
439 }
440 j = k;
441 k *= 2;
442 }
443 }
444
445 /**
446 * helps sort an array of indices into an array of doubles.
447 * Assumes that array[lower+1] through to array[upper] is
448 * already in heap form and then puts array[lower] to
449 * array[upper] in heap form.
450 */
451 private static void adjust(double[] array, int[] indices, int lower, int upper) {
452
453 int j, k;
454 int temp;
455
456 j = lower;
457 k = lower * 2;
458
459 while (k <= upper) {
460 if ((k < upper) && (array[indices[k - 1]] < array[indices[k]])) {
461 k += 1;
462 }
463 if (array[indices[j - 1]] < array[indices[k - 1]]) {
464 temp = indices[j - 1];
465 indices[j - 1] = indices[k - 1];
466 indices[k - 1] = temp;
467 }
468 j = k;
469 k *= 2;
470 }
471 }
472 }
473