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