Skip to content

Commit 4280f3e

Browse files
committed
Fixed documentation problems noted by gareth_nx
Changed the markdown document docs/specs/stdlib_sorting.md to correct problems noted by gareth_nx. Changed comments in the FYPP file src/stlib_sorting_sort_index.fypp to note that the input array is sorted on output. [ticket: X]
1 parent b2a2149 commit 4280f3e

File tree

2 files changed

+40
-34
lines changed

2 files changed

+40
-34
lines changed

doc/specs/stdlib_sorting.md

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,14 @@ housekeeping it has slower runtime performance than `ORD_SORT`.
152152
provided as optional `work` and `iwork` arguments or allocated
153153
internally on the stack.
154154

155-
#### The `SORT` subroutines
155+
#### The `SORT` subroutine
156156

157157
`SORT` uses the `introsort` sorting algorithm of David Musser.
158158
`introsort` is a hybrid unstable comparison algorithm combining
159159
`quicksort`, `insertion sort`, and `heap sort`. While this algorithm's
160160
runtime performance is always O(N Ln(N)), it is relatively fast on
161-
randomly ordered data, but inconsistent in performance on partly
162-
sorted data.as the official source of the algorithm.
161+
randomly ordered data, but does not show the improvement in
162+
performance on partly sorted data found for `ORD_SORT`.
163163

164164
As with `introsort`, `SORT` is an unstable hybrid algorithm.
165165
First it examines the array and estimates the depth of recursion a
@@ -182,14 +182,16 @@ calls `introsort` proper. `introsort` proper then:
182182
* Calls `introsort` proper on the rightmost partition, and then
183183
returns.
184184

185-
The resulting algorithm is of order O(N Ln(N)) run time
186-
performance for all inputs. Because it relies on `quicksort`, the
187-
coefficient of the O(N Ln(N)) behavior is typically small compared to
188-
other sorting algorithms on random data. On partially sorted data it
189-
can show either slower `heap sort` performance, or enhanced
190-
performance by up to a factor of six. Still, even when it shows
191-
enhanced performance, its performance on partially sorted data is
192-
typically an order of magnitude slower than `ORD_SORT`.
185+
The resulting algorithm is of order O(N Ln(N)) run time performance
186+
for all inputs. Because it relies on `quicksort`, the coefficient of
187+
the O(N Ln(N)) behavior is typically small compared to other sorting
188+
algorithms on random data. On partially sorted data it can show either
189+
slower `heap sort` performance, or enhanced performance by up to a
190+
factor of six. Still, even when it shows enhanced performance, its
191+
performance on partially sorted data is typically an order of
192+
magnitude slower than `ORD_SORT`. Its memory requirements are also
193+
low, being of order O(Ln(N)), while the memory requirements of
194+
`ORD_SORT` and `SORT_INDEX` are of order O(N).
193195

194196
### Tentative specifications of the `stdlib_sorting` procedures
195197

@@ -255,9 +257,7 @@ function `LGT`.
255257
call read_sorted_file( 'dummy_file1', array1 )
256258
call read_sorted_file( 'dummy_file2', array2 )
257259
! Concatenate the arrays
258-
allocate( array( size(array1) + size(array2) ) )
259-
array( 1:size(array1) ) = array1(:)
260-
array( size(array1)+1:size(array1)+size(array2) ) = array2(:)
260+
array = [ array1, array2 ]
261261
! Sort the resulting array
262262
call ord_sort( array, work )
263263
! Process the sorted array
@@ -318,16 +318,17 @@ element of `array` is a `NaN`. Sorting of `CHARACTER(*)` and
318318
...
319319
```
320320

321-
#### `sort_index` - creates an array of sorting indices for an input array.
321+
#### `sort_index` - creates an array of sorting indices for an input array, while also sorting the array.
322322

323323
##### Status
324324

325325
Experimental
326326

327327
##### Description
328328

329-
Returns an integer array whose elements would sort the input array in
330-
the specified direction retaining order stability.
329+
Returns the input `array` sorted in the direction requested while
330+
retaining order stability, and an integer array whose elements would
331+
sort the input `array` to produce the output `array`.
331332

332333
##### Syntax
333334

@@ -381,9 +382,10 @@ replace "scratch" memory that would otherwise be allocated on the
381382
stack. If `array` is of any kind of `REAL` the order of the elements in
382383
`index` and `array` on return are undefined if any element of `array`
383384
is a `NaN`. Sorting of `CHARACTER(*)` and `STRING_TYPE` arrays are
384-
based on the operator `>`, and not on the function `LGT`. It should be
385-
emphasized that the order of `array` will typically be different on
386-
return.
385+
based on the operator `>`, and not on the function `LGT`.
386+
387+
It should be emphasized that the order of `array` will typically be
388+
different on return
387389

388390

389391
##### Examples
@@ -392,15 +394,15 @@ Sorting a related rank one array:
392394

393395
```Fortran
394396
subroutine sort_related_data( a, b, work, index, iwork )
395-
! Sort `b` in terms or its related array `a`
397+
! Sort `a`, and also sort `b` to be reorderd the same way as `a`
396398
integer, intent(inout) :: a(:)
397399
integer(int32), intent(inout) :: b(:) ! The same size as a
398400
integer(int32), intent(inout) :: work(:)
399401
integer(int_size), intent(inout) :: index(:)
400402
integer(int_size), intent(inout) :: iwork(:)
401403
! Find the indices to sort a
402-
call sort_index(a, index(1:size(a)),&
403-
work(1:size(a)/2), iwork(1:size(a)/2))
404+
call sort_index(a, index(1:size(a)),&
405+
work(1:size(a)/2), iwork(1:size(a)/2))
404406
! Sort b based on the sorting of a
405407
b(:) = b( index(1:size(a)) )
406408
end subroutine sort_related_data
@@ -410,23 +412,23 @@ Sorting a rank 2 array based on the data in a column
410412

411413
```Fortran
412414
subroutine sort_related_data( array, column, work, index, iwork )
413-
! Sort `a_data` in terms or its component `a`
414-
integer, intent(inout) :: a(:,:)
415+
! Reorder rows of `array` such that `array(:, column)` is sorted
416+
integer, intent(inout) :: array(:,:)
415417
integer(int32), intent(in) :: column
416418
integer(int32), intent(inout) :: work(:)
417419
integer(int_size), intent(inout) :: index(:)
418420
integer(int_size), intent(inout) :: iwork(:)
419421
integer, allocatable :: dummy(:)
420422
integer :: i
421-
allocate(dummy(size(a, dim=1)))
422-
! Extract a component of `a_data`
423-
dummy(:) = a(:, column)
423+
allocate(dummy(size(array, dim=1)))
424+
! Extract a column of `array`
425+
dummy(:) = array(:, column)
424426
! Find the indices to sort the column
425427
call sort_index(dummy, index(1:size(dummy)),&
426428
work(1:size(dummy)/2), iwork(1:size(dummy)/2))
427429
! Sort a based on the sorting of its column
428-
do i=1, size(a, dim=2)
429-
a(:, i) = a(index(1:size(a, dim=1)), i)
430+
do i=1, size(array, dim=2)
431+
array(:, i) = array(index(1:size(array, dim=1)), i)
430432
end do
431433
end subroutine sort_related_data
432434
```

src/stdlib_sorting_sort_index.fypp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ contains
6363

6464
module subroutine ${k1}$_sort_index( array, index, work, iwork, reverse )
6565
! A modification of `${k1}$_ord_sort` to return an array of indices that
66-
! would provide a stable sort of the `ARRAY` input. The indices by default
66+
! would perform a stable sort of the `ARRAY` as input, and also sort `ARRAY`
67+
! as desired. The indices by default
6768
! correspond to a non-decreasing sort, but if the optional argument
6869
! `REVERSE` is present with a value of `.TRUE.` the indices correspond to
6970
! a non-increasing sort. The logic of the determination of indexing largely
@@ -471,7 +472,8 @@ contains
471472

472473
module subroutine ${k1}$_sort_index( array, index, work, iwork, reverse )
473474
! A modification of `${k1}$_ord_sort` to return an array of indices that
474-
! would provide a stable sort of the `ARRAY` input. The indices by default
475+
! would perform a stable sort of the `ARRAY` as input, and also sort `ARRAY`
476+
! as desired. The indices by default
475477
! correspond to a non-decreasing sort, but if the optional argument
476478
! `REVERSE` is present with a value of `.TRUE.` the indices correspond to
477479
! a non-increasing sort. The logic of the determination of indexing largely
@@ -876,7 +878,8 @@ contains
876878

877879
module subroutine char_sort_index( array, index, work, iwork, reverse )
878880
! A modification of `char_ord_sort` to return an array of indices that
879-
! would provide a stable sort of the `ARRAY` input. The indices by default
881+
! would perform a stable sort of the `ARRAY` as input, and also sort `ARRAY`
882+
! as desired. The indices by default
880883
! correspond to a non-decreasing sort, but if the optional argument
881884
! `REVERSE` is present with a value of `.TRUE.` the indices correspond to
882885
! a non-increasing sort. The logic of the determination of indexing largely
@@ -1280,7 +1283,8 @@ contains
12801283

12811284
module subroutine string_sort_index( array, index, work, iwork, reverse )
12821285
! A modification of `string_ord_sort` to return an array of indices that
1283-
! would provide a stable sort of the `ARRAY` input. The indices by default
1286+
! would perform a stable sort of the `ARRAY` as input, and also sort `ARRAY`
1287+
! as desired. The indices by default
12841288
! correspond to a non-decreasing sort, but if the optional argument
12851289
! `REVERSE` is present with a value of `.TRUE.` the indices correspond to
12861290
! a non-increasing sort. The logic of the determination of indexing largely

0 commit comments

Comments
 (0)