sort

Sorts a vector by algebraic value. Optionally, a vector can be sorted by absolute value, and a sort permutation can be returned.

Synopsis

#include <imsl.h>

float *imsl_f_sort (int n, float *x, , 0)

The type double function is imsl_d_sort.

Required Arguments

int n (Input)
The length of the input vector.

float *x (Input)
Input vector to be sorted.

Return Value

A vector of length n containing the values of the input vector x sorted into ascending order. If an error occurs, then NULL is returned.

Synopsis with Optional Arguments

#include <imsl.h>

float *imsl_f_sort (int n, float *x,

IMSL_ABSOLUTE,

IMSL_PERMUTATION, int **perm,

IMSL_PERMUTATION_USER, int perm_user[],

IMSL_RETURN_USER, float y[],

0)

Optional Arguments

IMSL_ABSOLUTE
Sort x by absolute value.

IMSL_PERMUTATION, int **perm (Output)
Return a pointer to the sort permutation.

IMSL_PERMUTATION_USER, int perm_user[] (Output)
Return the sort permutation in user-supplied space.

IMSL_RETURN_USER, float y[] (Output)
Return the sorted data in user-supplied space.

Description

By default, imsl_f_sort sorts the elements of x into ascending order by algebraic value. The vector is divided into two parts by choosing a central element T of the vector. The first and last elements of x are compared with T and exchanged until the three values appear in the vector in ascending order. The elements of the vector are rearranged until all elements greater than or equal to the central elements appear in the second part of the vector and all those less than or equal to the central element appear in the first part. The upper and lower subscripts of one of the segments are saved, and the process continues iteratively on the other segment. When one segment is finally sorted, the process begins again by retrieving the subscripts of another unsorted portion of the vector. On completion, xj  xi for j < i. If the option IMSL_ABSOLUTE is selected, the elements of x are sorted into ascending order by absolute value. If we denote the return vector by y, on completion, |yj |yi| for j < i.

If the option IMSL_PERMUTATION is chosen, a record of the permutations to the array x is returned. That is, after the initialization of permi = i, the elements of perm are moved in the same manner as are the elements of x.

Examples

 

Example 1

In this example, an input vector is sorted algebraically.

 

#include <stdio.h>

#include <imsl.h>

 

int main()

{

float x[] = {1.0, 3.0, -2.0, 4.0};

float *sorted_result;

int n;

 

n = 4;

sorted_result = imsl_f_sort (n, x, 0);

 

imsl_f_write_matrix("Sorted vector", 1, 4, sorted_result, 0);

}

Output

 

Sorted vector

1 2 3 4

-2 1 3 4

Example 2

This example sorts an input vector by absolute value and prints the result stored in user-allocated space.

 

#include <stdio.h>

#include <imsl.h>

 

int main()

{

float x[] = {1.0, 3.0, -2.0, 4.0};

float sorted_result[4];

int n;

 

n = 4;

imsl_f_sort (n, x,

IMSL_ABSOLUTE,

IMSL_RETURN_USER, sorted_result,

0);

 

imsl_f_write_matrix("Sorted vector", 1, 4, sorted_result, 0);

}

Output

 

Sorted vector

1 2 3 4

1 -2 3 4