forked from fortran-lang/stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdlib_sorting.fypp
540 lines (504 loc) · 23.5 KB
/
stdlib_sorting.fypp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
#:include "common.fypp"
#:set INT_TYPES_ALT_NAME = list(zip(INT_TYPES, INT_TYPES, INT_KINDS))
#:set REAL_TYPES_ALT_NAME = list(zip(REAL_TYPES, REAL_TYPES, REAL_KINDS))
#:set STRING_TYPES_ALT_NAME = list(zip(STRING_TYPES, STRING_TYPES, STRING_KINDS))
#:set CHAR_TYPES_ALT_NAME = list(zip(["character(len=*)"], ["character(len=len(array))"], ["char"]))
#:set BITSET_TYPES_ALT_NAME = list(zip(BITSET_TYPES, BITSET_TYPES, BITSET_KINDS))
#! For better code reuse in fypp, make lists that contain the input types,
#! with each having output types and a separate name prefix for subroutines
#! This approach allows us to have the same code for all input types.
#:set IRSCB_TYPES_ALT_NAME = INT_TYPES_ALT_NAME + REAL_TYPES_ALT_NAME + STRING_TYPES_ALT_NAME + CHAR_TYPES_ALT_NAME &
& + BITSET_TYPES_ALT_NAME
!! Licensing:
!!
!! This file is subject both to the Fortran Standard Library license, and
!! to additional licensing requirements as it contains translations of
!! other software.
!!
!! The Fortran Standard Library, including this file, is distributed under
!! the MIT license that should be included with the library's distribution.
!!
!! Copyright (c) 2021 Fortran stdlib developers
!!
!! Permission is hereby granted, free of charge, to any person obtaining a
!! copy of this software and associated documentation files (the
!! "Software"), to deal in the Software without restriction, including
!! without limitation the rights to use, copy, modify, merge, publish,
!! distribute, sublicense, and/or sellcopies of the Software, and to permit
!! persons to whom the Software is furnished to do so, subject to the
!! following conditions:
!!
!! The above copyright notice and this permission notice shall be included
!! in all copies or substantial portions of the Software.
!!
!! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
!! OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
!! MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
!! IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
!! CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
!! TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
!! SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
!!
!! Two of the generic subroutines, `ORD_SORT` and `SORT_INDEX`, are
!! substantially translations to Fortran 2008 of the `"Rust" sort` sorting
!! routines in
!! [`slice.rs`](https://github.com/rust-lang/rust/blob/90eb44a5897c39e3dff9c7e48e3973671dcd9496/src/liballoc/slice.rs)
!! The `rust sort` implementation is distributed with the header:
!!
!! Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
!! file at the top-level directory of this distribution and at
!! http://rust-lang.org/COPYRIGHT.
!!
!! Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
!! http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
!! <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
!! option. This file may not be copied, modified, or distributed
!! except according to those terms.
!!
!! so the license for the original`slice.rs` code is compatible with the use
!! of modified versions of the code in the Fortran Standard Library under
!! the MIT license.
!!
!! One of the generic subroutines, `SORT`, is substantially a
!! translation to Fortran 2008, of the `introsort` of David Musser.
!! David Musser has given permission to include a variant of `introsort`
!! in the Fortran Standard Library under the MIT license provided
!! we cite:
!!
!! Musser, D.R., “Introspective Sorting and Selection Algorithms,”
!! Software—Practice and Experience, Vol. 27(8), 983–993 (August 1997).
!!
!! as the official source of the algorithm.
module stdlib_sorting
!! This module implements overloaded sorting subroutines named `ORD_SORT`,
!! `SORT_INDEX`, and `SORT`, that each can be used to sort four kinds
!! of `INTEGER` arrays, three kinds of `REAL` arrays, `character(len=*)` arrays,
!! and arrays of `type(string_type)`.
!! ([Specification](../page/specs/stdlib_sorting.html))
!!
!! By default sorting is in order of
!! increasing value, but there is an option to sort in decreasing order.
!! All the subroutines have worst case run time performance of `O(N Ln(N))`,
!! but on largely sorted data `ORD_SORT` and `SORT_INDEX` can have a run time
!! performance of `O(N)`.
!!
!! `ORD_SORT` is a translation of the `"Rust" sort` sorting algorithm in
!! `slice.rs`:
!! https://github.com/rust-lang/rust/blob/90eb44a5897c39e3dff9c7e48e3973671dcd9496/src/liballoc/slice.rs
!! which in turn is inspired by the `timsort` algorithm of Tim Peters,
!! http://svn.python.org/projects/python/trunk/Objects/listsort.txt.
!! `ORD_SORT` is a hybrid stable comparison algorithm combining `merge sort`,
!! and `insertion sort`. It is always at worst O(N Ln(N)) in sorting random
!! data, having a performance about 25% slower than `SORT` on such
!! data, but has much better performance than `SORT` on partially
!! sorted data, having O(N) performance on uniformly non-increasing or
!! non-decreasing data.
!!
!! `SORT_INDEX` is a modification of `ORD_SORT` so that in addition to
!! sorting the input array, it returns the indices that map to a
!! stable sort of the original array. These indices are
!! intended to be used to sort data that is correlated with the input
!! array, e.g., different arrays in a database, different columns of a
!! rank 2 array, different elements of a derived type. It is less
!! efficient than `ORD_SORT` at sorting a simple array.
!!
!! `SORT` uses the `INTROSORT` sorting algorithm of David Musser,
!! http://www.cs.rpi.edu/~musser/gp/introsort.ps. `introsort` is a hybrid
!! unstable comparison algorithm combining `quicksort`, `insertion sort`, and
!! `heap sort`. While this algorithm is always O(N Ln(N)) it is relatively
!! fast on randomly ordered data, but inconsistent in performance on partly
!! sorted data, sometimes having `merge sort` performance, sometimes having
!! better than `quicksort` performance. `UNORD_SOORT` is about 25%
!! more efficient than `ORD_SORT` at sorting purely random data, but af an
!! order of `Ln(N)` less efficient at sorting partially sorted data.
use stdlib_kinds, only: &
int8, &
int16, &
int32, &
int64, &
sp, &
dp, &
xdp, &
qp
use stdlib_optval, only: optval
use stdlib_string_type, only: string_type, assignment(=), operator(>), &
operator(>=), operator(<), operator(<=)
use stdlib_bitsets, only: bitset_64, bitset_large, &
assignment(=), operator(>), operator(>=), operator(<), operator(<=)
implicit none
private
integer, parameter, public :: int_size = int64 !! Integer kind for indexing
! Constants for use by tim_sort
integer, parameter :: &
! The maximum number of entries in a run stack, good for an array of
! 2**64 elements see
! https://svn.python.org/projects/python/trunk/Objects/listsort.txt
max_merge_stack = int( ceiling( log( 2._dp**64 ) / &
log(1.6180339887_dp) ) )
type run_type
!! Version: experimental
!!
!! Used to pass state around in a stack among helper functions for the
!! `ORD_SORT` and `SORT_INDEX` algorithms
integer(int_size) :: base = 0
integer(int_size) :: len = 0
end type run_type
public ord_sort
!! Version: experimental
!!
!! The generic subroutine implementing the `ORD_SORT` algorithm to return
!! an input array with its elements sorted in order of (non-)decreasing
!! value. Its use has the syntax:
!!
!! call ord_sort( array[, work, reverse] )
!!
!! with the arguments:
!!
!! * array: the rank 1 array to be sorted. It is an `intent(inout)`
!! argument of any of the types `integer(int8)`, `integer(int16)`,
!! `integer(int32)`, `integer(int64)`, `real(real32)`, `real(real64)`,
!! `real(real128)`, `character(*)`, `type(string_type)`,
!! `type(bitset_64)`, `type(bitset_large)`. If both the
!! type of `array` is real and at least one of the elements is a
!! `NaN`, then the ordering of the result is undefined. Otherwise it
!! is defined to be the original elements in non-decreasing order.
!!
!! * work (optional): shall be a rank 1 array of the same type as
!! `array`, and shall have at least `size(array)/2` elements. It is an
!! `intent(out)` argument to be used as "scratch" memory
!! for internal record keeping. If associated with an array in static
!! storage, its use can significantly reduce the stack memory requirements
!! for the code. Its value on return is undefined.
!!
!! * `reverse` (optional): shall be a scalar of type default logical. It
!! is an `intent(in)` argument. If present with a value of `.true.` then
!! `array` will be sorted in order of non-increasing values in stable
!! order. Otherwise index will sort `array` in order of non-decreasing
!! values in stable order.
!!
!!#### Example
!!
!!```fortran
!! ...
!! ! Read arrays from sorted files
!! call read_sorted_file( 'dummy_file1', array1 )
!! call read_sorted_file( 'dummy_file2', array2 )
!! ! Concatenate the arrays
!! allocate( array( size(array1) + size(array2) ) )
!! array( 1:size(array1) ) = array1(:)
!! array( size(array1)+1:size(array1)+size(array2) ) = array2(:)
!! ! Sort the resulting array
!! call ord_sort( array, work )
!! ! Process the sorted array
!! call array_search( array, values )
!! ...
!!```
public sort
!! Version: experimental
!!
!! The generic subroutine implementing the `SORT` algorithm to return
!! an input array with its elements sorted in order of (non-)decreasing
!! value. Its use has the syntax:
!!
!! call sort( array[, reverse] )
!!
!! with the arguments:
!!
!! * array: the rank 1 array to be sorted. It is an `intent(inout)`
!! argument of any of the types `integer(int8)`, `integer(int16)`,
!! `integer(int32)`, `integer(int64)`, `real(real32)`, `real(real64)`,
!! `real(real128)`, `character(*)`, `type(string_type)`,
!! `type(bitset_64)`, `type(bitset_large)`. If both the type
!! of `array` is real and at least one of the elements is a `NaN`, then
!! the ordering of the result is undefined. Otherwise it is defined to be the
!! original elements in non-decreasing order.
!! * `reverse` (optional): shall be a scalar of type default logical. It
!! is an `intent(in)` argument. If present with a value of `.true.` then
!! `array` will be sorted in order of non-increasing values in unstable
!! order. Otherwise index will sort `array` in order of non-decreasing
!! values in unstable order.
!!
!!#### Example
!!
!!```fortran
!! ...
!! ! Read random data from a file
!! call read_file( 'dummy_file', array )
!! ! Sort the random data
!! call sort( array )
!! ! Process the sorted data
!! call array_search( array, values )
!! ...
!!```
public radix_sort
!! Version: experimental
!!
!! The generic subroutine implementing the LSD radix sort algorithm to return
!! an input array with its elements sorted in order of (non-)decreasing
!! value. Its use has the syntax:
!!
!! call radix_sort( array[, work, reverse] )
!!
!! with the arguments:
!!
!! * array: the rank 1 array to be sorted. It is an `intent(inout)`
!! argument of any of the types `integer(int8)`, `integer(int16)`,
!! `integer(int32)`, `integer(int64)`, `real(real32)`, `real(real64)`.
!! If both the type of `array` is real and at least one of the
!! elements is a `NaN`, then the ordering of the result is undefined.
!! Otherwise it is defined to be the original elements in
!! non-decreasing order. Especially, -0.0 is lesser than 0.0.
!!
!! * work (optional): shall be a rank 1 array of the same type as
!! `array`, and shall have at least `size(array)` elements. It is an
!! `intent(inout)` argument to be used as buffer. Its value on return is
!! undefined. If it is not present, `radix_sort` will allocate a
!! buffer for use, and deallocate it before return. If you do several
!! similar `radix_sort`s, reusing the `work` array is a good parctice.
!! This argument is not present for `int8_radix_sort` because it use
!! counting sort, so no buffer is needed.
!!
!! * `reverse` (optional): shall be a scalar of type default logical. It
!! is an `intent(in)` argument. If present with a value of `.true.` then
!! `array` will be sorted in order of non-increasing values in stable
!! order. Otherwise index will sort `array` in order of non-decreasing
!! values in stable order.
!!
!!#### Example
!!
!!```fortran
!! ...
!! ! Read random data from a file
!! call read_file( 'dummy_file', array )
!! ! Sort the random data
!! call radix_sort( array )
!! ...
!!```
public sort_index
!! Version: experimental
!!
!! The generic subroutine implementing the `SORT_INDEX` algorithm to
!! return an index array whose elements would sort the input array in the
!! desired direction. It is primarily intended to be used to sort a
!! derived type array based on the values of a component of the array.
!! Its use has the syntax:
!!
!! call sort_index( array, index[, work, iwork, reverse ] )
!!
!! with the arguments:
!!
!! * array: the rank 1 array to be sorted. It is an `intent(inout)`
!! argument of any of the types `integer(int8)`, `integer(int16)`,
!! `integer(int32)`, `integer(int64)`, `real(real32)`, `real(real64)`,
!! `real(real128)`, `character(*)`, `type(string_type)`,
!! `type(bitset_64)`, `type(bitset_large)`. If both the
!! type of `array` is real and at least one of the elements is a `NaN`,
!! then the ordering of the `array` and `index` results is undefined.
!! Otherwise it is defined to be as specified by reverse.
!!
!! * index: a rank 1 array of sorting indices. It is an `intent(out)`
!! argument of the type `integer(int_size)`. Its size shall be the
!! same as `array`. On return, if defined, its elements would
!! sort the input `array` in the direction specified by `reverse`.
!!
!! * work (optional): shall be a rank 1 array of the same type as
!! `array`, and shall have at least `size(array)/2` elements. It is an
!! `intent(out)` argument to be used as "scratch" memory
!! for internal record keeping. If associated with an array in static
!! storage, its use can significantly reduce the stack memory requirements
!! for the code. Its value on return is undefined.
!!
!! * iwork (optional): shall be a rank 1 integer array of kind `int_size`,
!! and shall have at least `size(array)/2` elements. It is an
!! `intent(out)` argument to be used as "scratch" memory
!! for internal record keeping. If associated with an array in static
!! storage, its use can significantly reduce the stack memory requirements
!! for the code. Its value on return is undefined.
!!
!! * `reverse` (optional): shall be a scalar of type default logical. It
!! is an `intent(in)` argument. If present with a value of `.true.` then
!! `index` will sort `array` in order of non-increasing values in stable
!! order. Otherwise index will sort `array` in order of non-decreasing
!! values in stable order.
!!
!!#### Examples
!!
!! Sorting a related rank one array:
!!
!!```Fortran
!! subroutine sort_related_data( a, b, work, index, iwork )
!! ! Sort `b` in terms or its related array `a`
!! integer, intent(inout) :: a(:)
!! integer(int32), intent(inout) :: b(:) ! The same size as a
!! integer(int32), intent(out) :: work(:)
!! integer(int_size), intent(out) :: index(:)
!! integer(int_size), intent(out) :: iwork(:)
!! ! Find the indices to sort a
!! call sort_index(a, index(1:size(a)),&
!! work(1:size(a)/2), iwork(1:size(a)/2))
!! ! Sort b based on the sorting of a
!! b(:) = b( index(1:size(a)) )
!! end subroutine sort_related_data
!!```
!!
!! Sorting a rank 2 array based on the data in a column
!!
!!```Fortran
!! subroutine sort_related_data( array, column, work, index, iwork )
!! ! Sort `a_data` in terms or its component `a`
!! integer, intent(inout) :: a(:,:)
!! integer(int32), intent(in) :: column
!! integer(int32), intent(out) :: work(:)
!! integer(int_size), intent(out) :: index(:)
!! integer(int_size), intent(out) :: iwork(:)
!! integer, allocatable :: dummy(:)
!! integer :: i
!! allocate(dummy(size(a, dim=1)))
!! ! Extract a component of `a_data`
!! dummy(:) = a(:, column)
!! ! Find the indices to sort the column
!! call sort_index(dummy, index(1:size(dummy)),&
!! work(1:size(dummy)/2), iwork(1:size(dummy)/2))
!! ! Sort a based on the sorting of its column
!! do i=1, size(a, dim=2)
!! a(:, i) = a(index(1:size(a, dim=1)), i)
!! end do
!! end subroutine sort_related_data
!!```
!!
!! Sorting an array of a derived type based on the dsta in one component
!!```fortran
!! subroutine sort_a_data( a_data, a, work, index, iwork )
!! ! Sort `a_data` in terms or its component `a`
!! type(a_type), intent(inout) :: a_data(:)
!! integer(int32), intent(inout) :: a(:)
!! integer(int32), intent(out) :: work(:)
!! integer(int_size), intent(out) :: index(:)
!! integer(int_size), intent(out) :: iwork(:)
!! ! Extract a component of `a_data`
!! a(1:size(a_data)) = a_data(:) % a
!! ! Find the indices to sort the component
!! call sort_index(a(1:size(a_data)), index(1:size(a_data)),&
!! work(1:size(a_data)/2), iwork(1:size(a_data)/2))
!! ! Sort a_data based on the sorting of that component
!! a_data(:) = a_data( index(1:size(a_data)) )
!! end subroutine sort_a_data
!!```
interface ord_sort
!! Version: experimental
!!
!! The generic subroutine interface implementing the `ORD_SORT` algorithm,
!! a translation to Fortran 2008, of the `"Rust" sort` algorithm found in
!! `slice.rs`
!! https://github.com/rust-lang/rust/blob/90eb44a5897c39e3dff9c7e48e3973671dcd9496/src/liballoc/slice.rs#L2159
!! `ORD_SORT` is a hybrid stable comparison algorithm combining `merge sort`,
!! and `insertion sort`.
!! ([Specification](../page/specs/stdlib_sorting.html#ord_sort-sorts-an-input-array))
!!
!! It is always at worst O(N Ln(N)) in sorting random
!! data, having a performance about 25% slower than `SORT` on such
!! data, but has much better performance than `SORT` on partially
!! sorted data, having O(N) performance on uniformly non-increasing or
!! non-decreasing data.
#:for t1, t2, name1 in IRSCB_TYPES_ALT_NAME
module subroutine ${name1}$_ord_sort( array, work, reverse )
!! Version: experimental
!!
!! `${name1}$_ord_sort( array )` sorts the input `ARRAY` of type `${t1}$`
!! using a hybrid sort based on the `"Rust" sort` algorithm found in `slice.rs`
${t1}$, intent(inout) :: array(0:)
${t2}$, intent(out), optional :: work(0:)
logical, intent(in), optional :: reverse
end subroutine ${name1}$_ord_sort
#:endfor
end interface ord_sort
interface radix_sort
!! Version: experimental
!!
!! The generic subroutine interface implementing the LSD radix sort algorithm,
!! see https://en.wikipedia.org/wiki/Radix_sort for more details.
!! It is always O(N) in sorting random data, but need a O(N) buffer.
!! ([Specification](../page/specs/stdlib_sorting.html#radix_sort-sorts-an-input-array))
!!
pure module subroutine int8_radix_sort(array, reverse)
integer(kind=int8), dimension(:), intent(inout) :: array
logical, intent(in), optional :: reverse
end subroutine int8_radix_sort
pure module subroutine int16_radix_sort(array, work, reverse)
integer(kind=int16), dimension(:), intent(inout) :: array
integer(kind=int16), dimension(:), intent(inout), target, optional :: work
logical, intent(in), optional :: reverse
end subroutine int16_radix_sort
pure module subroutine int32_radix_sort(array, work, reverse)
integer(kind=int32), dimension(:), intent(inout) :: array
integer(kind=int32), dimension(:), intent(inout), target, optional :: work
logical, intent(in), optional :: reverse
end subroutine int32_radix_sort
pure module subroutine int64_radix_sort(array, work, reverse)
integer(kind=int64), dimension(:), intent(inout) :: array
integer(kind=int64), dimension(:), intent(inout), target, optional :: work
logical, intent(in), optional :: reverse
end subroutine int64_radix_sort
module subroutine sp_radix_sort(array, work, reverse)
real(kind=sp), dimension(:), intent(inout), target :: array
real(kind=sp), dimension(:), intent(inout), target, optional :: work
logical, intent(in), optional :: reverse
end subroutine sp_radix_sort
module subroutine dp_radix_sort(array, work, reverse)
real(kind=dp), dimension(:), intent(inout), target :: array
real(kind=dp), dimension(:), intent(inout), target, optional :: work
logical, intent(in), optional :: reverse
end subroutine dp_radix_sort
end interface radix_sort
interface sort
!! Version: experimental
!!
!! The generic subroutine interface implementing the `SORT` algorithm, based
!! on the `introsort` of David Musser.
!! ([Specification](../page/specs/stdlib_sorting.html#sort-sorts-an-input-array))
#:for t1, t2, name1 in IRSCB_TYPES_ALT_NAME
pure module subroutine ${name1}$_sort( array, reverse )
!! Version: experimental
!!
!! `${name1}$_sort( array[, reverse] )` sorts the input `ARRAY` of type `${t1}$`
!! using a hybrid sort based on the `introsort` of David Musser.
!! The algorithm is of order O(N Ln(N)) for all inputs.
!! Because it relies on `quicksort`, the coefficient of the O(N Ln(N))
!! behavior is small for random data compared to other sorting algorithms.
${t1}$, intent(inout) :: array(0:)
logical, intent(in), optional :: reverse
end subroutine ${name1}$_sort
#:endfor
end interface sort
interface sort_index
!! Version: experimental
!!
!! The generic subroutine interface implementing the `SORT_INDEX` algorithm,
!! based on the `"Rust" sort` algorithm found in `slice.rs`
!! https://github.com/rust-lang/rust/blob/90eb44a5897c39e3dff9c7e48e3973671dcd9496/src/liballoc/slice.rs#L2159
!! but modified to return an array of indices that would provide a stable
!! sort of the rank one `ARRAY` input.
!! ([Specification](../page/specs/stdlib_sorting.html#sort_index-creates-an-array-of-sorting-indices-for-an-input-array-while-also-sorting-the-array))
!!
!! The indices by default correspond to a
!! non-decreasing sort, but if the optional argument `REVERSE` is present
!! with a value of `.TRUE.` the indices correspond to a non-increasing sort.
#:for t1, t2, name1 in IRSCB_TYPES_ALT_NAME
module subroutine ${name1}$_sort_index( array, index, work, iwork, &
reverse )
!! Version: experimental
!!
!! `${name1}$_sort_index( array, index[, work, iwork, reverse] )` sorts
!! an input `ARRAY` of type `${t1}$`
!! using a hybrid sort based on the `"Rust" sort` algorithm found in `slice.rs`
!! and returns the sorted `ARRAY` and an array `INDEX` of indices in the
!! order that would sort the input `ARRAY` in the desired direction.
${t1}$, intent(inout) :: array(0:)
integer(int_size), intent(out) :: index(0:)
${t2}$, intent(out), optional :: work(0:)
integer(int_size), intent(out), optional :: iwork(0:)
logical, intent(in), optional :: reverse
end subroutine ${name1}$_sort_index
#:endfor
end interface sort_index
end module stdlib_sorting