rgsl/sort.rs
1//
2// A rust binding for the GSL library by Guillaume Gomez (guillaume1.gomez@gmail.com)
3//
4
5/*!
6# Sorting
7
8This chapter describes functions for sorting data, both directly and indirectly (using an index). All the functions use the heapsort algorithm.
9Heapsort is an O(N \log N) algorithm which operates in-place and does not require any additional storage. It also provides consistent performance,
10the running time for its worst-case (ordered data) being not significantly longer than the average and best cases. Note that the heapsort algorithm
11does not preserve the relative ordering of equal elements—it is an unstable sort. However the resulting order of equal elements will be consistent
12across different platforms when using these functions.
13
14## References and Further Reading
15
16The subject of sorting is covered extensively in Knuth’s Sorting and Searching,
17
18Donald E. Knuth, The Art of Computer Programming: Sorting and Searching (Vol 3, 3rd Ed, 1997), Addison-Wesley, ISBN 0201896850.
19The Heapsort algorithm is described in the following book,
20
21Robert Sedgewick, Algorithms in C, Addison-Wesley, ISBN 0201514257.
22!*/
23
24/// The following functions will sort the elements of an array or vector, either directly or indirectly. They are defined for all real and
25/// integer types using the normal suffix rules. For example, the float versions of the array functions are gsl_sort_float and gsl_sort_float_index.
26/// The corresponding vector functions are gsl_sort_vector_float and gsl_sort_vector_float_index. The prototypes are available in the header files
27/// gsl_sort_float.h gsl_sort_vector_float.h. The complete set of prototypes can be included using the header files gsl_sort.h and gsl_sort_vector.h.
28///
29/// There are no functions for sorting complex arrays or vectors, since the ordering of complex numbers is not uniquely defined. To sort a complex
30/// vector by magnitude compute a real vector containing the magnitudes of the complex elements, and sort this vector indirectly. The resulting index
31/// gives the appropriate ordering of the original complex vector.
32pub mod vectors {
33 use crate::Value;
34 use ffi::FFI;
35 use types::{Permutation, VectorF64};
36
37 /// This function sorts the n elements of the array data with stride stride into ascending numerical order.
38 #[doc(alias = "gsl_sort")]
39 pub fn sort(data: &mut [f64], stride: usize, n: usize) {
40 unsafe { sys::gsl_sort(data.as_mut_ptr(), stride, n) }
41 }
42
43 /// This function sorts the n elements of the array data1 with stride stride1 into ascending numerical order, while making the same rearrangement
44 /// of the array data2 with stride stride2, also of size n.
45 #[doc(alias = "gsl_sort2")]
46 pub fn sort2(data1: &mut [f64], stride1: usize, data2: &mut [f64], stride2: usize, n: usize) {
47 unsafe { sys::gsl_sort2(data1.as_mut_ptr(), stride1, data2.as_mut_ptr(), stride2, n) }
48 }
49
50 /// This function sorts the elements of the vector v into ascending numerical order.
51 #[doc(alias = "gsl_sort_vector")]
52 pub fn sort_vector(v: &mut VectorF64) {
53 unsafe { sys::gsl_sort_vector(v.unwrap_unique()) }
54 }
55
56 /// This function sorts the elements of the vector v1 into ascending numerical order, while making the same rearrangement of the vector v2.
57 #[doc(alias = "gsl_sort_vector2")]
58 pub fn sort_vector2(v1: &mut VectorF64, v2: &mut VectorF64) {
59 unsafe { sys::gsl_sort_vector2(v1.unwrap_unique(), v2.unwrap_unique()) }
60 }
61
62 /// This function indirectly sorts the n elements of the array data with stride stride into ascending order, storing the resulting
63 /// permutation in p. The array p must be allocated with a sufficient length to store the n elements of the permutation. The elements of p
64 /// give the index of the array element which would have been stored in that position if the array had been sorted in place. The array data is not changed.
65 #[doc(alias = "gsl_sort_index")]
66 pub fn sort_index(p: &mut [usize], data: &[f64], stride: usize, n: usize) {
67 unsafe { sys::gsl_sort_index(p.as_mut_ptr(), data.as_ptr(), stride, n) }
68 }
69
70 /// This function indirectly sorts the elements of the vector v into ascending order, storing the resulting permutation in p. The elements of p give the
71 /// index of the vector element which would have been stored in that position if the vector had been sorted in place. The first element of p gives the index
72 /// of the least element in v, and the last element of p gives the index of the greatest element in v. The vector v is not changed.
73 #[doc(alias = "gsl_sort_vector_index")]
74 pub fn sort_vector_index(p: &mut Permutation, v: &VectorF64) -> Result<(), Value> {
75 let ret = unsafe { sys::gsl_sort_vector_index(p.unwrap_unique(), v.unwrap_shared()) };
76 result_handler!(ret, ())
77 }
78}
79
80/// The functions described in this section select the k smallest or largest elements of a data set of size N. The routines use an O(kN) direct insertion
81/// algorithm which is suited to subsets that are small compared with the total size of the dataset. For example, the routines are useful for selecting the
82/// 10 largest values from one million data points, but not for selecting the largest 100,000 values. If the subset is a significant part of the total dataset
83/// it may be faster to sort all the elements of the dataset directly with an O(N \log N) algorithm and obtain the smallest or largest values that way.
84pub mod select {
85 use crate::Value;
86 use ffi::FFI;
87 use types::VectorF64;
88
89 /// This function copies the k smallest elements of the array src, of size n and stride stride, in ascending numerical order into the array dest. The size
90 /// k of the subset must be less than or equal to n. The data src is not modified by this operation.
91 #[doc(alias = "gsl_sort_smallest")]
92 pub fn sort_smallest(
93 dest: &mut [f64],
94 k: usize,
95 src: &[f64],
96 stride: usize,
97 ) -> Result<(), Value> {
98 let ret = unsafe {
99 sys::gsl_sort_smallest(dest.as_mut_ptr(), k, src.as_ptr(), stride, src.len() as _)
100 };
101 result_handler!(ret, ())
102 }
103
104 /// This function copies the k largest elements of the array src, of size n and stride stride, in descending numerical order into the array dest. k must
105 /// be less than or equal to n. The data src is not modified by this operation.
106 #[doc(alias = "gsl_sort_largest")]
107 pub fn sort_largest(
108 dest: &mut [f64],
109 k: usize,
110 src: &[f64],
111 stride: usize,
112 ) -> Result<(), Value> {
113 let ret = unsafe {
114 sys::gsl_sort_largest(dest.as_mut_ptr(), k, src.as_ptr(), stride, src.len() as _)
115 };
116 result_handler!(ret, ())
117 }
118
119 /// This function copies the k smallest or largest elements of the vector v into the array dest. k must be less than or equal to the length of the vector v.
120 #[doc(alias = "gsl_sort_vector_smallest")]
121 pub fn sort_vector_smallest(dest: &mut [f64], k: usize, v: &VectorF64) -> Result<(), Value> {
122 let ret = unsafe { sys::gsl_sort_vector_smallest(dest.as_mut_ptr(), k, v.unwrap_shared()) };
123 result_handler!(ret, ())
124 }
125
126 /// This function copies the k smallest or largest elements of the vector v into the array dest. k must be less than or equal to the length of the vector v.
127 #[doc(alias = "gsl_sort_vector_largest")]
128 pub fn sort_vector_largest(dest: &mut [f64], k: usize, v: &VectorF64) -> Result<(), Value> {
129 let ret = unsafe { sys::gsl_sort_vector_largest(dest.as_mut_ptr(), k, v.unwrap_shared()) };
130 result_handler!(ret, ())
131 }
132
133 /// This function stores the indices of the k smallest elements of the array src, of size n and stride stride, in the array p. The indices are chosen so that
134 /// the corresponding data is in ascending numerical order. k must be less than or equal to n. The data src is not modified by this operation.
135 #[doc(alias = "gsl_sort_smallest_index")]
136 pub fn sort_smallest_index(
137 p: &mut [usize],
138 k: usize,
139 src: &[f64],
140 stride: usize,
141 ) -> Result<(), Value> {
142 let ret = unsafe {
143 sys::gsl_sort_smallest_index(p.as_mut_ptr(), k, src.as_ptr(), stride, src.len() as _)
144 };
145 result_handler!(ret, ())
146 }
147
148 /// This function stores the indices of the k largest elements of the array src, of size n and stride stride, in the array p. The indices are chosen so that
149 /// the corresponding data is in descending numerical order. k must be less than or equal to n. The data src is not modified by this operation.
150 #[doc(alias = "gsl_sort_largest_index")]
151 pub fn sort_largest_index(
152 p: &mut [usize],
153 k: usize,
154 src: &[f64],
155 stride: usize,
156 ) -> Result<(), Value> {
157 let ret = unsafe {
158 sys::gsl_sort_largest_index(p.as_mut_ptr(), k, src.as_ptr(), stride, src.len() as _)
159 };
160 result_handler!(ret, ())
161 }
162
163 /// This function stores the indices of the k smallest or largest elements of the vector v in the array p. k must be less than or equal to the length of
164 /// the vector v.
165 #[doc(alias = "gsl_sort_vector_smallest_index")]
166 pub fn sort_vector_smallest_index(
167 p: &mut [usize],
168 k: usize,
169 v: &VectorF64,
170 ) -> Result<(), Value> {
171 let ret =
172 unsafe { sys::gsl_sort_vector_smallest_index(p.as_mut_ptr(), k, v.unwrap_shared()) };
173 result_handler!(ret, ())
174 }
175
176 /// This function stores the indices of the k smallest or largest elements of the vector v in the array p. k must be less than or equal to the length of
177 /// the vector v.
178 #[doc(alias = "gsl_sort_vector_largest_index")]
179 pub fn sort_vector_largest_index(
180 p: &mut [usize],
181 k: usize,
182 v: &VectorF64,
183 ) -> Result<(), Value> {
184 let ret =
185 unsafe { sys::gsl_sort_vector_largest_index(p.as_mut_ptr(), k, v.unwrap_shared()) };
186 result_handler!(ret, ())
187 }
188}