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.
using System;
using Imsl.Math;
using Imsl.Stat;
public class SortEx1
{
public static void Main(String[] args)
{
double[] ra = new double[]{ 10.0, - 9.0, 8.0, - 7.0, 6.0,
5.0, 4.0, - 3.0, - 2.0, - 1.0};
int[] iperm = new int[]{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);
Console.Out.WriteLine();
// 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 C# source.