@@ -4,7 +4,7 @@ title: Sorting Procedures
4
4
5
5
# The ` stdlib_sorting ` module
6
6
7
- ( TOC)
7
+ [ TOC]
8
8
9
9
## Overview of sorting
10
10
@@ -24,7 +24,7 @@ module's `string_type` type.
24
24
25
25
## Overview of the module
26
26
27
- The module ` stdlib_sorting ` defines several public entities one
27
+ The module ` stdlib_sorting ` defines several public entities, one
28
28
default integer parameter, ` int_size ` , and three overloaded
29
29
subroutines: ` ORD_SORT ` , ` SORT ` , and ` SORT_INDEX ` . The
30
30
overloaded subroutines also each have seven specific names for
44
44
* ` ORD_SORT ` is intended to sort simple arrays of intrinsic data
45
45
that have significant sections that were partially ordered before
46
46
the sort;
47
- * ` SORT_INDEX ` is based on ORD_SORT, but in addition to sorting the
47
+ * ` SORT_INDEX ` is based on ` ORD_SORT ` , but in addition to sorting the
48
48
input array, it returns indices that map the original array to its
49
49
sorted version. This enables related arrays to be re-ordered in the
50
50
same way; and
@@ -111,13 +111,13 @@ performance on uniformly increasing or decreasing data.
111
111
` ORD_SORT ` begins by traversing the array starting in its tail
112
112
attempting to identify ` runs ` in the array, where a run is either a
113
113
uniformly decreasing sequence, ` ARRAY(i-1) > ARRAY(i) ` , or a
114
- non-decreasing, ` ARRAY(i-1) <= ARRAY(i) ` , sequence. Once delimitated
114
+ non-decreasing, ` ARRAY(i-1) <= ARRAY(i) ` , sequence. First delimitated
115
115
decreasing sequences are reversed in their order. Then, if the
116
116
sequence has less than ` MIN_RUN ` elements, previous elements in the
117
117
array are added to the run using ` insertion sort ` until the run
118
118
contains ` MIN_RUN ` elements or the array is completely processed. As
119
119
each run is identified the start and length of the run
120
- is then pushed onto a stack and the stack is then processed using
120
+ are then pushed onto a stack and the stack is then processed using
121
121
` merge ` until it obeys the stack invariants:
122
122
123
123
1 . len(i-2) > len(i-1) + len(i)
@@ -163,7 +163,6 @@ runtime performance is always O(N Ln(N)), it is relatively fast on
163
163
randomly ordered data, but does not show the improvement in
164
164
performance on partly sorted data found for ` ORD_SORT ` .
165
165
166
- As with ` introsort ` , ` SORT ` is an unstable hybrid algorithm.
167
166
First it examines the array and estimates the depth of recursion a
168
167
quick sort would require for ideal (random) data, `D =
169
168
Ceiling(Ln(N)/Ln(2))`. It then defines a limit to the number of
@@ -195,7 +194,7 @@ magnitude slower than `ORD_SORT`. Its memory requirements are also
195
194
low, being of order O(Ln(N)), while the memory requirements of
196
195
` ORD_SORT ` and ` SORT_INDEX ` are of order O(N).
197
196
198
- ### Tentative specifications of the ` stdlib_sorting ` procedures
197
+ ### Specifications of the ` stdlib_sorting ` procedures
199
198
200
199
#### ` ord_sort ` - sorts an input array
201
200
@@ -267,6 +266,19 @@ function `LGT`.
267
266
...
268
267
```
269
268
269
+ ``` fortran
270
+ program demo_ord_sort
271
+ use stdlib_sorting, only: ord_sort
272
+ implicit none
273
+ integer, allocatable :: array1(:), work(:)
274
+
275
+ array1 = [ 5, 4, 3, 1, 10, 4, 9]
276
+ allocate(work, mold = array1)
277
+ call ord_sort(array1, work)
278
+ print*, array1 !print [1, 3, 4, 4, 5, 9, 10]
279
+ end program demo_ord_sort
280
+ ```
281
+
270
282
#### ` sort ` - sorts an input array
271
283
272
284
##### Status
@@ -320,6 +332,18 @@ element of `array` is a `NaN`. Sorting of `CHARACTER(*)` and
320
332
...
321
333
```
322
334
335
+ ``` fortran
336
+ program demo_sort
337
+ use stdlib_sorting, only: sort
338
+ implicit none
339
+ integer, allocatable :: array(:)
340
+
341
+ array = [ 5, 4, 3, 1, 10, 4, 9]
342
+ call sort(array)
343
+ print*, array !print [1, 3, 4, 4, 5, 9, 10]
344
+ end program demo_sort
345
+ ```
346
+
323
347
#### ` sort_index ` - creates an array of sorting indices for an input array, while also sorting the array.
324
348
325
349
##### Status
0 commit comments