Skip to content

Commit 8b9be81

Browse files
committed
Commited suggestions by jvdp1
Made various corrections in the document stdlib_sorting.md and the code stdlib_sorting.fypp. [ticket: X]
1 parent b45441b commit 8b9be81

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

doc/specs/stdlib_sorting.md

+31-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Sorting Procedures
44

55
# The `stdlib_sorting` module
66

7-
(TOC)
7+
[TOC]
88

99
## Overview of sorting
1010

@@ -24,7 +24,7 @@ module's `string_type` type.
2424

2525
## Overview of the module
2626

27-
The module `stdlib_sorting` defines several public entities one
27+
The module `stdlib_sorting` defines several public entities, one
2828
default integer parameter, `int_size`, and three overloaded
2929
subroutines: `ORD_SORT`, `SORT`, and `SORT_INDEX`. The
3030
overloaded subroutines also each have seven specific names for
@@ -44,7 +44,7 @@ data:
4444
* `ORD_SORT` is intended to sort simple arrays of intrinsic data
4545
that have significant sections that were partially ordered before
4646
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
4848
input array, it returns indices that map the original array to its
4949
sorted version. This enables related arrays to be re-ordered in the
5050
same way; and
@@ -111,13 +111,13 @@ performance on uniformly increasing or decreasing data.
111111
`ORD_SORT` begins by traversing the array starting in its tail
112112
attempting to identify `runs` in the array, where a run is either a
113113
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
115115
decreasing sequences are reversed in their order. Then, if the
116116
sequence has less than `MIN_RUN` elements, previous elements in the
117117
array are added to the run using `insertion sort` until the run
118118
contains `MIN_RUN` elements or the array is completely processed. As
119119
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
121121
`merge` until it obeys the stack invariants:
122122

123123
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
163163
randomly ordered data, but does not show the improvement in
164164
performance on partly sorted data found for `ORD_SORT`.
165165

166-
As with `introsort`, `SORT` is an unstable hybrid algorithm.
167166
First it examines the array and estimates the depth of recursion a
168167
quick sort would require for ideal (random) data, `D =
169168
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
195194
low, being of order O(Ln(N)), while the memory requirements of
196195
`ORD_SORT` and `SORT_INDEX` are of order O(N).
197196

198-
### Tentative specifications of the `stdlib_sorting` procedures
197+
### Specifications of the `stdlib_sorting` procedures
199198

200199
#### `ord_sort` - sorts an input array
201200

@@ -267,6 +266,19 @@ function `LGT`.
267266
...
268267
```
269268

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+
270282
#### `sort` - sorts an input array
271283

272284
##### Status
@@ -320,6 +332,18 @@ element of `array` is a `NaN`. Sorting of `CHARACTER(*)` and
320332
...
321333
```
322334

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+
323347
#### `sort_index` - creates an array of sorting indices for an input array, while also sorting the array.
324348

325349
##### Status

0 commit comments

Comments
 (0)