1 | /*************************************************************************
|
---|
2 | Copyright 2008 by Sergey Bochkanov (ALGLIB project).
|
---|
3 |
|
---|
4 | >>> SOURCE LICENSE >>>
|
---|
5 | This program is free software; you can redistribute it and/or modify
|
---|
6 | it under the terms of the GNU General Public License as published by
|
---|
7 | the Free Software Foundation (www.fsf.org); either version 2 of the
|
---|
8 | License, or (at your option) any later version.
|
---|
9 |
|
---|
10 | This program is distributed in the hope that it will be useful,
|
---|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | GNU General Public License for more details.
|
---|
14 |
|
---|
15 | A copy of the GNU General Public License is available at
|
---|
16 | http://www.fsf.org/licensing/licenses
|
---|
17 |
|
---|
18 | >>> END OF LICENSE >>>
|
---|
19 | *************************************************************************/
|
---|
20 |
|
---|
21 |
|
---|
22 | namespace alglib {
|
---|
23 | public class tsort {
|
---|
24 | public static void tagsort(ref double[] a,
|
---|
25 | int n,
|
---|
26 | ref int[] p1,
|
---|
27 | ref int[] p2) {
|
---|
28 | int i = 0;
|
---|
29 | int[] pv = new int[0];
|
---|
30 | int[] vp = new int[0];
|
---|
31 | int lv = 0;
|
---|
32 | int lp = 0;
|
---|
33 | int rv = 0;
|
---|
34 | int rp = 0;
|
---|
35 |
|
---|
36 |
|
---|
37 | //
|
---|
38 | // Special cases
|
---|
39 | //
|
---|
40 | if (n <= 0) {
|
---|
41 | return;
|
---|
42 | }
|
---|
43 | if (n == 1) {
|
---|
44 | p1 = new int[0 + 1];
|
---|
45 | p2 = new int[0 + 1];
|
---|
46 | p1[0] = 0;
|
---|
47 | p2[0] = 0;
|
---|
48 | return;
|
---|
49 | }
|
---|
50 |
|
---|
51 | //
|
---|
52 | // General case, N>1: prepare permutations table P1
|
---|
53 | //
|
---|
54 | p1 = new int[n - 1 + 1];
|
---|
55 | for (i = 0; i <= n - 1; i++) {
|
---|
56 | p1[i] = i;
|
---|
57 | }
|
---|
58 |
|
---|
59 | //
|
---|
60 | // General case, N>1: sort, update P1
|
---|
61 | //
|
---|
62 | tagsortfasti(ref a, ref p1, n);
|
---|
63 |
|
---|
64 | //
|
---|
65 | // General case, N>1: fill permutations table P2
|
---|
66 | //
|
---|
67 | // To fill P2 we maintain two arrays:
|
---|
68 | // * PV, Position(Value). PV[i] contains position of I-th key at the moment
|
---|
69 | // * VP, Value(Position). VP[i] contains key which has position I at the moment
|
---|
70 | //
|
---|
71 | // At each step we making permutation of two items:
|
---|
72 | // Left, which is given by position/value pair LP/LV
|
---|
73 | // and Right, which is given by RP/RV
|
---|
74 | // and updating PV[] and VP[] correspondingly.
|
---|
75 | //
|
---|
76 | pv = new int[n - 1 + 1];
|
---|
77 | vp = new int[n - 1 + 1];
|
---|
78 | p2 = new int[n - 1 + 1];
|
---|
79 | for (i = 0; i <= n - 1; i++) {
|
---|
80 | pv[i] = i;
|
---|
81 | vp[i] = i;
|
---|
82 | }
|
---|
83 | for (i = 0; i <= n - 1; i++) {
|
---|
84 |
|
---|
85 | //
|
---|
86 | // calculate LP, LV, RP, RV
|
---|
87 | //
|
---|
88 | lp = i;
|
---|
89 | lv = vp[lp];
|
---|
90 | rv = p1[i];
|
---|
91 | rp = pv[rv];
|
---|
92 |
|
---|
93 | //
|
---|
94 | // Fill P2
|
---|
95 | //
|
---|
96 | p2[i] = rp;
|
---|
97 |
|
---|
98 | //
|
---|
99 | // update PV and VP
|
---|
100 | //
|
---|
101 | vp[lp] = rv;
|
---|
102 | vp[rp] = lv;
|
---|
103 | pv[lv] = rp;
|
---|
104 | pv[rv] = lp;
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | public static void tagsortfasti(ref double[] a,
|
---|
110 | ref int[] b,
|
---|
111 | int n) {
|
---|
112 | int i = 0;
|
---|
113 | int k = 0;
|
---|
114 | int t = 0;
|
---|
115 | double tmp = 0;
|
---|
116 | int tmpi = 0;
|
---|
117 |
|
---|
118 |
|
---|
119 | //
|
---|
120 | // Special cases
|
---|
121 | //
|
---|
122 | if (n <= 1) {
|
---|
123 | return;
|
---|
124 | }
|
---|
125 |
|
---|
126 | //
|
---|
127 | // General case, N>1: sort, update B
|
---|
128 | //
|
---|
129 | i = 2;
|
---|
130 | do {
|
---|
131 | t = i;
|
---|
132 | while (t != 1) {
|
---|
133 | k = t / 2;
|
---|
134 | if ((double)(a[k - 1]) >= (double)(a[t - 1])) {
|
---|
135 | t = 1;
|
---|
136 | } else {
|
---|
137 | tmp = a[k - 1];
|
---|
138 | a[k - 1] = a[t - 1];
|
---|
139 | a[t - 1] = tmp;
|
---|
140 | tmpi = b[k - 1];
|
---|
141 | b[k - 1] = b[t - 1];
|
---|
142 | b[t - 1] = tmpi;
|
---|
143 | t = k;
|
---|
144 | }
|
---|
145 | }
|
---|
146 | i = i + 1;
|
---|
147 | }
|
---|
148 | while (i <= n);
|
---|
149 | i = n - 1;
|
---|
150 | do {
|
---|
151 | tmp = a[i];
|
---|
152 | a[i] = a[0];
|
---|
153 | a[0] = tmp;
|
---|
154 | tmpi = b[i];
|
---|
155 | b[i] = b[0];
|
---|
156 | b[0] = tmpi;
|
---|
157 | t = 1;
|
---|
158 | while (t != 0) {
|
---|
159 | k = 2 * t;
|
---|
160 | if (k > i) {
|
---|
161 | t = 0;
|
---|
162 | } else {
|
---|
163 | if (k < i) {
|
---|
164 | if ((double)(a[k]) > (double)(a[k - 1])) {
|
---|
165 | k = k + 1;
|
---|
166 | }
|
---|
167 | }
|
---|
168 | if ((double)(a[t - 1]) >= (double)(a[k - 1])) {
|
---|
169 | t = 0;
|
---|
170 | } else {
|
---|
171 | tmp = a[k - 1];
|
---|
172 | a[k - 1] = a[t - 1];
|
---|
173 | a[t - 1] = tmp;
|
---|
174 | tmpi = b[k - 1];
|
---|
175 | b[k - 1] = b[t - 1];
|
---|
176 | b[t - 1] = tmpi;
|
---|
177 | t = k;
|
---|
178 | }
|
---|
179 | }
|
---|
180 | }
|
---|
181 | i = i - 1;
|
---|
182 | }
|
---|
183 | while (i >= 1);
|
---|
184 | }
|
---|
185 |
|
---|
186 |
|
---|
187 | public static void tagsortfastr(ref double[] a,
|
---|
188 | ref double[] b,
|
---|
189 | int n) {
|
---|
190 | int i = 0;
|
---|
191 | int k = 0;
|
---|
192 | int t = 0;
|
---|
193 | double tmp = 0;
|
---|
194 | double tmpr = 0;
|
---|
195 |
|
---|
196 |
|
---|
197 | //
|
---|
198 | // Special cases
|
---|
199 | //
|
---|
200 | if (n <= 1) {
|
---|
201 | return;
|
---|
202 | }
|
---|
203 |
|
---|
204 | //
|
---|
205 | // General case, N>1: sort, update B
|
---|
206 | //
|
---|
207 | i = 2;
|
---|
208 | do {
|
---|
209 | t = i;
|
---|
210 | while (t != 1) {
|
---|
211 | k = t / 2;
|
---|
212 | if ((double)(a[k - 1]) >= (double)(a[t - 1])) {
|
---|
213 | t = 1;
|
---|
214 | } else {
|
---|
215 | tmp = a[k - 1];
|
---|
216 | a[k - 1] = a[t - 1];
|
---|
217 | a[t - 1] = tmp;
|
---|
218 | tmpr = b[k - 1];
|
---|
219 | b[k - 1] = b[t - 1];
|
---|
220 | b[t - 1] = tmpr;
|
---|
221 | t = k;
|
---|
222 | }
|
---|
223 | }
|
---|
224 | i = i + 1;
|
---|
225 | }
|
---|
226 | while (i <= n);
|
---|
227 | i = n - 1;
|
---|
228 | do {
|
---|
229 | tmp = a[i];
|
---|
230 | a[i] = a[0];
|
---|
231 | a[0] = tmp;
|
---|
232 | tmpr = b[i];
|
---|
233 | b[i] = b[0];
|
---|
234 | b[0] = tmpr;
|
---|
235 | t = 1;
|
---|
236 | while (t != 0) {
|
---|
237 | k = 2 * t;
|
---|
238 | if (k > i) {
|
---|
239 | t = 0;
|
---|
240 | } else {
|
---|
241 | if (k < i) {
|
---|
242 | if ((double)(a[k]) > (double)(a[k - 1])) {
|
---|
243 | k = k + 1;
|
---|
244 | }
|
---|
245 | }
|
---|
246 | if ((double)(a[t - 1]) >= (double)(a[k - 1])) {
|
---|
247 | t = 0;
|
---|
248 | } else {
|
---|
249 | tmp = a[k - 1];
|
---|
250 | a[k - 1] = a[t - 1];
|
---|
251 | a[t - 1] = tmp;
|
---|
252 | tmpr = b[k - 1];
|
---|
253 | b[k - 1] = b[t - 1];
|
---|
254 | b[t - 1] = tmpr;
|
---|
255 | t = k;
|
---|
256 | }
|
---|
257 | }
|
---|
258 | }
|
---|
259 | i = i - 1;
|
---|
260 | }
|
---|
261 | while (i >= 1);
|
---|
262 | }
|
---|
263 |
|
---|
264 |
|
---|
265 | public static void tagsortfast(ref double[] a,
|
---|
266 | int n) {
|
---|
267 | int i = 0;
|
---|
268 | int k = 0;
|
---|
269 | int t = 0;
|
---|
270 | double tmp = 0;
|
---|
271 |
|
---|
272 |
|
---|
273 | //
|
---|
274 | // Special cases
|
---|
275 | //
|
---|
276 | if (n <= 1) {
|
---|
277 | return;
|
---|
278 | }
|
---|
279 |
|
---|
280 | //
|
---|
281 | // General case, N>1: sort, update B
|
---|
282 | //
|
---|
283 | i = 2;
|
---|
284 | do {
|
---|
285 | t = i;
|
---|
286 | while (t != 1) {
|
---|
287 | k = t / 2;
|
---|
288 | if ((double)(a[k - 1]) >= (double)(a[t - 1])) {
|
---|
289 | t = 1;
|
---|
290 | } else {
|
---|
291 | tmp = a[k - 1];
|
---|
292 | a[k - 1] = a[t - 1];
|
---|
293 | a[t - 1] = tmp;
|
---|
294 | t = k;
|
---|
295 | }
|
---|
296 | }
|
---|
297 | i = i + 1;
|
---|
298 | }
|
---|
299 | while (i <= n);
|
---|
300 | i = n - 1;
|
---|
301 | do {
|
---|
302 | tmp = a[i];
|
---|
303 | a[i] = a[0];
|
---|
304 | a[0] = tmp;
|
---|
305 | t = 1;
|
---|
306 | while (t != 0) {
|
---|
307 | k = 2 * t;
|
---|
308 | if (k > i) {
|
---|
309 | t = 0;
|
---|
310 | } else {
|
---|
311 | if (k < i) {
|
---|
312 | if ((double)(a[k]) > (double)(a[k - 1])) {
|
---|
313 | k = k + 1;
|
---|
314 | }
|
---|
315 | }
|
---|
316 | if ((double)(a[t - 1]) >= (double)(a[k - 1])) {
|
---|
317 | t = 0;
|
---|
318 | } else {
|
---|
319 | tmp = a[k - 1];
|
---|
320 | a[k - 1] = a[t - 1];
|
---|
321 | a[t - 1] = tmp;
|
---|
322 | t = k;
|
---|
323 | }
|
---|
324 | }
|
---|
325 | }
|
---|
326 | i = i - 1;
|
---|
327 | }
|
---|
328 | while (i >= 1);
|
---|
329 | }
|
---|
330 |
|
---|
331 |
|
---|
332 | /*************************************************************************
|
---|
333 | Heap operations: adds element to the heap
|
---|
334 |
|
---|
335 | PARAMETERS:
|
---|
336 | A - heap itself, must be at least array[0..N]
|
---|
337 | B - array of integer tags, which are updated according to
|
---|
338 | permutations in the heap
|
---|
339 | N - size of the heap (without new element).
|
---|
340 | updated on output
|
---|
341 | VA - value of the element being added
|
---|
342 | VB - value of the tag
|
---|
343 |
|
---|
344 | -- ALGLIB --
|
---|
345 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
346 | *************************************************************************/
|
---|
347 | public static void tagheappushi(ref double[] a,
|
---|
348 | ref int[] b,
|
---|
349 | ref int n,
|
---|
350 | double va,
|
---|
351 | int vb) {
|
---|
352 | int j = 0;
|
---|
353 | int k = 0;
|
---|
354 | double v = 0;
|
---|
355 |
|
---|
356 | if (n < 0) {
|
---|
357 | return;
|
---|
358 | }
|
---|
359 |
|
---|
360 | //
|
---|
361 | // N=0 is a special case
|
---|
362 | //
|
---|
363 | if (n == 0) {
|
---|
364 | a[0] = va;
|
---|
365 | b[0] = vb;
|
---|
366 | n = n + 1;
|
---|
367 | return;
|
---|
368 | }
|
---|
369 |
|
---|
370 | //
|
---|
371 | // add current point to the heap
|
---|
372 | // (add to the bottom, then move up)
|
---|
373 | //
|
---|
374 | // we don't write point to the heap
|
---|
375 | // until its final position is determined
|
---|
376 | // (it allow us to reduce number of array access operations)
|
---|
377 | //
|
---|
378 | j = n;
|
---|
379 | n = n + 1;
|
---|
380 | while (j > 0) {
|
---|
381 | k = (j - 1) / 2;
|
---|
382 | v = a[k];
|
---|
383 | if ((double)(v) < (double)(va)) {
|
---|
384 |
|
---|
385 | //
|
---|
386 | // swap with higher element
|
---|
387 | //
|
---|
388 | a[j] = v;
|
---|
389 | b[j] = b[k];
|
---|
390 | j = k;
|
---|
391 | } else {
|
---|
392 |
|
---|
393 | //
|
---|
394 | // element in its place. terminate.
|
---|
395 | //
|
---|
396 | break;
|
---|
397 | }
|
---|
398 | }
|
---|
399 | a[j] = va;
|
---|
400 | b[j] = vb;
|
---|
401 | }
|
---|
402 |
|
---|
403 |
|
---|
404 | /*************************************************************************
|
---|
405 | Heap operations: replaces top element with new element
|
---|
406 | (which is moved down)
|
---|
407 |
|
---|
408 | PARAMETERS:
|
---|
409 | A - heap itself, must be at least array[0..N-1]
|
---|
410 | B - array of integer tags, which are updated according to
|
---|
411 | permutations in the heap
|
---|
412 | N - size of the heap
|
---|
413 | VA - value of the element which replaces top element
|
---|
414 | VB - value of the tag
|
---|
415 |
|
---|
416 | -- ALGLIB --
|
---|
417 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
418 | *************************************************************************/
|
---|
419 | public static void tagheapreplacetopi(ref double[] a,
|
---|
420 | ref int[] b,
|
---|
421 | int n,
|
---|
422 | double va,
|
---|
423 | int vb) {
|
---|
424 | int j = 0;
|
---|
425 | int k1 = 0;
|
---|
426 | int k2 = 0;
|
---|
427 | double v = 0;
|
---|
428 | double v1 = 0;
|
---|
429 | double v2 = 0;
|
---|
430 |
|
---|
431 | if (n < 1) {
|
---|
432 | return;
|
---|
433 | }
|
---|
434 |
|
---|
435 | //
|
---|
436 | // N=1 is a special case
|
---|
437 | //
|
---|
438 | if (n == 1) {
|
---|
439 | a[0] = va;
|
---|
440 | b[0] = vb;
|
---|
441 | return;
|
---|
442 | }
|
---|
443 |
|
---|
444 | //
|
---|
445 | // move down through heap:
|
---|
446 | // * J - current element
|
---|
447 | // * K1 - first child (always exists)
|
---|
448 | // * K2 - second child (may not exists)
|
---|
449 | //
|
---|
450 | // we don't write point to the heap
|
---|
451 | // until its final position is determined
|
---|
452 | // (it allow us to reduce number of array access operations)
|
---|
453 | //
|
---|
454 | j = 0;
|
---|
455 | k1 = 1;
|
---|
456 | k2 = 2;
|
---|
457 | while (k1 < n) {
|
---|
458 | if (k2 >= n) {
|
---|
459 |
|
---|
460 | //
|
---|
461 | // only one child.
|
---|
462 | //
|
---|
463 | // swap and terminate (because this child
|
---|
464 | // have no siblings due to heap structure)
|
---|
465 | //
|
---|
466 | v = a[k1];
|
---|
467 | if ((double)(v) > (double)(va)) {
|
---|
468 | a[j] = v;
|
---|
469 | b[j] = b[k1];
|
---|
470 | j = k1;
|
---|
471 | }
|
---|
472 | break;
|
---|
473 | } else {
|
---|
474 |
|
---|
475 | //
|
---|
476 | // two childs
|
---|
477 | //
|
---|
478 | v1 = a[k1];
|
---|
479 | v2 = a[k2];
|
---|
480 | if ((double)(v1) > (double)(v2)) {
|
---|
481 | if ((double)(va) < (double)(v1)) {
|
---|
482 | a[j] = v1;
|
---|
483 | b[j] = b[k1];
|
---|
484 | j = k1;
|
---|
485 | } else {
|
---|
486 | break;
|
---|
487 | }
|
---|
488 | } else {
|
---|
489 | if ((double)(va) < (double)(v2)) {
|
---|
490 | a[j] = v2;
|
---|
491 | b[j] = b[k2];
|
---|
492 | j = k2;
|
---|
493 | } else {
|
---|
494 | break;
|
---|
495 | }
|
---|
496 | }
|
---|
497 | k1 = 2 * j + 1;
|
---|
498 | k2 = 2 * j + 2;
|
---|
499 | }
|
---|
500 | }
|
---|
501 | a[j] = va;
|
---|
502 | b[j] = vb;
|
---|
503 | }
|
---|
504 |
|
---|
505 |
|
---|
506 | /*************************************************************************
|
---|
507 | Heap operations: pops top element from the heap
|
---|
508 |
|
---|
509 | PARAMETERS:
|
---|
510 | A - heap itself, must be at least array[0..N-1]
|
---|
511 | B - array of integer tags, which are updated according to
|
---|
512 | permutations in the heap
|
---|
513 | N - size of the heap, N>=1
|
---|
514 |
|
---|
515 | On output top element is moved to A[N-1], B[N-1], heap is reordered, N is
|
---|
516 | decreased by 1.
|
---|
517 |
|
---|
518 | -- ALGLIB --
|
---|
519 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
520 | *************************************************************************/
|
---|
521 | public static void tagheappopi(ref double[] a,
|
---|
522 | ref int[] b,
|
---|
523 | ref int n) {
|
---|
524 | double va = 0;
|
---|
525 | int vb = 0;
|
---|
526 |
|
---|
527 | if (n < 1) {
|
---|
528 | return;
|
---|
529 | }
|
---|
530 |
|
---|
531 | //
|
---|
532 | // N=1 is a special case
|
---|
533 | //
|
---|
534 | if (n == 1) {
|
---|
535 | n = 0;
|
---|
536 | return;
|
---|
537 | }
|
---|
538 |
|
---|
539 | //
|
---|
540 | // swap top element and last element,
|
---|
541 | // then reorder heap
|
---|
542 | //
|
---|
543 | va = a[n - 1];
|
---|
544 | vb = b[n - 1];
|
---|
545 | a[n - 1] = a[0];
|
---|
546 | b[n - 1] = b[0];
|
---|
547 | n = n - 1;
|
---|
548 | tagheapreplacetopi(ref a, ref b, n, va, vb);
|
---|
549 | }
|
---|
550 | }
|
---|
551 | }
|
---|