[2563] | 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 | using System;
|
---|
| 22 |
|
---|
| 23 | namespace alglib
|
---|
| 24 | {
|
---|
| 25 | public class tsort
|
---|
| 26 | {
|
---|
| 27 | public static void tagsort(ref double[] a,
|
---|
| 28 | int n,
|
---|
| 29 | ref int[] p1,
|
---|
| 30 | ref int[] p2)
|
---|
| 31 | {
|
---|
| 32 | int i = 0;
|
---|
| 33 | int j = 0;
|
---|
| 34 | int k = 0;
|
---|
| 35 | int t = 0;
|
---|
| 36 | double tmp = 0;
|
---|
| 37 | int tmpi = 0;
|
---|
| 38 | int[] pv = new int[0];
|
---|
| 39 | int[] vp = new int[0];
|
---|
| 40 | int lv = 0;
|
---|
| 41 | int lp = 0;
|
---|
| 42 | int rv = 0;
|
---|
| 43 | int rp = 0;
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 | //
|
---|
| 47 | // Special cases
|
---|
| 48 | //
|
---|
| 49 | if( n<=0 )
|
---|
| 50 | {
|
---|
| 51 | return;
|
---|
| 52 | }
|
---|
| 53 | if( n==1 )
|
---|
| 54 | {
|
---|
| 55 | p1 = new int[0+1];
|
---|
| 56 | p2 = new int[0+1];
|
---|
| 57 | p1[0] = 0;
|
---|
| 58 | p2[0] = 0;
|
---|
| 59 | return;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | //
|
---|
| 63 | // General case, N>1: prepare permutations table P1
|
---|
| 64 | //
|
---|
| 65 | p1 = new int[n-1+1];
|
---|
| 66 | for(i=0; i<=n-1; i++)
|
---|
| 67 | {
|
---|
| 68 | p1[i] = i;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | //
|
---|
| 72 | // General case, N>1: sort, update P1
|
---|
| 73 | //
|
---|
| 74 | tagsortfasti(ref a, ref p1, n);
|
---|
| 75 |
|
---|
| 76 | //
|
---|
| 77 | // General case, N>1: fill permutations table P2
|
---|
| 78 | //
|
---|
| 79 | // To fill P2 we maintain two arrays:
|
---|
| 80 | // * PV, Position(Value). PV[i] contains position of I-th key at the moment
|
---|
| 81 | // * VP, Value(Position). VP[i] contains key which has position I at the moment
|
---|
| 82 | //
|
---|
| 83 | // At each step we making permutation of two items:
|
---|
| 84 | // Left, which is given by position/value pair LP/LV
|
---|
| 85 | // and Right, which is given by RP/RV
|
---|
| 86 | // and updating PV[] and VP[] correspondingly.
|
---|
| 87 | //
|
---|
| 88 | pv = new int[n-1+1];
|
---|
| 89 | vp = new int[n-1+1];
|
---|
| 90 | p2 = new int[n-1+1];
|
---|
| 91 | for(i=0; i<=n-1; i++)
|
---|
| 92 | {
|
---|
| 93 | pv[i] = i;
|
---|
| 94 | vp[i] = i;
|
---|
| 95 | }
|
---|
| 96 | for(i=0; i<=n-1; i++)
|
---|
| 97 | {
|
---|
| 98 |
|
---|
| 99 | //
|
---|
| 100 | // calculate LP, LV, RP, RV
|
---|
| 101 | //
|
---|
| 102 | lp = i;
|
---|
| 103 | lv = vp[lp];
|
---|
| 104 | rv = p1[i];
|
---|
| 105 | rp = pv[rv];
|
---|
| 106 |
|
---|
| 107 | //
|
---|
| 108 | // Fill P2
|
---|
| 109 | //
|
---|
| 110 | p2[i] = rp;
|
---|
| 111 |
|
---|
| 112 | //
|
---|
| 113 | // update PV and VP
|
---|
| 114 | //
|
---|
| 115 | vp[lp] = rv;
|
---|
| 116 | vp[rp] = lv;
|
---|
| 117 | pv[lv] = rp;
|
---|
| 118 | pv[rv] = lp;
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 |
|
---|
| 123 | public static void tagsortfasti(ref double[] a,
|
---|
| 124 | ref int[] b,
|
---|
| 125 | int n)
|
---|
| 126 | {
|
---|
| 127 | int i = 0;
|
---|
| 128 | int j = 0;
|
---|
| 129 | int k = 0;
|
---|
| 130 | int t = 0;
|
---|
| 131 | double tmp = 0;
|
---|
| 132 | int tmpi = 0;
|
---|
| 133 |
|
---|
| 134 |
|
---|
| 135 | //
|
---|
| 136 | // Special cases
|
---|
| 137 | //
|
---|
| 138 | if( n<=1 )
|
---|
| 139 | {
|
---|
| 140 | return;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | //
|
---|
| 144 | // General case, N>1: sort, update B
|
---|
| 145 | //
|
---|
| 146 | i = 2;
|
---|
| 147 | do
|
---|
| 148 | {
|
---|
| 149 | t = i;
|
---|
| 150 | while( t!=1 )
|
---|
| 151 | {
|
---|
| 152 | k = t/2;
|
---|
| 153 | if( (double)(a[k-1])>=(double)(a[t-1]) )
|
---|
| 154 | {
|
---|
| 155 | t = 1;
|
---|
| 156 | }
|
---|
| 157 | else
|
---|
| 158 | {
|
---|
| 159 | tmp = a[k-1];
|
---|
| 160 | a[k-1] = a[t-1];
|
---|
| 161 | a[t-1] = tmp;
|
---|
| 162 | tmpi = b[k-1];
|
---|
| 163 | b[k-1] = b[t-1];
|
---|
| 164 | b[t-1] = tmpi;
|
---|
| 165 | t = k;
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 | i = i+1;
|
---|
| 169 | }
|
---|
| 170 | while( i<=n );
|
---|
| 171 | i = n-1;
|
---|
| 172 | do
|
---|
| 173 | {
|
---|
| 174 | tmp = a[i];
|
---|
| 175 | a[i] = a[0];
|
---|
| 176 | a[0] = tmp;
|
---|
| 177 | tmpi = b[i];
|
---|
| 178 | b[i] = b[0];
|
---|
| 179 | b[0] = tmpi;
|
---|
| 180 | t = 1;
|
---|
| 181 | while( t!=0 )
|
---|
| 182 | {
|
---|
| 183 | k = 2*t;
|
---|
| 184 | if( k>i )
|
---|
| 185 | {
|
---|
| 186 | t = 0;
|
---|
| 187 | }
|
---|
| 188 | else
|
---|
| 189 | {
|
---|
| 190 | if( k<i )
|
---|
| 191 | {
|
---|
| 192 | if( (double)(a[k])>(double)(a[k-1]) )
|
---|
| 193 | {
|
---|
| 194 | k = k+1;
|
---|
| 195 | }
|
---|
| 196 | }
|
---|
| 197 | if( (double)(a[t-1])>=(double)(a[k-1]) )
|
---|
| 198 | {
|
---|
| 199 | t = 0;
|
---|
| 200 | }
|
---|
| 201 | else
|
---|
| 202 | {
|
---|
| 203 | tmp = a[k-1];
|
---|
| 204 | a[k-1] = a[t-1];
|
---|
| 205 | a[t-1] = tmp;
|
---|
| 206 | tmpi = b[k-1];
|
---|
| 207 | b[k-1] = b[t-1];
|
---|
| 208 | b[t-1] = tmpi;
|
---|
| 209 | t = k;
|
---|
| 210 | }
|
---|
| 211 | }
|
---|
| 212 | }
|
---|
| 213 | i = i-1;
|
---|
| 214 | }
|
---|
| 215 | while( i>=1 );
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 |
|
---|
| 219 | public static void tagsortfastr(ref double[] a,
|
---|
| 220 | ref double[] b,
|
---|
| 221 | int n)
|
---|
| 222 | {
|
---|
| 223 | int i = 0;
|
---|
| 224 | int j = 0;
|
---|
| 225 | int k = 0;
|
---|
| 226 | int t = 0;
|
---|
| 227 | double tmp = 0;
|
---|
| 228 | double tmpr = 0;
|
---|
| 229 |
|
---|
| 230 |
|
---|
| 231 | //
|
---|
| 232 | // Special cases
|
---|
| 233 | //
|
---|
| 234 | if( n<=1 )
|
---|
| 235 | {
|
---|
| 236 | return;
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | //
|
---|
| 240 | // General case, N>1: sort, update B
|
---|
| 241 | //
|
---|
| 242 | i = 2;
|
---|
| 243 | do
|
---|
| 244 | {
|
---|
| 245 | t = i;
|
---|
| 246 | while( t!=1 )
|
---|
| 247 | {
|
---|
| 248 | k = t/2;
|
---|
| 249 | if( (double)(a[k-1])>=(double)(a[t-1]) )
|
---|
| 250 | {
|
---|
| 251 | t = 1;
|
---|
| 252 | }
|
---|
| 253 | else
|
---|
| 254 | {
|
---|
| 255 | tmp = a[k-1];
|
---|
| 256 | a[k-1] = a[t-1];
|
---|
| 257 | a[t-1] = tmp;
|
---|
| 258 | tmpr = b[k-1];
|
---|
| 259 | b[k-1] = b[t-1];
|
---|
| 260 | b[t-1] = tmpr;
|
---|
| 261 | t = k;
|
---|
| 262 | }
|
---|
| 263 | }
|
---|
| 264 | i = i+1;
|
---|
| 265 | }
|
---|
| 266 | while( i<=n );
|
---|
| 267 | i = n-1;
|
---|
| 268 | do
|
---|
| 269 | {
|
---|
| 270 | tmp = a[i];
|
---|
| 271 | a[i] = a[0];
|
---|
| 272 | a[0] = tmp;
|
---|
| 273 | tmpr = b[i];
|
---|
| 274 | b[i] = b[0];
|
---|
| 275 | b[0] = tmpr;
|
---|
| 276 | t = 1;
|
---|
| 277 | while( t!=0 )
|
---|
| 278 | {
|
---|
| 279 | k = 2*t;
|
---|
| 280 | if( k>i )
|
---|
| 281 | {
|
---|
| 282 | t = 0;
|
---|
| 283 | }
|
---|
| 284 | else
|
---|
| 285 | {
|
---|
| 286 | if( k<i )
|
---|
| 287 | {
|
---|
| 288 | if( (double)(a[k])>(double)(a[k-1]) )
|
---|
| 289 | {
|
---|
| 290 | k = k+1;
|
---|
| 291 | }
|
---|
| 292 | }
|
---|
| 293 | if( (double)(a[t-1])>=(double)(a[k-1]) )
|
---|
| 294 | {
|
---|
| 295 | t = 0;
|
---|
| 296 | }
|
---|
| 297 | else
|
---|
| 298 | {
|
---|
| 299 | tmp = a[k-1];
|
---|
| 300 | a[k-1] = a[t-1];
|
---|
| 301 | a[t-1] = tmp;
|
---|
| 302 | tmpr = b[k-1];
|
---|
| 303 | b[k-1] = b[t-1];
|
---|
| 304 | b[t-1] = tmpr;
|
---|
| 305 | t = k;
|
---|
| 306 | }
|
---|
| 307 | }
|
---|
| 308 | }
|
---|
| 309 | i = i-1;
|
---|
| 310 | }
|
---|
| 311 | while( i>=1 );
|
---|
| 312 | }
|
---|
| 313 |
|
---|
| 314 |
|
---|
| 315 | public static void tagsortfast(ref double[] a,
|
---|
| 316 | int n)
|
---|
| 317 | {
|
---|
| 318 | int i = 0;
|
---|
| 319 | int j = 0;
|
---|
| 320 | int k = 0;
|
---|
| 321 | int t = 0;
|
---|
| 322 | double tmp = 0;
|
---|
| 323 | double tmpr = 0;
|
---|
| 324 |
|
---|
| 325 |
|
---|
| 326 | //
|
---|
| 327 | // Special cases
|
---|
| 328 | //
|
---|
| 329 | if( n<=1 )
|
---|
| 330 | {
|
---|
| 331 | return;
|
---|
| 332 | }
|
---|
| 333 |
|
---|
| 334 | //
|
---|
| 335 | // General case, N>1: sort, update B
|
---|
| 336 | //
|
---|
| 337 | i = 2;
|
---|
| 338 | do
|
---|
| 339 | {
|
---|
| 340 | t = i;
|
---|
| 341 | while( t!=1 )
|
---|
| 342 | {
|
---|
| 343 | k = t/2;
|
---|
| 344 | if( (double)(a[k-1])>=(double)(a[t-1]) )
|
---|
| 345 | {
|
---|
| 346 | t = 1;
|
---|
| 347 | }
|
---|
| 348 | else
|
---|
| 349 | {
|
---|
| 350 | tmp = a[k-1];
|
---|
| 351 | a[k-1] = a[t-1];
|
---|
| 352 | a[t-1] = tmp;
|
---|
| 353 | t = k;
|
---|
| 354 | }
|
---|
| 355 | }
|
---|
| 356 | i = i+1;
|
---|
| 357 | }
|
---|
| 358 | while( i<=n );
|
---|
| 359 | i = n-1;
|
---|
| 360 | do
|
---|
| 361 | {
|
---|
| 362 | tmp = a[i];
|
---|
| 363 | a[i] = a[0];
|
---|
| 364 | a[0] = tmp;
|
---|
| 365 | t = 1;
|
---|
| 366 | while( t!=0 )
|
---|
| 367 | {
|
---|
| 368 | k = 2*t;
|
---|
| 369 | if( k>i )
|
---|
| 370 | {
|
---|
| 371 | t = 0;
|
---|
| 372 | }
|
---|
| 373 | else
|
---|
| 374 | {
|
---|
| 375 | if( k<i )
|
---|
| 376 | {
|
---|
| 377 | if( (double)(a[k])>(double)(a[k-1]) )
|
---|
| 378 | {
|
---|
| 379 | k = k+1;
|
---|
| 380 | }
|
---|
| 381 | }
|
---|
| 382 | if( (double)(a[t-1])>=(double)(a[k-1]) )
|
---|
| 383 | {
|
---|
| 384 | t = 0;
|
---|
| 385 | }
|
---|
| 386 | else
|
---|
| 387 | {
|
---|
| 388 | tmp = a[k-1];
|
---|
| 389 | a[k-1] = a[t-1];
|
---|
| 390 | a[t-1] = tmp;
|
---|
| 391 | t = k;
|
---|
| 392 | }
|
---|
| 393 | }
|
---|
| 394 | }
|
---|
| 395 | i = i-1;
|
---|
| 396 | }
|
---|
| 397 | while( i>=1 );
|
---|
| 398 | }
|
---|
| 399 | }
|
---|
| 400 | }
|
---|