public class Sort extends Object
Class Sort
contains ascending and descending methods for sorting elements
of an array or a matrix.
The QuickSort algorithm is used, except for short sequences that are handled using an insertion sort.
The QuickSort algorithm is a randomized QuickSort with 3-way partitioning. Basic QuickSort is slow if the sequence to be sorted contains many duplicate keys. The 3-way partitioning algorithm eliminates this problem. The pivot is chosen as the middle element of three potential pivots chosen at random.
The matrix ascending method sorts the rows of real matrix x
using a
particular column or columns in x
as the keys. The sort is algebraic with the
first key as the most significant, the second key as the next most significant,
etc. When x is sorted in ascending order, the resulting sorted array is such
that the following is true:
The observations also can be sorted in descending order.
The rows ofx
containing the missing value code NaN in at least one of the
specified columns are considered as an additional group. These rows are
moved to the end of the sorted x
.
If all of the sort keys in a pair of rows are equal then the rows keep their original relative order.
The sorting algorithm is based on a quicksort method given by Singleton (1969) with modifications, see Bentley and Sedgewick (1997).
Modifier and Type | Method and Description |
---|---|
static void |
ascending(double[] ra)
Sorts an array into ascending order.
|
static void |
ascending(double[][] ra,
int nKeys)
Sorts a matrix into ascending order by the first
nKeys . |
static void |
ascending(double[][] ra,
int[] indKeys)
Sorts a matrix into ascending order by specified keys.
|
static void |
ascending(double[][] ra,
int[] indKeys,
int[] iPerm)
Sorts a matrix into ascending order by specified keys and returns the
permutation vector.
|
static void |
ascending(double[][] ra,
int nKeys,
int[] iPerm)
Sorts a matrix into ascending order according to the first
nKeys keys and returns the permutation vector. |
static void |
ascending(double[] ra,
int[] iPerm)
Sorts an array into ascending order and returns the permutation vector.
|
static void |
ascending(int[] ia)
Sorts an integer array into ascending order.
|
static void |
ascending(int[][] ia,
int nKeys)
Sorts a matrix into ascending order by the first
nKeys . |
static void |
ascending(int[][] ia,
int[] indKeys,
int[] iPerm)
Sorts a matrix into ascending order by specified keys and returns the
permutation vector.
|
static void |
ascending(int[][] ia,
int nKeys,
int[] iPerm)
Sorts a matrix into ascending order according to the first
nKeys keys and returns the permutation vector. |
static void |
ascending(int[] ia,
int[] iPerm)
Sorts an integer array into ascending order and returns the permutation
vector.
|
static void |
descending(double[] ra)
Sorts an array into descending order.
|
static void |
descending(double[][] ra,
int nKeys)
Sorts a matrix into descending order by the first
nkeys . |
static void |
descending(double[][] ra,
int[] indKeys)
Sorts a matrix into descending order by specified keys.
|
static void |
descending(double[][] ra,
int[] indKeys,
int[] iPerm)
Sorts a matrix into descending order by specified keys and return the
permutation vector.
|
static void |
descending(double[][] ra,
int nKeys,
int[] iPerm)
Sorts a matrix into descending order by the first
nkeys and
returns the permutation vector. |
static void |
descending(double[] ra,
int[] iPerm)
Sorts an array into descending order and returns the permutation vector.
|
static void |
descending(int[] ra)
Sorts an integer array into descending order.
|
static void |
descending(int[] ra,
int[] iPerm)
Sorts an integer array into descending order and returns the permutation
vector.
|
public static void ascending(int[] ia)
ia
- an int
array to be sorted into ascending orderpublic static void descending(int[] ra)
ra
- an int
array to be sorted into descending orderpublic static void ascending(int[] ia, int[] iPerm)
ia
- an int
array to be sorted into ascending orderiPerm
- an int
array that on input is the same length
as ia
and contains the values 0, 1, .... On output, the
values represent the permutation of the values in ia
, that
is, the re-ordering of those values in terms of their previous position.public static void descending(int[] ra, int[] iPerm)
ra
- an int
array to be sorted into descending orderiPerm
- an int
array that on input is the same length
as ra
and contains the values 0, 1, .... On output, the
values represent the permutation of the values in ra
, that
is, the re-ordering of those values in terms of their previous position.public static void ascending(double[] ra)
ra
- a double
array to be sorted into ascending orderpublic static void descending(double[] ra)
ra
- a double
array to be sorted into descending orderpublic static void ascending(double[] ra, int[] iPerm)
ra
- a double
array to be sorted into ascending orderiPerm
- an int
array that on input is the same length
as ra
and contains the values 0, 1, .... On output, the
values represent the permutation of the values in ra
, that
is, the re-ordering of those values in terms of their previous position.public static void descending(double[] ra, int[] iPerm)
ra
- a double
array to be sorted into descending orderiPerm
- an int
array that on input is the same length
as ra
and contains the values 0, 1, .... On output, the
values represent the permutation of the values in ra
, that
is, the re-ordering of those values in terms of their previous position.public static void ascending(double[][] ra, int nKeys)
nKeys
.ra
- a double
matrix to be sorted into ascending ordernKeys
- an int
, the number of keys. The first
nKeys
columns of ra
are to be used as the
sorting keys.public static void ascending(double[][] ra, int[] indKeys)
ra
- a double
matrix to be sorted into ascending orderindKeys
- an int
array containing the order into which
the columns of ra
are to be sorted. These values must be
between 0 and one less than the number of columns in ra
.public static void ascending(double[][] ra, int nKeys, int[] iPerm)
nKeys
keys and returns the permutation vector.ra
- a double
matrix to be sorted into ascending ordernKeys
- an int
, the number of keys. The first
nKeys
columns of ra
are to be used as the
sorting keys.iPerm
- an int
array that on input is the same length
as ra
and contains the values 0, 1, .... On output, the
values represent the permutation of the values in ra
, that
is, the re-ordering of those values in terms of their previous position.public static void ascending(int[][] ia, int nKeys)
nKeys
.ia
- an int
matrix to be sorted into ascending ordernKeys
- an int
, the number of keys. The first
nKeys
columns of ia
are to be used as the
sorting keys.public static void ascending(int[][] ia, int nKeys, int[] iPerm)
nKeys
keys and returns the permutation vector.ia
- an int
matrix to be sorted into ascending ordernKeys
- an int
, the number of keys. The first
nKeys
columns of ia
are to be used as the
sorting keys.iPerm
- an int
array that on input is the same length
as ia
and contains the values 0, 1, .... On output, the
values represent the permutation of the values in ia
, that
is, the re-ordering of those values in terms of their previous position.public static void ascending(double[][] ra, int[] indKeys, int[] iPerm)
ra
- a double
matrix to be sorted into ascending orderindKeys
- an int
array containing the order into which
the columns of ra
are to be sorted. These values must be
between 0 and one less than the number of columns in ra
.iPerm
- an int
array that on input is the same length
as ra
and contains the values 0, 1, .... On output, the
values represent the permutation of the values in ra
, that
is, the re-ordering of those values in terms of their previous position.public static void ascending(int[][] ia, int[] indKeys, int[] iPerm)
ia
- an int
matrix to be sorted into ascending orderindKeys
- an int
array containing the order into which
the columns of ia
are to be sorted. These values must be
between 0 and one less than the number of columns in ia
.iPerm
- an int
array that on input is the same length
as ia
and contains the values 0, 1, .... On output, the
values represent the permutation of the values in ia
, that
is, the re-ordering of those values in terms of their previous position.public static void descending(double[][] ra, int nKeys)
nkeys
.ra
- a double
matrix to be sorted into descending ordernKeys
- an int
, the number of keys. The first
nKeys
columns of ra
are to be used as the
sorting keys.public static void descending(double[][] ra, int[] indKeys)
ra
- a double
matrix to be sorted into descending orderindKeys
- an int
array containing the order into which
the columns of ra
are to be sorted. These values must be
between 0 and one less than the number of columns in ra
.public static void descending(double[][] ra, int nKeys, int[] iPerm)
nkeys
and
returns the permutation vector.ra
- a double
matrix to be sorted into descending ordernKeys
- an int
, the number of keys. The first
nKeys
columns of ra
are to be used as the
sorting keys.iPerm
- an int
array that on input is the same length
as ra
and contains the values 0, 1, .... On output, the
values represent the permutation of the values in ra
, that
is, the re-ordering of those values in terms of their previous position.public static void descending(double[][] ra, int[] indKeys, int[] iPerm)
ra
- a double
matrix to be sorted into descending orderindKeys
- an int
array containing the order into which
the columns of ra
are to be sorted. These values must be
between 0 and one less than the number of columns in ra
.iPerm
- an int
array that on input is the same length
as ra
and contains the values 0, 1, .... On output, the
values represent the permutation of the values in ra
, that
is, the re-ordering of those values in terms of their previous position.Copyright © 2020 Rogue Wave Software. All rights reserved.