2
* This program is free software; you can redistribute it and/or modify
3
* it under the terms of the GNU General Public License as published by
4
* the Free Software Foundation; either version 2 of the License, or
5
* (at your option) any later version.
7
* This program is distributed in the hope that it will be useful,
8
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
* GNU General Public License for more details.
12
* You should have received a copy of the GNU General Public License
13
* along with this program; if not, write to the Free Software
14
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
* MultiDimensionalSort.java
19
* Copyright (C) 2004 Stijn Lievens
23
package weka.classifiers.misc.monotone;
25
import java.util.Arrays;
26
import java.util.Comparator;
29
* Class for doing multidimensional sorting, using an array of
30
* <code> Comparator. </code>
31
* The goal is to sort an array topologically. If <code> o1 </code>
32
* and <code> o2 </code> are two objects of the array <code> a, </code>
33
* and for all valid indices <code> i </code> in the array <code> c </code>
34
* if holds that <code> c[i].compare(o1,o2) < 0 </code> then
35
* <code> o1 </code> comes before <code> o2 </code> in the sorted array.
37
* A typical is the sorting of vectors in an n-dimensional space,
38
* where the ordering is determined by the product ordering.
41
* This implementation is part of the master's thesis: "Studie
42
* en implementatie van instantie-gebaseerde algoritmen voor gesuperviseerd
43
* rangschikken", Stijn Lievens, Ghent University, 2004.
46
* @author Stijn Lievens (stijn.lievens@ugent.be)
47
* @version $Revision: 1.1 $
49
public class MultiDimensionalSort {
52
* Sort an array using different comparators.
54
* @param a the array to be sorted. The sorted array is returned
55
* in the array <code> a </code> itself.
56
* @param c an array holding the different comparators
58
public static void multiDimensionalSort (Object[] a, Comparator[] c) {
59
multiDimensionalSort(a, 0, a.length, c);
63
* Sort part of an array using different comparators.
65
* @param a the array to be sorted, the indicated part of the array will
66
* be replaced by the sorted elements
67
* @param fromIndex index of the first element to be sorted (inclusive)
68
* @param toIndex index of the last element to be sorted (exclusive)
69
* @param c array holding the different comparators
70
* @throws IllegalArgumentException if <code> fromIndex > toIndex </code>
72
public static void multiDimensionalSort(Object[] a, int fromIndex,
73
int toIndex, Comparator[] c)
74
throws IllegalArgumentException {
76
if (fromIndex > toIndex) {
77
throw new IllegalArgumentException
78
("Illegal range: fromIndex can be at most toIndex");
80
multiDimensionalSort(a, fromIndex, toIndex, c, 0);
84
* Do the actual sorting in a recursive way.
86
* @param a the array to be sorted, the indicated part of the array will
87
* be replaced by the sorted elements
88
* @param fromIndex index of the first element to be sorted (inclusive)
89
* @param toIndex index of the last element to be sorted (exclusive)
90
* @param c array holding the different comparators
91
* @param depth the index of the comparator to use
93
private static void multiDimensionalSort(Object[] a, int fromIndex,
94
int toIndex, Comparator[] c, int depth) {
96
if (depth == c.length) {
97
return; // maximum depth reached
100
Comparator comp = c[depth]; // comparator to use
101
Arrays.sort(a, fromIndex, toIndex, comp); // sort part of the array
103
// look for breakpoints in the array and sort recursively
104
int mark = fromIndex;
105
for (int i = fromIndex + 1; i < toIndex; i++) {
106
if (comp.compare(a[i - 1], a[i]) != 0) {
107
// a[i] is different from a[i-1], breakpoint detected
108
multiDimensionalSort(a, mark, i, c, depth + 1);
112
// sort the last part
113
multiDimensionalSort(a, mark, toIndex, c, depth + 1);