SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
SplitSort.java
1/*
2 * Class: SplitSort
3 * Description: Performs a split sort on arrays.
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
7 * Organization: DIRO, Universite de Montreal
8 * @author
9 * @since
10
11 * SSJ is free software: you can redistribute it and/or modify it under
12 * the terms of the GNU General Public License (GPL) as published by the
13 * Free Software Foundation, either version 3 of the License, or
14 * any later version.
15
16 * SSJ is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20
21 * A copy of the GNU General Public License is available at
22 <a href="http://www.gnu.org/licenses">GPL licence site</a>.
23 */
24package umontreal.ssj.util.multidimsort;
25
26import java.util.Arrays;
27
42public class SplitSort<T extends MultiDimComparable<? super T>> implements MultiDimSortComparable<T> {
43 private int dimension;
44
50 public SplitSort(int d) {
51 this.dimension = d;
52 }
53
54 public void sort(T[] a, int iMin, int iMax) {
55 if (dimension == 1) {
56 Arrays.sort(a, iMin, iMax, new MultiDimComparator<T>(0));
57 } else {
58 splitSort(a, iMin, iMax, 0);
59 }
60 }
61
62 private void splitSort(T[] a, int iMin, int iMax, int splitCoord) {
63 if (iMin == (iMax - 1))
64 return;
65 Arrays.sort(a, iMin, iMax, new MultiDimComparator<T>(splitCoord));
66 int iMid = (iMin + iMax) / 2;
67 splitSort(a, iMin, iMid, (splitCoord + 1) % dimension);
68 splitSort(a, iMid, iMax, (splitCoord + 1) % dimension);
69 }
70
71 public void sort(T[] a) {
72 sort(a, 0, a.length);
73 }
74
75 public void sort(double[][] a, int iMin, int iMax) {
76 if (dimension == 1) {
77 Arrays.sort(a, iMin, iMax, new DoubleArrayComparator(0));
78 } else {
79 splitSort(a, iMin, iMax, 0);
80 }
81 }
82
83 private void splitSort(double[][] a, int iMin, int iMax, int splitCoord) {
84 if (iMin == (iMax - 1))
85 return;
86 Arrays.sort(a, iMin, iMax, new DoubleArrayComparator(splitCoord));
87 int iMid = (iMin + iMax) / 2;
88 splitSort(a, iMin, iMid, (splitCoord + 1) % dimension);
89 splitSort(a, iMid, iMax, (splitCoord + 1) % dimension);
90 }
91
92 public void sort(double[][] a) {
93 sort(a, 0, a.length);
94 }
95
96 public int dimension() {
97 return dimension;
98 }
99}
This class is useful if one wishes to perform an ordinary one-dimensional sort on MultiDimComparable<...
SplitSort(int d)
Constructs a SplitSort that will use the first d dimensions to sort.
This interface is an extension (or variant) of the Comparable interface in Java.
This interface extends MultiDimSort<T> to implement multivariate sorting algorithms that sort objects...