Skip to content

Commit 6c4d3e2

Browse files
committed
update specs
1 parent 5a6f463 commit 6c4d3e2

File tree

1 file changed

+38
-34
lines changed

1 file changed

+38
-34
lines changed

doc/specs/stdlib_sorting.md

Lines changed: 38 additions & 34 deletions
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,12 @@ 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+
139+
**QUESTION: is the `reverse` mode still a stable comparison algorithm?**
140+
141+
137142
#### The `SORT_INDEX` subroutine
138143

139144
The `SORT` and `ORD_SORT` subroutines can sort rank 1 isolated
@@ -205,11 +210,11 @@ Experimental
205210
##### Description
206211

207212
Returns an input `array` with the elements sorted in order of
208-
increasing value.
213+
increasing, or decreasing, value.
209214

210215
##### Syntax
211216

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

214219
##### Class
215220

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

241+
`reverse` (optional): shall be a scalar of type default logical. It
242+
is an `intent(in)` argument. If present with a value of `.true.` then
243+
`array` will be sorted in order of non-increasing values in stable
244+
order **(stable order: is it still true?)**. Otherwise index will sort `array` in order of non-decreasing
245+
values in stable order.
246+
236247
##### Notes
237248

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

252263

253264
##### Example
254265

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-
269266
```fortran
270267
program demo_ord_sort
271268
use stdlib_sorting, only: ord_sort
@@ -287,12 +284,12 @@ Experimental
287284

288285
##### Description
289286

290-
Returns an input array with the elements sorted in order of increasing
291-
value.
287+
Returns an input array with the elements sorted in order of increasing, or
288+
decreasing, value.
292289

293290
##### Syntax
294291

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

297294
##### Class
298295

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

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

311315
`SORT` implements a hybrid sorting algorithm combining

0 commit comments

Comments
 (0)