1 /* orderi.f -- translated by f2c (version 19980913).
2 You must link the resulting object file with the libraries:
3 -lf2c -lm (in that order)
4 */
5
6 #include "f2c.h"
7
8 /* $Procedure ORDERI ( Order of an integer array ) */
orderi_(integer * array,integer * ndim,integer * iorder)9 /* Subroutine */ int orderi_(integer *array, integer *ndim, integer *iorder)
10 {
11 /* System generated locals */
12 integer i__1;
13
14 /* Local variables */
15 integer i__, j;
16 extern /* Subroutine */ int swapi_(integer *, integer *);
17 integer jg, gap;
18
19 /* $ Abstract */
20
21 /* Determine the order of elements in an integer array. */
22
23 /* $ Disclaimer */
24
25 /* THIS SOFTWARE AND ANY RELATED MATERIALS WERE CREATED BY THE */
26 /* CALIFORNIA INSTITUTE OF TECHNOLOGY (CALTECH) UNDER A U.S. */
27 /* GOVERNMENT CONTRACT WITH THE NATIONAL AERONAUTICS AND SPACE */
28 /* ADMINISTRATION (NASA). THE SOFTWARE IS TECHNOLOGY AND SOFTWARE */
29 /* PUBLICLY AVAILABLE UNDER U.S. EXPORT LAWS AND IS PROVIDED "AS-IS" */
30 /* TO THE RECIPIENT WITHOUT WARRANTY OF ANY KIND, INCLUDING ANY */
31 /* WARRANTIES OF PERFORMANCE OR MERCHANTABILITY OR FITNESS FOR A */
32 /* PARTICULAR USE OR PURPOSE (AS SET FORTH IN UNITED STATES UCC */
33 /* SECTIONS 2312-2313) OR FOR ANY PURPOSE WHATSOEVER, FOR THE */
34 /* SOFTWARE AND RELATED MATERIALS, HOWEVER USED. */
35
36 /* IN NO EVENT SHALL CALTECH, ITS JET PROPULSION LABORATORY, OR NASA */
37 /* BE LIABLE FOR ANY DAMAGES AND/OR COSTS, INCLUDING, BUT NOT */
38 /* LIMITED TO, INCIDENTAL OR CONSEQUENTIAL DAMAGES OF ANY KIND, */
39 /* INCLUDING ECONOMIC DAMAGE OR INJURY TO PROPERTY AND LOST PROFITS, */
40 /* REGARDLESS OF WHETHER CALTECH, JPL, OR NASA BE ADVISED, HAVE */
41 /* REASON TO KNOW, OR, IN FACT, SHALL KNOW OF THE POSSIBILITY. */
42
43 /* RECIPIENT BEARS ALL RISK RELATING TO QUALITY AND PERFORMANCE OF */
44 /* THE SOFTWARE AND ANY RELATED MATERIALS, AND AGREES TO INDEMNIFY */
45 /* CALTECH AND NASA FOR ALL THIRD-PARTY CLAIMS RESULTING FROM THE */
46 /* ACTIONS OF RECIPIENT IN THE USE OF THE SOFTWARE. */
47
48 /* $ Required_Reading */
49
50 /* None. */
51
52 /* $ Keywords */
53
54 /* ARRAY, SORT */
55
56 /* $ Declarations */
57 /* $ Brief_I/O */
58
59 /* VARIABLE I/O DESCRIPTION */
60 /* -------- --- -------------------------------------------------- */
61 /* ARRAY I Input array. */
62 /* NDIM I Dimension of ARRAY. */
63 /* IORDER O Order vector for ARRAY. */
64
65 /* $ Detailed_Input */
66
67 /* ARRAY is the input array. */
68
69 /* NDIM is the number of elements in the input array. */
70
71 /* $ Detailed_Output */
72
73 /* IORDER is the order vector for the input array. */
74 /* IORDER(1) is the index of the smallest element */
75 /* of ARRAY; IORDER(2) is the index of the next */
76 /* smallest; and so on. */
77
78 /* $ Parameters */
79
80 /* None. */
81
82 /* $ Exceptions */
83
84 /* 1) A negative input dimension causes this routine to */
85 /* leave the output order vector unchanged. */
86
87 /* This routine is error free. */
88
89 /* $ Files */
90
91 /* None. */
92
93 /* $ Particulars */
94
95 /* ORDERI finds the index of the smallest element of the input */
96 /* array. This becomes the first element of the order vector. */
97 /* The process is repeated for the rest of the elements. */
98
99 /* The order vector returned by ORDERI may be used by any of */
100 /* the REORD routines to sort sets of related arrays, as shown */
101 /* in the example below. */
102
103 /* $ Examples */
104
105 /* In the following example, the ORDER and REORD routines are */
106 /* used to sort four related arrays (containing the names, */
107 /* masses, integer ID codes, and visual magnitudes for a group */
108 /* of satellites). This is representative of the typical use of */
109 /* these routines. */
110
111 /* C */
112 /* C Sort the object arrays by ID code. */
113 /* C */
114 /* CALL ORDERI ( CODES, N, IORDER ) */
115
116 /* CALL REORDC ( IORDER, N, NAMES ) */
117 /* CALL REORDD ( IORDER, N, MASSES ) */
118 /* CALL REORDI ( IORDER, N, CODES ) */
119 /* CALL REORDR ( IORDER, N, VMAGS ) */
120
121 /* $ Restrictions */
122
123 /* None. */
124
125 /* $ Literature_References */
126
127 /* None. */
128
129 /* $ Author_and_Institution */
130
131 /* I.M. Underwood (JPL) */
132
133 /* $ Version */
134
135 /* - SPICELIB Version 1.0.2, 23-MAR-2010 (NJB) */
136
137 /* Header example was updated to show use of this routine. */
138 /* Exceptions section was updated. Header sections were */
139 /* re-ordered. */
140
141 /* - SPICELIB Version 1.0.1, 10-MAR-1992 (WLT) */
142
143 /* Comment section for permuted index source lines was added */
144 /* following the header. */
145
146 /* - SPICELIB Version 1.0.0, 31-JAN-1990 (IMU) */
147
148 /* -& */
149 /* $ Index_Entries */
150
151 /* order of an integer array */
152
153 /* -& */
154
155 /* Local variables */
156
157
158 /* Begin with the initial ordering. */
159
160 i__1 = *ndim;
161 for (i__ = 1; i__ <= i__1; ++i__) {
162 iorder[i__ - 1] = i__;
163 }
164
165 /* Find the smallest element, then the next smallest, and so on. */
166 /* This uses the Shell Sort algorithm, but swaps the elements of */
167 /* the order vector instead of the array itself. */
168
169 gap = *ndim / 2;
170 while(gap > 0) {
171 i__1 = *ndim;
172 for (i__ = gap + 1; i__ <= i__1; ++i__) {
173 j = i__ - gap;
174 while(j > 0) {
175 jg = j + gap;
176 if (array[iorder[j - 1] - 1] <= array[iorder[jg - 1] - 1]) {
177 j = 0;
178 } else {
179 swapi_(&iorder[j - 1], &iorder[jg - 1]);
180 }
181 j -= gap;
182 }
183 }
184 gap /= 2;
185 }
186 return 0;
187 } /* orderi_ */
188
189