ParaMonte Fortran 2.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
pm_arraySort::setSorted Interface Reference

Sort the input scalar string or contiguous vector in ascending order, or return the sorted indices of the input scalar string or contiguous array of rank 1 in ascending order or in the user-specified order. More...

Detailed Description

Sort the input scalar string or contiguous vector in ascending order, or return the sorted indices of the input scalar string or contiguous array of rank 1 in ascending order or in the user-specified order.

This generic interface either sorts the contents of an array or its indices in ascending order or via the user-specified custom input procedure isSorted().

The resulting output array or its index will be in ascending order (or in the requested order as specified by isSorted().
There are currently twelve sorting algorithms implemented in this generic interface.
Only the default sorting method can be used for sorting the indices of an array.
However, all sorting methods can be used to sort the elements of the array.
The default algorithm is generally the fastest sorting method.

Parameters
[in,out]array: The contiguous array of rank 1 of either
  1. type css_pdt (string container) or,
  2. type css_type (string container of default kind) or,
  3. type character of kind any supported by the processor (e.g., SK, SKA, SKD , or SKU) of arbitrary length type parameter or,
  4. type integer of kind any supported by the processor (e.g., IK, IK8, IK16, IK32, or IK64) or,
  5. type logical of kind any supported by the processor (e.g., LK) or,
  6. type complex of kind any supported by the processor (e.g., CK, CK32, CK64, or CK128) or,
  7. type real of kind any supported by the processor (e.g., RK, RK32, RK64, or RK128),
or,
  1. a scalar of type character of kind any supported by the processor (e.g., SK, SKA, SKD , or SKU) of arbitrary length type parameter.
On output,
  1. If the input argument index is present, then array has intent(in) and its contents remain intact upon return.
  2. If the input argument index is missing, then array has intent(inout) and its contents will be in ascending order upon return.
[out]index: The output contiguous array of rank 1 of type integer of kind any supported by the processor (e.g., IK, IK8, IK16, IK32, or IK64) of the same length as the input argument array containing the rank (i.e., sorted indices) of array such that array(index(:)) is sorted in ascending order on return.
The size of index must match that of array (or its length type parameter if array is a scalar string).
Read index(i) as the index of the element of array that contains the ith smallest (or ranked) value in the array.
This kind of ranking of values is widely known as ordinal ranking.
In ordinal ranking, all items receive distinct ordinal numbers, including items that compare equal.
The assignment of distinct ordinal numbers to items that compare equal can be done at random, or arbitrarily.
But it is generally preferable to use a system that is arbitrary but consistent, as this gives stable results if the ranking is done multiple times.
In computer data processing, ordinal ranking is also referred to as row numbering.
(optional. If missing, the contents of the input array will be sorted in ascending order and returned instead. It can be present only if the input argument method is missing.)
isSorted: The external user-specified function that takes two input scalar arguments of the same type and kind as the input array.
It returns a scalar logical of default kind LK that is .true. if the first input scalar argument is sorted with respect to the second input argument according to the user-defined condition within isSorted, otherwise, it is .false..
If array is a scalar string (i.e., an assumed-length scalar character), then both input arguments to isSorted() are scalar characters of length 1 of kind any supported by the processor (e.g., SK, SKA, SKD , or SKU).
The following illustrates the generic interface of isSorted(),
function isSorted(lhs, rhs) result(sorted)
use pm_kind, only: LK
TYPE(KIND) , intent(in) :: lhs, rhs
logical(LK) :: sorted
end function
This module defines the relevant Fortran kind type-parameters frequently used in the ParaMonte librar...
Definition: pm_kind.F90:268
integer, parameter LK
The default logical kind in the ParaMonte library: kind(.true.) in Fortran, kind(....
Definition: pm_kind.F90:541
where TYPE(KIND) is the same as the type and kind of the input argument array, which can be one of the following.
character(*, SK), intent(in) :: lhs, rhs
character(1, SK), intent(in) :: lhs, rhs
type(css_type) , intent(in) :: lhs, rhs
type(css_pdt) , intent(in) :: lhs, rhs
integer(IK) , intent(in) :: lhs, rhs
logical(LK) , intent(in) :: lhs, rhs
complex(CK) , intent(in) :: lhs, rhs
real(RK) , intent(in) :: lhs, rhs
This module contains the derived types for generating allocatable containers of scalar,...
This is the css_pdt parameterized type for generating instances of container of scalar of string obje...
This is the css_type type for generating instances of container of scalar of string objects.
where the specified kind type parameters (SK, IK, LK, CK, RK) can refer to any of the supported kinds by the processor.
This user-defined equivalence check is extremely useful where a user-defined sorting criterion other than simple ascending order is needed, for example, when the case-sensitivity of an input string or array of strings is irrelevant or when sorting of the absolute values matters excluding the signs of the numbers, or when descending order is desired.
In such cases, user can define a custom sorting condition within the user-defined external function isSorted to achieve the goal.
(optional, the default sorting condition is ascending order, that is a < b.)
method: The input scalar constant that can be any of the following:
  1. The constant qsorti or equivalently, an object of type qsorti_type, implying that the iterative version of the QuickSort sorting algorithm should be used.
    This method sorts the input array by a mixture of Quicksort and Selection sorting methods.
    When the size the array to be sorted reaches 30 or less, the algorithm switches from Quicksort to Selection sorting.
    This algorithm is typically of order \( N \log_2 N\), and the worst-case order of \(N^2\).
    The worst case performance occurs for completely sorted input arrays.
  2. The constant qsortr or equivalently, an object of type qsortr_type, implying that the recursive version of the QuickSort sorting algorithm should be used.
  3. The constant qsortrdp or equivalently, an object of type qsortrdp_type, implying that the recursive version of the Dual-Pivot QuickSort sorting algorithm should be used.
    The Dual-Pivot Quicksort algorithm can be slightly faster than the default Quicksort algorithms above.
    However, performance benchmarks indicate that the efficiency gain is marginal and insignificant.
    This algorithm is typically of order \( N \log_2 N\), and the worst-case order of \(N^2\). The worst case performance occurs for completely sorted input arrays.
  4. The constant bubble or equivalently, an object of type bubble_type, implying that the Bubble sorting algorithm should be used.
    This algorithm is of order \(N^2\).
  5. The constant heapi or equivalently, an object of type heapi_type, implying that the iterative version of the Heap sorting algorithm should be used.
    This algorithm is typically of order \( N \log_2 N\).
  6. The constant heapr or equivalently, an object of type heapr_type, implying that the recursive version of the Heap sorting algorithm should be used.
    This algorithm is typically of order \( N \log_2 N\).
  7. The constant insertionl or equivalently, an object of type insertionl_type, implying that the linear-search version of the insertion sorting algorithm should be used.
    The complexity of this algorithm is typically of order \(N^2\).
  8. The constant insertionb or equivalently, an object of type insertionb_type, implying that the binary-search version of the insertion sorting algorithm should be used.
    The complexity of this algorithm is typically of order \(N^2\).
  9. The constant merger or equivalently, an object of type merger_type, implying that the recursive version of the Merge sorting algorithm should be used.
    This algorithm is typically of order \( N \log_2 N\).
  10. The constant selection or equivalently, an object of type selection_type, implying that the Selection sorting algorithm should be used.
    This algorithm is of order \(N^2\).
  11. The constant shell or equivalently, an object of type shell_type, implying that the Shell sorting algorithm should be used.
    This algorithm is of order \(N\log(N)\).
The presence of this argument is merely for compile-time resolution of the procedures of this generic interface.
(optional. default = qsorti. It can be present only if the input argument index is missing.)


Possible calling interfaces

! Sorting the indices of an array.
call setSorted(array, index(:))
call setSorted(array, index(:), isSorted)
! Sorting the contents of an array.
call setSorted(array)
call setSorted(array, method)
call setSorted(array, isSorted)
call setSorted(array, isSorted, method)
Return .true. if the input array is sorted, either ascending or descending, or all equal.
Sort the input scalar string or contiguous vector in ascending order, or return the sorted indices of...
This module contains procedures and generic interfaces for various sorting tasks.
Warning
The sizes of the input array and index must be equal.
This condition is verified only if the library is built with the preprocessor macro CHECK_ENABLED=1.
Remarks
The procedures under discussion are pure. The procedures under this generic interface are always impure when the input argument isSorted() is present.
The procedures under discussion are recursive. The procedures are explicitly recursive only if the input argument methodis set to any of the recursive sorting algorithms.
Developer Remark:
The use of the Fortran intrinsic minloc in the sorting routines appears to lead a slightly better performance than the manual search.
Note
To rank the input array in descending order, simply call the output index elements from the end to the beginning or, rewrite the array with array = array( index(ubound(array, dim = 1) : lbound(array, dim = 1) : -1) ).
To sort the input array in descending order, simply call the output array elements from the end to the beginning or, rewrite the array with array = array(ubound(array, dim = 1) : lbound(array, dim = 1) : -1).
Alternatively, supply an external comparison function isSorted() with the appropriate comparison.
A rooter array can be sorted along with a leader array with the help of setSorted.
See also
getLoc
setLoc
getBin
getSorted
setSorted
isAscending
isDescending
isAscendingAll
isDescendingAll
isSorted
getRankDense
setRankDense
getRankOrdinal
setRankOrdinal
getRankModified
setRankModified
getRankStandard
setRankStandard
getRankFractional
setRankFractional
getSelected
setSelected


Example usage

1program example
2
3 use pm_kind, only: SK, IK, LK
4 use pm_arraySort, only: setSorted
5 use pm_distUnif, only: getUnifRand
8 use pm_io, only: display_type
9
10 implicit none
11
12 integer(IK), allocatable :: index(:)
13
14 type(display_type) :: disp
15 disp = display_type(file = "main.out.F90")
16
17 call disp%skip()
18 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
19 call disp%show("!Sort a string of characters of arbitrary kind in arbitrary orders.")
20 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
21 call disp%skip()
22
23 block
24 character(:), allocatable :: array
25 call disp%skip()
26 call disp%show("!Sort array in ascending order with case-sensitivity.")
27 call disp%show("array = 'ParaMonte'")
28 array = 'ParaMonte'
29 call disp%show("call setResized(index, len(array, IK))")
30 call setResized(index, len(array, IK))
31 call disp%show("call setSorted(array, index)")
32 call setSorted(array, index)
33 call disp%show("index")
34 call disp%show( index )
35 call disp%show("getRemapped(array, index)")
36 call disp%show( getRemapped(array, index) , deliml = """" )
37 call disp%show("call setSorted(array)")
38 call setSorted(array)
39 call disp%show("array")
40 call disp%show( array , deliml = """" )
41 call disp%skip()
42
43 call disp%skip()
44 call disp%show("!Sort array in ascending order without case-sensitivity via a custom-designed input comparison function.")
45 array = "ParaMonte"
46 call disp%skip()
47 call disp%show("array")
48 call disp%show( array , deliml = """" )
49 call disp%show("call setResized(index, len(array, IK))")
50 call setResized(index, len(array, IK))
51 call disp%show("call setSorted(array, index, isSortedChar)")
52 call setSorted(array, index, isSortedChar)
53 call disp%show("index")
54 call disp%show( index )
55 call disp%show("getRemapped(array, index)")
56 call disp%show( getRemapped(array, index) , deliml = """" )
57 call disp%show("call setSorted(array, isSortedChar)")
58 call setSorted(array, isSortedChar)
59 call disp%show("array")
60 call disp%show( array , deliml = """" )
61 call disp%skip()
62 end block
63
64 call disp%skip()
65 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
66 call disp%show("!Sort array of strings of the same length of arbitrary kind in ascending order.")
67 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
68 call disp%skip()
69
70 block
71 character(:), allocatable :: array(:)
72 array = [ "ParaMonte " &
73 , "V.2 " &
74 , "is " &
75 , "a " &
76 , "Parallel " &
77 , "Monte " &
78 , "Carlo " &
79 , "and " &
80 , "Machine " &
81 , "Learning " &
82 , "Library. " &
83 ]
84 call disp%skip()
85 call disp%show("array")
86 call disp%show( array , deliml = """" )
87 call disp%show("call setResized(index, size(array, 1, IK))")
88 call setResized(index, size(array, 1, IK))
89 call disp%show("call setSorted(array, index)")
90 call setSorted(array, index)
91 call disp%show("index")
92 call disp%show( index )
93 call disp%show("array(index)")
94 call disp%show( array(index) , deliml = """" )
95 call disp%show("call setSorted(array)")
96 call setSorted(array)
97 call disp%show("array")
98 call disp%show( array , deliml = """" )
99 call disp%skip()
100 end block
101
102 call disp%skip()
103 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
104 call disp%show("!Sort array of integer values of arbitrary kind in arbitrary orders.")
105 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
106 call disp%skip()
107
108 block
109 integer, allocatable :: array(:)
110 call disp%skip()
111 call disp%show("!Sort integer values in ascending order.")
112 call disp%show("array = getUnifRand(-9, 9, s1 = getUnifRand(5_IK, 10_IK))")
113 array = getUnifRand(-9, 9, s1 = getUnifRand(5_IK, 10_IK))
114 call disp%show("array")
115 call disp%show( array )
116 call disp%show("call setResized(index, size(array, 1, IK))")
117 call setResized(index, size(array, 1, IK))
118 call disp%show("call setSorted(array, index)")
119 call setSorted(array, index)
120 call disp%show("index")
121 call disp%show( index )
122 call disp%show("array(index)")
123 call disp%show( array(index) )
124 call disp%show("call setSorted(array)")
125 call setSorted(array)
126 call disp%show("array")
127 call disp%show( array )
128 call disp%skip()
129
130 call disp%skip()
131 call disp%show("!Sort integer values in descending order via a custom-designed input comparison function.")
132 call disp%skip()
133 call disp%show("array")
134 call disp%show( array )
135 call disp%show("call setResized(index, size(array, 1, IK))")
136 call setResized(index, size(array, 1, IK))
137 call disp%show("call setSorted(array, index, isSortedInteger)")
138 call setSorted(array, index, isSortedInteger)
139 call disp%show("index")
140 call disp%show( index )
141 call disp%show("array(index)")
142 call disp%show( array(index) )
143 call disp%show("call setSorted(array, isSortedInteger)")
144 call setSorted(array, isSortedInteger)
145 call disp%show("array")
146 call disp%show( array )
147 call disp%skip()
148 end block
149
150 call disp%skip()
151 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
152 call disp%show("!Sort array of logical values of arbitrary kind in arbitrary orders.")
153 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
154 call disp%skip()
155
156 block
157 logical, allocatable :: array(:)
158 call disp%skip()
159 call disp%show("!Sort logical values in ascending order.")
160 call disp%show("array = getUnifRand(.false., .true., s1 = getUnifRand(5_IK, 10_IK))")
161 array = getUnifRand(.false., .true., s1 = getUnifRand(5_IK, 10_IK))
162 call disp%show("array")
163 call disp%show( array )
164 call disp%show("call setResized(index, size(array, 1, IK))")
165 call setResized(index, size(array, 1, IK))
166 call disp%show("call setSorted(array, index)")
167 call setSorted(array, index)
168 call disp%show("index")
169 call disp%show( index )
170 call disp%show("array(index)")
171 call disp%show( array(index) )
172 call disp%show("call setSorted(array)")
173 call setSorted(array)
174 call disp%show("array")
175 call disp%show( array )
176 call disp%skip()
177
178 call disp%skip()
179 call disp%show("!Sort logical values in descending order via a custom-designed input comparison function.")
180 call disp%skip()
181 call disp%show("array")
182 call disp%show( array )
183 call disp%show("call setResized(index, size(array, 1, IK))")
184 call setResized(index, size(array, 1, IK))
185 call disp%show("call setSorted(array, index, isSortedLogical)")
186 call setSorted(array, index, isSortedLogical)
187 call disp%show("index")
188 call disp%show( index )
189 call disp%show("array(index)")
190 call disp%show( array(index) )
191 call disp%show("call setSorted(array, isSortedLogical)")
192 call setSorted(array, isSortedLogical)
193 call disp%show("array")
194 call disp%show( array )
195 call disp%skip()
196 end block
197
198 call disp%skip()
199 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
200 call disp%show("!Sort array of complex values of arbitrary kind in arbitrary orders.")
201 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
202 call disp%skip()
203
204 block
205 complex, allocatable :: array(:)
206 call disp%skip()
207 call disp%show("!Sort complex values in ascending order.")
208 call disp%show("array = getUnifRand((-9., -9), (9., 9.), s1 = getUnifRand(3_IK, 5_IK))")
209 array = getUnifRand((-9., -9), (9., 9.), s1 = getUnifRand(3_IK, 5_IK))
210 call disp%show("array")
211 call disp%show( array )
212 call disp%show("call setResized(index, size(array, 1, IK))")
213 call setResized(index, size(array, 1, IK))
214 call disp%show("call setSorted(array, index)")
215 call setSorted(array, index)
216 call disp%show("index")
217 call disp%show( index )
218 call disp%show("array(index)")
219 call disp%show( array(index) )
220 call disp%show("call setSorted(array)")
221 call setSorted(array)
222 call disp%show("array")
223 call disp%show( array )
224 call disp%skip()
225
226 call disp%skip()
227 call disp%show("!Sort complex values in ascending order of their modulus via a custom-designed input comparison function.")
228 call disp%skip()
229 call disp%show("array")
230 call disp%show( array )
231 call disp%show("call setResized(index, size(array, 1, IK))")
232 call setResized(index, size(array, 1, IK))
233 call disp%show("call setSorted(array, index, isSortedComplex)")
234 call setSorted(array, index, isSortedComplex)
235 call disp%show("index")
236 call disp%show( index )
237 call disp%show("array(index)")
238 call disp%show( array(index) )
239 call disp%show("call setSorted(array, isSortedComplex)")
240 call setSorted(array, isSortedComplex)
241 call disp%show("array")
242 call disp%show( array )
243 call disp%skip()
244 end block
245
246 call disp%skip()
247 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
248 call disp%show("!Sort array of real values of arbitrary kind in arbitrary orders.")
249 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
250 call disp%skip()
251
252 block
253 real, allocatable :: array(:)
254 call disp%skip()
255 call disp%show("!Sort real values in ascending order.")
256 call disp%show("array = getUnifRand(-9., 9., s1 = getUnifRand(4_IK, 8_IK))")
257 array = getUnifRand(-9., 9., s1 = getUnifRand(4_IK, 8_IK))
258 call disp%show("array")
259 call disp%show( array )
260 call disp%show("call setResized(index, size(array, 1, IK))")
261 call setResized(index, size(array, 1, IK))
262 call disp%show("call setSorted(array, index)")
263 call setSorted(array, index)
264 call disp%show("index")
265 call disp%show( index )
266 call disp%show("array(index)")
267 call disp%show( array(index) )
268 call disp%show("call setSorted(array)")
269 call setSorted(array)
270 call disp%show("array")
271 call disp%show( array )
272 call disp%skip()
273
274 call disp%skip()
275 call disp%show("!Sort real values in ascending order of their modulus via a custom-designed input comparison function.")
276 call disp%skip()
277 call disp%show("array")
278 call disp%show( array )
279 call disp%show("call setResized(index, size(array, 1, IK))")
280 call setResized(index, size(array, 1, IK))
281 call disp%show("call setSorted(array, index, isSortedReal)")
282 call setSorted(array, index, isSortedReal)
283 call disp%show("index")
284 call disp%show( index )
285 call disp%show("array(index)")
286 call disp%show( array(index) )
287 call disp%show("call setSorted(array, isSortedReal)")
288 call setSorted(array, isSortedReal)
289 call disp%show("array")
290 call disp%show( array )
291 call disp%skip()
292 end block
293
294#if PDT_ENABLED
295 call disp%skip()
296 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
297 call disp%show("!Sort array of string containers in ascending order.")
298 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
299 call disp%skip()
300
301 block
302 use pm_container, only: css_pdt ! \bug Intel ifort 2021.6 cannot handle PDT aliases such as `strc`.
303 type(css_pdt), allocatable :: array(:)
304 array = [ css_pdt("ParaMonte") &
305 , css_pdt("V.2") &
306 , css_pdt("is") &
307 , css_pdt("a") &
308 , css_pdt("Parallel") &
309 , css_pdt("Monte") &
310 , css_pdt("Carlo") &
311 , css_pdt("and") &
312 , css_pdt("Machine") &
313 , css_pdt("Learning") &
314 , css_pdt("Library.") &
315 ]
316 call disp%skip()
317 call disp%show("array")
318 call disp%show( array , deliml = """" )
319 call disp%show("call setResized(index, size(array, 1, IK))")
320 call setResized(index, size(array, 1, IK))
321 call disp%show("call setSorted(array, index)")
322 call setSorted(array, index)
323 call disp%show("index")
324 call disp%show( index )
325 call disp%show("getRemapped(array, index)")
326 call disp%show( getRemapped(array, index) , deliml = """" )
327 call disp%show("call setSorted(array)")
328 call setSorted(array)
329 call disp%show("array")
330 call disp%show( array , deliml = """" )
331 call disp%skip()
332 end block
333#endif
334
335contains
336
337 function isSortedInteger(a,b) result(sorted)
338 use pm_kind, only: LK
339 integer , intent(in) :: a, b
340 logical(LK) :: sorted
341 sorted = a > b
342 end function
343
344 function isSortedLogical(a,b) result(sorted)
345 use pm_logicalCompare, only: operator(>)
346 use pm_kind, only: LK
347 logical , intent(in) :: a, b
348 logical(LK) :: sorted
349 sorted = a > b
350 end function
351
352 function isSortedComplex(a,b) result(sorted)
353 use pm_kind, only: LK
354 complex , intent(in) :: a, b
355 logical(LK) :: sorted
356 sorted = abs(a) < abs(b)
357 end function
358
359 function isSortedReal(a,b) result(sorted)
360 use pm_kind, only: LK
361 real , intent(in) :: a, b
362 logical(LK) :: sorted
363 sorted = abs(a) < abs(b)
364 end function
365
366 function isSortedChar(a,b) result(sorted)
367 use pm_strASCII, only: getStrLower
368 use pm_kind, only: LK
369 character(1) , intent(in) :: a, b
370 logical(LK) :: sorted
371 sorted = getStrLower(a) < getStrLower(b)
372 end function
373
374end program example
Generate a copy of the input array whose elements are reordered according to the input index array su...
Allocate or resize (shrink or expand) an input allocatable scalar string or array of rank 1....
Generate and return a scalar or a contiguous array of rank 1 of length s1 of randomly uniformly distr...
This is a generic method of the derived type display_type with pass attribute.
Definition: pm_io.F90:11726
This is a generic method of the derived type display_type with pass attribute.
Definition: pm_io.F90:11508
Generate and return the input string where the uppercase English alphabets are all converted to lower...
This module contains procedures and generic interfaces for remapping arrays of various types.
This module contains procedures and generic interfaces for resizing allocatable arrays of various typ...
This module contains classes and procedures for computing various statistical quantities related to t...
This module contains classes and procedures for input/output (IO) or generic display operations on st...
Definition: pm_io.F90:252
type(display_type) disp
This is a scalar module variable an object of type display_type for general display.
Definition: pm_io.F90:11393
integer, parameter IK
The default integer kind in the ParaMonte library: int32 in Fortran, c_int32_t in C-Fortran Interoper...
Definition: pm_kind.F90:540
integer, parameter SK
The default character kind in the ParaMonte library: kind("a") in Fortran, c_char in C-Fortran Intero...
Definition: pm_kind.F90:539
This module contains procedures and generic interfaces for performing a variety of logical comparison...
This module contains the uncommon and hardly representable ASCII characters as well as procedures for...
Definition: pm_strASCII.F90:61
Generate and return an object of type display_type.
Definition: pm_io.F90:10282

Example Unix compile command via Intel ifort compiler
1#!/usr/bin/env sh
2rm main.exe
3ifort -fpp -standard-semantics -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Example Windows Batch compile command via Intel ifort compiler
1del main.exe
2set PATH=..\..\..\lib;%PATH%
3ifort /fpp /standard-semantics /O3 /I:..\..\..\include main.F90 ..\..\..\lib\libparamonte*.lib /exe:main.exe
4main.exe

Example Unix / MinGW compile command via GNU gfortran compiler
1#!/usr/bin/env sh
2rm main.exe
3gfortran -cpp -ffree-line-length-none -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Example output
1
2!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3!Sort a string of characters of arbitrary kind in arbitrary orders.
4!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5
6
7!Sort array in ascending order with case-sensitivity.
8array = 'ParaMonte'
9call setResized(index, len(array, IK))
10call setSorted(array, index)
11index
12+5, +1, +2, +4, +9, +7, +6, +3, +8
13getRemapped(array, index)
14"MPaaenort"
15call setSorted(array)
16array
17"MPaaenort"
18
19
20!Sort array in ascending order without case-sensitivity via a custom-designed input comparison function.
21
22array
23"ParaMonte"
24call setResized(index, len(array, IK))
25call setSorted(array, index, isSortedChar)
26index
27+2, +4, +9, +5, +7, +6, +1, +3, +8
28getRemapped(array, index)
29"aaeMnoPrt"
30call setSorted(array, isSortedChar)
31array
32"aaeMnoPrt"
33
34
35!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36!Sort array of strings of the same length of arbitrary kind in ascending order.
37!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38
39
40array
41"ParaMonte ", "V.2 ", "is ", "a ", "Parallel ", "Monte ", "Carlo ", "and ", "Machine ", "Learning ", "Library. "
42call setResized(index, size(array, 1, IK))
43call setSorted(array, index)
44index
45+7, +10, +11, +9, +6, +1, +5, +2, +4, +8, +3
46array(index)
47"Carlo ", "Learning ", "Library. ", "Machine ", "Monte ", "ParaMonte ", "Parallel ", "V.2 ", "a ", "and ", "is "
48call setSorted(array)
49array
50"Carlo ", "Learning ", "Library. ", "Machine ", "Monte ", "ParaMonte ", "Parallel ", "V.2 ", "a ", "and ", "is "
51
52
53!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54!Sort array of integer values of arbitrary kind in arbitrary orders.
55!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56
57
58!Sort integer values in ascending order.
59array = getUnifRand(-9, 9, s1 = getUnifRand(5_IK, 10_IK))
60array
61+6, -3, -9, -2, +3
62call setResized(index, size(array, 1, IK))
63call setSorted(array, index)
64index
65+3, +2, +4, +5, +1
66array(index)
67-9, -3, -2, +3, +6
68call setSorted(array)
69array
70-9, -3, -2, +3, +6
71
72
73!Sort integer values in descending order via a custom-designed input comparison function.
74
75array
76-9, -3, -2, +3, +6
77call setResized(index, size(array, 1, IK))
78call setSorted(array, index, isSortedInteger)
79index
80+5, +4, +3, +2, +1
81array(index)
82+6, +3, -2, -3, -9
83call setSorted(array, isSortedInteger)
84array
85+6, +3, -2, -3, -9
86
87
88!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89!Sort array of logical values of arbitrary kind in arbitrary orders.
90!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91
92
93!Sort logical values in ascending order.
94array = getUnifRand(.false., .true., s1 = getUnifRand(5_IK, 10_IK))
95array
96T, F, F, F, F, F, T, F
97call setResized(index, size(array, 1, IK))
98call setSorted(array, index)
99index
100+2, +3, +4, +5, +6, +8, +1, +7
101array(index)
102F, F, F, F, F, F, T, T
103call setSorted(array)
104array
105F, F, F, F, F, F, T, T
106
107
108!Sort logical values in descending order via a custom-designed input comparison function.
109
110array
111F, F, F, F, F, F, T, T
112call setResized(index, size(array, 1, IK))
113call setSorted(array, index, isSortedLogical)
114index
115+7, +8, +1, +2, +3, +4, +5, +6
116array(index)
117T, T, F, F, F, F, F, F
118call setSorted(array, isSortedLogical)
119array
120T, T, F, F, F, F, F, F
121
122
123!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
124!Sort array of complex values of arbitrary kind in arbitrary orders.
125!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
126
127
128!Sort complex values in ascending order.
129array = getUnifRand((-9., -9), (9., 9.), s1 = getUnifRand(3_IK, 5_IK))
130array
131(-4.34800339, -5.56972313), (+1.20127106, -6.77455425), (-3.60797691, +5.41838264), (+4.99476147, -5.31792641), (-2.37871575, +1.76057220)
132call setResized(index, size(array, 1, IK))
133call setSorted(array, index)
134index
135+1, +3, +5, +2, +4
136array(index)
137(-4.34800339, -5.56972313), (-3.60797691, +5.41838264), (-2.37871575, +1.76057220), (+1.20127106, -6.77455425), (+4.99476147, -5.31792641)
138call setSorted(array)
139array
140(-4.34800339, -5.56972313), (-3.60797691, +5.41838264), (-2.37871575, +1.76057220), (+1.20127106, -6.77455425), (+4.99476147, -5.31792641)
141
142
143!Sort complex values in ascending order of their modulus via a custom-designed input comparison function.
144
145array
146(-4.34800339, -5.56972313), (-3.60797691, +5.41838264), (-2.37871575, +1.76057220), (+1.20127106, -6.77455425), (+4.99476147, -5.31792641)
147call setResized(index, size(array, 1, IK))
148call setSorted(array, index, isSortedComplex)
149index
150+3, +2, +4, +1, +5
151array(index)
152(-2.37871575, +1.76057220), (-3.60797691, +5.41838264), (+1.20127106, -6.77455425), (-4.34800339, -5.56972313), (+4.99476147, -5.31792641)
153call setSorted(array, isSortedComplex)
154array
155(-2.37871575, +1.76057220), (-3.60797691, +5.41838264), (+1.20127106, -6.77455425), (-4.34800339, -5.56972313), (+4.99476147, -5.31792641)
156
157
158!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159!Sort array of real values of arbitrary kind in arbitrary orders.
160!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
161
162
163!Sort real values in ascending order.
164array = getUnifRand(-9., 9., s1 = getUnifRand(4_IK, 8_IK))
165array
166-4.23643875, +0.851614952, +5.68609047, -5.17461300, -8.61941719
167call setResized(index, size(array, 1, IK))
168call setSorted(array, index)
169index
170+5, +4, +1, +2, +3
171array(index)
172-8.61941719, -5.17461300, -4.23643875, +0.851614952, +5.68609047
173call setSorted(array)
174array
175-8.61941719, -5.17461300, -4.23643875, +0.851614952, +5.68609047
176
177
178!Sort real values in ascending order of their modulus via a custom-designed input comparison function.
179
180array
181-8.61941719, -5.17461300, -4.23643875, +0.851614952, +5.68609047
182call setResized(index, size(array, 1, IK))
183call setSorted(array, index, isSortedReal)
184index
185+4, +3, +2, +5, +1
186array(index)
187+0.851614952, -4.23643875, -5.17461300, +5.68609047, -8.61941719
188call setSorted(array, isSortedReal)
189array
190+0.851614952, -4.23643875, -5.17461300, +5.68609047, -8.61941719
191
192
Test:
test_pm_arraySort
Bug:

Status: Unresolved
Source: Intel Classic Fortran Compiler ifort version 2021.5
Description: See pm_arraySplit for the description of a relevant bug in PDT name aliasing when compiled with Intel Classic Fortran Compiler ifort version 2021.5 that also applies to this module.
Remedy (as of ParaMonte Library version 2.0.0): See pm_arraySplit for the remedy.
Bug:

Status: Unresolved
Source: GNU Fortran Compiler gfortran version 10.3
Description: The GNU Fortran Compiler gfortran version 10.3 cannot not compile the allocation of PDT string container object temp within the implementation of the corresponding procedures.
99 | deallocate(Temp)
| ^
internal compiler error: in gimplify_var_or_parm_decl, at gimplify.c:2834
Please submit a full bug report...


Remedy (as of ParaMonte Library version 2.0.0): For now, all allocatable Temp objects are converted to automatic arrays.

Todo:
Normal Priority: Low Priority: The current bypass for the PDT name aliasing bug should be reverted back to PDT name aliasing once the ifort bug is resolved.


Final Remarks


If you believe this algorithm or its documentation can be improved, we appreciate your contribution and help to edit this page's documentation and source file on GitHub.
For details on the naming abbreviations, see this page.
For details on the naming conventions, see this page.
This software is distributed under the MIT license with additional terms outlined below.

  1. If you use any parts or concepts from this library to any extent, please acknowledge the usage by citing the relevant publications of the ParaMonte library.
  2. If you regenerate any parts/ideas from this library in a programming environment other than those currently supported by this ParaMonte library (i.e., other than C, C++, Fortran, MATLAB, Python, R), please also ask the end users to cite this original ParaMonte library.

This software is available to the public under a highly permissive license.
Help us justify its continued development and maintenance by acknowledging its benefit to society, distributing it, and contributing to it.

Author:
Amir Shahmoradi, April 21, 2017, 1:54 AM, Institute for Computational Engineering and Sciences (ICES), The University of Texas Austin

Definition at line 5540 of file pm_arraySort.F90.


The documentation for this interface was generated from the following file: