@@ -55,12 +55,12 @@ data:
55
55
56
56
The Fortran Standard Library is distributed under the MIT
57
57
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 ` ,
59
59
` SORT_INDEX ` , and ` SORT ` are translations of codes with their
60
60
own distribution restrictions.
61
61
62
62
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
64
64
distributed as part of
65
65
[ ` slice.rs ` ] ( https://github.com/rust-lang/rust/blob/90eb44a5897c39e3dff9c7e48e3973671dcd9496/src/liballoc/slice.rs ) .
66
66
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:
75
75
option. This file may not be copied, modified, or distributed
76
76
except according to those terms.
77
77
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
79
79
modified versions of the code in the Fortran Standard Library under
80
80
the MIT license.
81
81
@@ -108,17 +108,16 @@ performance than `SORT` on partially sorted data, having O(N)
108
108
performance on uniformly increasing or decreasing data.
109
109
110
110
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:
122
121
123
122
1 . len(i-2) > len(i-1) + len(i)
124
123
2 . len(i-1) > len(i)
@@ -134,6 +133,12 @@ structured data. As a modified `merge sort`, `ORD_SORT` requires the
134
133
use of a "scratch" array, that may be provided as an optional ` work `
135
134
argument or allocated internally on the stack.
136
135
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
+
137
142
#### The ` SORT_INDEX ` subroutine
138
143
139
144
The ` SORT ` and ` ORD_SORT ` subroutines can sort rank 1 isolated
@@ -205,11 +210,11 @@ Experimental
205
210
##### Description
206
211
207
212
Returns an input ` array ` with the elements sorted in order of
208
- increasing value.
213
+ increasing, or decreasing, value.
209
214
210
215
##### Syntax
211
216
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 ] ) `
213
218
214
219
##### Class
215
220
@@ -233,6 +238,12 @@ memory for internal record keeping. If associated with an array in
233
238
static storage, its use can significantly reduce the stack memory
234
239
requirements for the code. Its contents on return are undefined.
235
240
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
+
236
247
##### Notes
237
248
238
249
` ORD_SORT ` implements a hybrid sorting algorithm combining
@@ -246,26 +257,12 @@ non-decreasing arrays. The optional `work` array replaces "scratch"
246
257
memory that would otherwise be allocated on the stack. If ` array ` is of
247
258
any type ` REAL ` the order of its elements on return undefined if any
248
259
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
250
261
function ` LGT ` .
251
262
252
263
253
264
##### Example
254
265
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
-
269
266
``` fortran
270
267
program demo_ord_sort
271
268
use stdlib_sorting, only: ord_sort
@@ -287,12 +284,12 @@ Experimental
287
284
288
285
##### Description
289
286
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.
292
289
293
290
##### Syntax
294
291
295
- ` call [[stdlib_sorting(module):sort(subroutine)]]sort ( array ) `
292
+ ` call [[stdlib_sorting(module):sort(subroutine)]]sort ( array, reverse ) `
296
293
297
294
##### Class
298
295
@@ -306,6 +303,13 @@ Pure generic subroutine.
306
303
` type(string_type) ` . It is an ` intent(inout) ` argument. On return its
307
304
input elements will be sorted in order of non-decreasing value.
308
305
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
+
309
313
##### Notes
310
314
311
315
` SORT ` implements a hybrid sorting algorithm combining
0 commit comments