Example 1: Sorting
An array is sorted by increasing value. A permutation array is also computed. Note that the permutation array begins at 0 in this example.
import com.imsl.math.*;
import com.imsl.stat.*;
public class SortEx1 {
public static void main(String args[]) {
double ra[] = { 10., -9., 8., -7., 6., 5., 4., -3., -2., -1.};
int iperm[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
PrintMatrix pm = new PrintMatrix("The Input Array");
PrintMatrixFormat mf = new PrintMatrixFormat();
mf.setNoRowLabels();
mf.setNoColumnLabels();
// Print the array
pm.print(mf, ra);
System.out.println();
// Sort the array
Sort.ascending(ra, iperm);
pm = new PrintMatrix("The Sorted Array - Lowest to Highest");
mf = new PrintMatrixFormat();
mf.setNoRowLabels();
mf.setNoColumnLabels();
// Print the array
pm.print(mf, ra);
pm = new PrintMatrix("The Resulting Permutation Array");
mf = new PrintMatrixFormat();
mf.setNoRowLabels();
mf.setNoColumnLabels();
// Print the array
pm.print(mf, iperm);
}
}
Output
The Input Array
10
-9
8
-7
6
5
4
-3
-2
-1
The Sorted Array - Lowest to Highest
-9
-7
-3
-2
-1
4
5
6
8
10
The Resulting Permutation Array
1
3
7
8
9
6
5
4
2
0
Link to Java source.