nag_chain_sort (m01cuc) (PDF version)
m01 Chapter Contents
m01 Chapter Introduction
NAG Library Manual

NAG Library Function Document

nag_chain_sort (m01cuc)

 Contents

    1  Purpose
    7  Accuracy

1  Purpose

nag_chain_sort (m01cuc) rearranges the links of a linked list into ascending or descending order of a specified data field of arbitrary type.

2  Specification

#include <nag.h>
#include <nagm01.h>
void  nag_chain_sort (Pointer *base, ptrdiff_t offset,
Integer (*compare)(const Nag_Pointer a, const Nag_Pointer b),
Nag_SortOrder order, NagError *fail)

3  Description

nag_chain_sort (m01cuc) uses a variant of list merging, as described in Knuth (1973). It uses a local stack to avoid the need for a flag bit in each pointer.

4  References

Knuth D E (1973) The Art of Computer Programming (Volume 3) (2nd Edition) Addison–Wesley

5  Arguments

1:     base Pointer *Input/Output
On entry: the pointer to the pointer to the first structure of the linked list.
On exit: the pointer to which base points is updated to point to the first element of the ordered list.
2:     offset ptrdiff_tInput
On entry: the offset within the structure of the pointer field which points to the next element of the linked list.
Note: this field in the last element of the linked list must have the value NULL.
3:     compare function, supplied by the userExternal Function
nag_chain_sort (m01cuc) compares the data fields of two elements of the list. Its arguments are pointers to the structure, therefore this function must allow for the offset of the data field in the structure (if it is not the first).
The function must return:
-1 if the first data field is less than the second,
0 if the first data field is equal to the second,
1 if the first data field is greater than the second.
The specification of compare is:
Integer  compare (const Nag_Pointer a, const Nag_Pointer b)
1:     a const Nag_Pointer Input
On entry: the first data field.
2:     b const Nag_Pointer Input
On entry: the second data field.
4:     order Nag_SortOrderInput
On entry: specifies whether the array will be sorted into ascending or descending order.
Constraint: order=Nag_Ascending or Nag_Descending.
5:     fail NagError *Input/Output
The NAG error argument (see Section 2.7 in How to Use the NAG Library and its Documentation).

6  Error Indicators and Warnings

NE_BAD_PARAM
On entry, order had an illegal value.
NE_CH_LOOP
Too many elements in chain or chain in a loop. The linked list may have become corrupted.

7  Accuracy

Not applicable.

8  Parallelism and Performance

nag_chain_sort (m01cuc) is not threaded in any implementation.

9  Further Comments

The time taken by nag_chain_sort (m01cuc) is approximately proportional to n logn .

10  Example

The example program declares a structure containing a data field, a pointer to the next record and an index field. It generates an array of such structures assigning random data to the data field. The index field is randomly assigned a unique value. The pointer to next record fields are assigned to create a linked list in the order of the index field. It sorts this linked list into ascending order according to the value of the data field.

10.1  Program Text

Program Text (m01cuce.c)

10.2  Program Data

None.

10.3  Program Results

Program Results (m01cuce.r)


nag_chain_sort (m01cuc) (PDF version)
m01 Chapter Contents
m01 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2016