@@ -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
2828default integer parameter, ` int_size ` , and three overloaded
2929subroutines: ` ORD_SORT ` , ` SORT ` , and ` SORT_INDEX ` . The
3030overloaded subroutines also each have seven specific names for
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
112112attempting to identify ` runs ` in the array, where a run is either a
113113uniformly 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
115115decreasing sequences are reversed in their order. Then, if the
116116sequence has less than ` MIN_RUN ` elements, previous elements in the
117117array are added to the run using ` insertion sort ` until the run
118118contains ` MIN_RUN ` elements or the array is completely processed. As
119119each 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
1231231 . 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
163163randomly ordered data, but does not show the improvement in
164164performance on partly sorted data found for ` ORD_SORT ` .
165165
166- As with ` introsort ` , ` SORT ` is an unstable hybrid algorithm.
167166First it examines the array and estimates the depth of recursion a
168167quick sort would require for ideal (random) data, `D =
169168Ceiling(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
195194low, 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