STK++ 0.9.13
STK_HeapSort.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2006-2007 Serge Iovleff
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the
16 Free Software Foundation, Inc.,
17 59 Temple Place,
18 Suite 330,
19 Boston, MA 02111-1307
20 USA
21
22 Contact : S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
23*/
24
25/*
26 * Project: stkpp::DManager
27 * Purpose: sorting method acting on Containers
28 * Author: Serge Iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 *
30 **/
31
37#ifndef STK_HEAPSORT_H
38#define STK_HEAPSORT_H
39
40//#include "Arrays/include/STK_ArrayBase.h"
41
42namespace STK
43{
44
49template<class Vector>
51{
53 typedef typename hidden::Traits<Vector>::Type Type;
54 // number of elements
55 const int nb_elt = T.size();
56 if (nb_elt < 2) return;
57
58 // if the container is base one, shift0 = 0 and shift1 = 1
59 int shift1 = T.begin(), shift0 = T.begin() - 1;
60
61 // create heap
62 for (int first = nb_elt/2; first > 0; -- first)
63 {
64 // the value value to insert in the heap
65 Type value = T[shift0 + first];
66 // organize the heap
67 int i=first, j=2*first;
68 while (j <= nb_elt)
69 {
70 // j+1 is greatest child
71 if ( j < nb_elt && T[shift0 + j] < T[shift1 + j] ) j++;
72 // we have find a child gt value
73 if (value >= T[shift0 + j]) break;
74 // else shift the inner value
75 T[shift0 + i] = T[shift0 + j];
76 // go down in the tree
77 i = j;
78 j*= 2;
79 }
80 // plug value in its final location
81 T[shift0 + i] = value;
82 }
83#ifdef STK_HEAPSORT_DEBUG
84 std::cout << "T=\n" << T.asDerived() << _T("\n";);
85#endif
86 // sort T
87 for (int last = nb_elt;;)
88 { // the value to sort
89 Type value = T[shift0 + last];
90 // Put the top of the heap at the end
91 T[shift0 + last] = T[shift1];
92 // decrease last. last==1 : we end the job
93 if (--last == 1)
94 { T[shift1] = value;
95 break;
96 }
97 // organize the heap
98 int i=1, j=2;
99 while (j <= last)
100 { // j+1 is greatest child
101 if ( j < last && T[shift0 + j] < T[shift1 + j] ) j++;
102 // we have find a child gt value
103 if (value >= T[shift0 + j]) break;
104 // else shift the inner value
105 T[shift0 + i] = T[shift0 + j];
106 // go down in the tree
107 i = j;
108 j*= 2;
109 }
110 // plug value in its final location
111 T[shift0 + i] = value;
112 }
113}
114
121template< class Vector>
122void heapSort( Vector const& T, Vector& Tsort)
123{
125 typedef typename Vector::Type Type;
126 // copy T in Tsort
127 Tsort = T.asDerived();
128 // number of elements
129 const int nb_elt = Tsort.size();
130 if (nb_elt < 2) return;
131
132 // if the container is base one, shift0 = 0 and shift1 = 1
133 int shift1 = Tsort.begin(), shift0 = Tsort.begin() - 1;
134
135 // create heap
136 for (int first = nb_elt/2; first > 0; -- first)
137 {
138 // the value value to insert in the heap
139 Type value = Tsort[shift0 + first];
140 // organize the heap
141 int i=first, j=2*first;
142 while (j <= nb_elt)
143 {
144 // j+1 is greatest child
145 if ( j < nb_elt && Tsort[shift0 + j] < Tsort[shift1 + j] ) j++;
146 // we have find a child gt value
147 if (value >= Tsort[shift0 + j]) break;
148 // else shift the inner value
149 Tsort[shift0 + i] = Tsort[shift0 + j];
150 // go down in the tree
151 i = j;
152 j*= 2;
153 }
154 // plug value in its final location
155 Tsort[shift0 + i] = value;
156 }
157#ifdef STK_HEAPSORT_DEBUG
158 std::cout << "T=\n" << Tsort << _T("\n";);
159#endif
160 // sort T
161 for (int last = nb_elt;;)
162 { // the value to sort
163 Type value = Tsort[shift0 + last];
164 // Put the top of the heap at the end
166 // decrease last. last==1 : we end the job
167 if (--last == 1)
168 { Tsort[shift1] = value;
169 break;
170 }
171 // organize the heap
172 int i=1, j=2;
173 while (j <= last)
174 { // j+1 is greatest child
175 if ( j < last && Tsort[shift0 + j] < Tsort[shift1 + j] ) j++;
176 // we have find a child gt value
177 if (value >= Tsort[shift0 + j]) break;
178 // else shift the inner value
179 Tsort[shift0 + i] = Tsort[shift0 + j];
180 // go down in the tree
181 i = j;
182 j*= 2;
183 }
184 // plug value in its final location
185 Tsort[shift0 + i] = value;
186 }
187}
188
196template< class Vector, class VectorInt>
197void heapSort( VectorInt& I, Vector const& T)
198{
201 typedef typename hidden::Traits<Vector>::Type Type;
202
203 // number of elements
204 int nb_elt = T.size();
205
206 // create index array
207 I.asDerived().resize(T.range());
208 int first = I.begin(), last = I.lastIdx();
209 for (int i=first; i<=last; i++)
210 { I[i] = i;}
211
212 if (nb_elt < 2) return;
213
214 // if the container is base one, shift0 = 0 and shift1 = 1
215 int shift1 = T.begin(), shift0 = T.begin() - 1;
216
217 // create heap
218 for (first = nb_elt/2; first > 0; --first)
219 {
220 // the value value to insert in the heap
221 Type value = T[I[shift0 + first]];
222 // organize the heap
223 int i=first, j=2*first;
224 while (j <= nb_elt)
225 {
226 // j+1 is greatest child
227 if ( j < nb_elt && T[I[shift0 + j]] < T[I[shift1 + j]] ) j++;
228 // we have find a child lt value
229 if (value >= T[I[shift0 + j]]) break;
230 // else shift the inner values
231 I[shift0 + i] = I[shift0 + j];
232 // go down in the tree
233 i = j;
234 j*= 2;
235 }
236 // plug value in its final location
237 I[shift0 + i] = shift0 + first;
238 }
239#ifdef STK_HEAPSORT_DEBUG
240 std::cout << "I=\n" << I <<"\n";
241#endif
242 // sort T
243 for (int last = nb_elt;;)
244 {
245 // the value to sort
246 int ivalue = I[shift0 + last];
247 Type value = T[ivalue];
248 // Put the top of the heap at the end
249 //T[shift0 + last] = T[shift1];
250 I[shift0 + last] = I[shift1];
251 // decrease last. last==1 : we end the job
252 if (--last == 1)
253 { //T[shift1] = value;
254 I[shift1] = ivalue;
255 break;
256 }
257 // organize the heap
258 int i=1, j=2;
259 while (j <= last)
260 { // j+1 is greatest child
261 if ( j < last && T[I[shift0 + j]] < T[I[shift1 + j]] ) j++;
262 // we have find a child gt value
263 if (value >= T[I[shift0 + j]]) break;
264 // else shift the inner value
265 // T[shift0 + i] = T[shift0 + j];
266 I[shift0 + i] = I[shift0 + j];
267 // go down in the tree
268 i = j;
269 j*= 2;
270 }
271 // plug value in its final location
272 // T[shift0 + i] = value;
273 I[shift0 + i] = ivalue;
274 }
275#ifdef STK_HEAPSORT_DEBUG
276 std::cout << "I=\n" << I <<"\n";
277#endif
278}
279
280
286template<class Vector, class VectorInt>
287void applySort1D( Vector& T, VectorInt const& I)
288{
291#ifdef STK_BOUNDS_CHECK
292 if (I.range() != T.range())
294#endif
295 Vector A(T.range());
296 for (int i=I.begin(); i< I.end(); i++) { A[i] = T[I[i]];}
297 T.move(A);
298}
299
305template < class Array, class VectorInt>
306void applySort2D( Array& T, VectorInt const& I)
307{
309#ifdef STK_BOUNDS_CHECK
310 if (I.range() != T.rows())
312#endif
313 Array A(T.rows(), T.cols());
314 for (int i=I.begin(); i< I.end(); i++) { A.row(i) = T.row(I[i]);}
315 T.move(A);
316}
317
318} // namespace STK
319
320#endif /*STK_HEAPSORT_H*/
#define STKRUNTIME_ERROR_2ARG(Where, Arg1, Arg2, Error)
Definition STK_Macros.h:120
#define STK_STATIC_ASSERT_ONE_DIMENSION_ONLY(EXPR)
#define _T(x)
Let x unmodified.
hidden::Traits< Array2DVector< Real > >::Type Type
Derived & move(Derived const &T)
move T to this.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
void heapSort(Vector &T)
Sort the container T in ascending order.
void applySort2D(Array &T, VectorInt const &I)
Apply a sorting index array to the 2D container T row by row.
void applySort1D(Vector &T, VectorInt const &I)
Apply a sorting index array to the 1D container T.
The namespace STK is the main domain space of the Statistical ToolKit project.