Skip to content

Commit 1c6e985

Browse files
authored
Merge pull request #3 from jvdp1/sort_sign
Update of the specs for stdlib_sorting
2 parents 5a6f463 + 18ad979 commit 1c6e985

File tree

1 file changed

+35
-34
lines changed

1 file changed

+35
-34
lines changed

doc/specs/stdlib_sorting.md

+35-34
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ data:
5555

5656
The Fortran Standard Library is distributed under the MIT
5757
License. However components of the library may be based on code with
58-
additional licensing restriction. In particular `ORD_SORT`,
58+
additional licensing restrictions. In particular `ORD_SORT`,
5959
`SORT_INDEX`, and `SORT` are translations of codes with their
6060
own distribution restrictions.
6161

6262
The `ORD_SORT` and `SORT_INDEX` subroutines are essentially
63-
translations to Fortran 2008 of the `"rust" sort` of the Rust Language
63+
translations to Fortran 2008 of the `"Rust" sort` of the Rust Language
6464
distributed as part of
6565
[`slice.rs`](https://github.com/rust-lang/rust/blob/90eb44a5897c39e3dff9c7e48e3973671dcd9496/src/liballoc/slice.rs).
6666
The header of the `slice.rs` file has as its licensing requirements:
@@ -75,7 +75,7 @@ The header of the `slice.rs` file has as its licensing requirements:
7575
option. This file may not be copied, modified, or distributed
7676
except according to those terms.
7777

78-
so the license for the `slice.rs` code is compatible with the use of
78+
So the license for the `slice.rs` code is compatible with the use of
7979
modified versions of the code in the Fortran Standard Library under
8080
the MIT license.
8181

@@ -108,17 +108,16 @@ performance than `SORT` on partially sorted data, having O(N)
108108
performance on uniformly increasing or decreasing data.
109109

110110

111-
`ORD_SORT` begins by traversing the array starting in its tail
112-
attempting to identify `runs` in the array, where a run is either a
113-
uniformly decreasing sequence, `ARRAY(i-1) > ARRAY(i)`, or a
114-
non-decreasing, `ARRAY(i-1) <= ARRAY(i)`, sequence. First delimitated
115-
decreasing sequences are reversed in their order. Then, if the
116-
sequence has less than `MIN_RUN` elements, previous elements in the
117-
array are added to the run using `insertion sort` until the run
118-
contains `MIN_RUN` elements or the array is completely processed. As
119-
each run is identified the start and length of the run
120-
are then pushed onto a stack and the stack is then processed using
121-
`merge` until it obeys the stack invariants:
111+
When sorting in an increasing order, `ORD_SORT` begins by traversing the array
112+
starting in its tail attempting to identify `runs` in the array, where a run is
113+
either a uniformly decreasing sequence, `ARRAY(i-1) > ARRAY(i)`, or a
114+
non-decreasing, `ARRAY(i-1) <= ARRAY(i)`, sequence. First delimited decreasing
115+
sequences are reversed in their order. Then, if the sequence has less than
116+
`MIN_RUN` elements, previous elements in the array are added to the run using
117+
`insertion sort` until the run contains `MIN_RUN` elements or the array is
118+
completely processed. As each run is identified the start and length of the run
119+
are then pushed onto a stack and the stack is then processed using `merge` until
120+
it obeys the stack invariants:
122121

123122
1. len(i-2) > len(i-1) + len(i)
124123
2. len(i-1) > len(i)
@@ -134,6 +133,9 @@ structured data. As a modified `merge sort`, `ORD_SORT` requires the
134133
use of a "scratch" array, that may be provided as an optional `work`
135134
argument or allocated internally on the stack.
136135

136+
Arrays can be also sorted in a decreasing order by providing the argument `reverse
137+
= .true.`.
138+
137139
#### The `SORT_INDEX` subroutine
138140

139141
The `SORT` and `ORD_SORT` subroutines can sort rank 1 isolated
@@ -205,11 +207,11 @@ Experimental
205207
##### Description
206208

207209
Returns an input `array` with the elements sorted in order of
208-
increasing value.
210+
increasing, or decreasing, value.
209211

210212
##### Syntax
211213

212-
`call [[stdlib_sorting(module):ord_sort(subroutine)]]ord_sort ( array[, work ] )`
214+
`call [[stdlib_sorting(module):ord_sort(subroutine)]]ord_sort ( array[, work, reverse ] )`
213215

214216
##### Class
215217

@@ -233,6 +235,12 @@ memory for internal record keeping. If associated with an array in
233235
static storage, its use can significantly reduce the stack memory
234236
requirements for the code. Its contents on return are undefined.
235237

238+
`reverse` (optional): shall be a scalar of type default logical. It
239+
is an `intent(in)` argument. If present with a value of `.true.` then
240+
`array` will be sorted in order of non-increasing values in stable
241+
order. Otherwise index will sort `array` in order of non-decreasing
242+
values in stable order.
243+
236244
##### Notes
237245

238246
`ORD_SORT` implements a hybrid sorting algorithm combining
@@ -246,26 +254,12 @@ non-decreasing arrays. The optional `work` array replaces "scratch"
246254
memory that would otherwise be allocated on the stack. If `array` is of
247255
any type `REAL` the order of its elements on return undefined if any
248256
element of `array` is a `NaN`. Sorting of `CHARACTER(*)` and
249-
`STRING_TYPE` arrays are based on the operator `>`, and not on the
257+
`STRING_TYPE` arrays are based on the operators `>` and `<`, and not on the
250258
function `LGT`.
251259

252260

253261
##### Example
254262

255-
```fortran
256-
...
257-
! Read arrays from sorted files
258-
call read_sorted_file( 'dummy_file1', array1 )
259-
call read_sorted_file( 'dummy_file2', array2 )
260-
! Concatenate the arrays
261-
array = [ array1, array2 ]
262-
! Sort the resulting array
263-
call ord_sort( array, work )
264-
! Process the sorted array
265-
call array_search( array, values )
266-
...
267-
```
268-
269263
```fortran
270264
program demo_ord_sort
271265
use stdlib_sorting, only: ord_sort
@@ -287,12 +281,12 @@ Experimental
287281

288282
##### Description
289283

290-
Returns an input array with the elements sorted in order of increasing
291-
value.
284+
Returns an input array with the elements sorted in order of increasing, or
285+
decreasing, value.
292286

293287
##### Syntax
294288

295-
`call [[stdlib_sorting(module):sort(subroutine)]]sort ( array )`
289+
`call [[stdlib_sorting(module):sort(subroutine)]]sort ( array, reverse )`
296290

297291
##### Class
298292

@@ -306,6 +300,13 @@ Pure generic subroutine.
306300
`type(string_type)`. It is an `intent(inout)` argument. On return its
307301
input elements will be sorted in order of non-decreasing value.
308302

303+
304+
`reverse` (optional): shall be a scalar of type default logical. It
305+
is an `intent(in)` argument. If present with a value of `.true.` then
306+
`array` will be sorted in order of non-increasing values in unstable
307+
order. Otherwise index will sort `array` in order of non-decreasing
308+
values in unstable order.
309+
309310
##### Notes
310311

311312
`SORT` implements a hybrid sorting algorithm combining

0 commit comments

Comments
 (0)