1 27 package org.htmlparser.util.sort; 28 29 import java.util.*; 30 31 42 public class Sort 43 { 44 48 private Sort () 49 { 50 } 51 52 64 public static void QuickSort (Vector v) throws ClassCastException 65 { 66 QuickSort (v, 0, v.size () - 1); 67 } 68 69 84 public static void QuickSort (Vector v, int lo0, int hi0) throws ClassCastException 85 { 86 int lo = lo0; 87 int hi = hi0; 88 Ordered mid; 89 90 if ( hi0 > lo0) 91 { mid = (Ordered)v.elementAt((lo0 + hi0) / 2); 93 94 while (lo <= hi) 96 { 97 while ((lo < hi0) && (0 > ((Ordered)v.elementAt (lo)).compare (mid))) 100 ++lo; 101 102 while ((hi > lo0) && (0 < ((Ordered)v.elementAt (hi)).compare (mid))) 105 --hi; 106 107 if (lo <= hi) 109 swap (v, lo++, hi--); 110 } 111 112 if (lo0 < hi) 115 QuickSort (v, lo0, hi); 116 117 if (lo < hi0) 120 QuickSort (v, lo, hi0); 121 } 122 } 123 124 private static void swap (Vector v, int i, int j) 125 { 126 Object o; 127 128 o = v.elementAt (i); 129 v.setElementAt (v.elementAt (j), i); 130 v.setElementAt (o, j); 131 } 132 133 144 public static void QuickSort (Ordered[] a) 145 { 146 QuickSort (a, 0, a.length - 1); 147 } 148 149 162 public static void QuickSort (Ordered[] a, int lo0, int hi0) 163 { 164 int lo = lo0; 165 int hi = hi0; 166 Ordered mid; 167 168 if ( hi0 > lo0) 169 { 170 mid = a[(lo0 + hi0) / 2]; 172 173 while (lo <= hi) 175 { 176 while ((lo < hi0) && (0 > a[lo].compare (mid))) 179 ++lo; 180 181 while ((hi > lo0) && (0 < a[hi].compare (mid))) 184 --hi; 185 186 if (lo <= hi) 188 swap (a, lo++, hi--); 189 } 190 191 if (lo0 < hi) 194 QuickSort (a, lo0, hi); 195 196 if (lo < hi0) 199 QuickSort (a, lo, hi0); 200 } 201 } 202 203 209 private static void swap (Object [] a, int i, int j) 210 { 211 Object o; 212 o = a[i]; 213 a[i] = a[j]; 214 a[j] = o; 215 } 216 217 228 public static void QuickSort (String [] a) 229 { 230 QuickSort (a, 0, a.length - 1); 231 } 232 233 246 public static void QuickSort (String [] a, int lo0, int hi0) 247 { 248 int lo = lo0; 249 int hi = hi0; 250 String mid; 251 252 if ( hi0 > lo0) 253 { 254 mid = a[(lo0 + hi0) / 2]; 256 257 while (lo <= hi) 259 { 260 while ((lo < hi0) && (0 > a[lo].compareTo (mid))) 263 ++lo; 264 265 while ((hi > lo0) && (0 < a[hi].compareTo (mid))) 268 --hi; 269 270 if (lo <= hi) 272 swap (a, lo++, hi--); 273 } 274 275 if (lo0 < hi) 278 QuickSort (a, lo0, hi); 279 280 if (lo < hi0) 283 QuickSort (a, lo, hi0); 284 } 285 } 286 287 296 public static void QuickSort (Sortable sortable, int lo0, int hi0) 297 { 298 int lo = lo0; 299 int hi = hi0; 300 Ordered mid; 301 Ordered test; 302 303 if ( hi0 > lo0) 304 { mid = sortable.fetch ((lo0 + hi0) / 2, null); 306 test = null; 307 308 while (lo <= hi) 310 { 311 while ((lo < hi0) && (0 > (test = sortable.fetch (lo, test)).compare (mid))) 314 ++lo; 315 316 while ((hi > lo0) && (0 < (test = sortable.fetch (hi, test)).compare (mid))) 319 --hi; 320 321 if (lo <= hi) 323 sortable.swap (lo++, hi--); 324 } 325 326 if (lo0 < hi) 329 QuickSort (sortable, lo0, hi); 330 331 if (lo < hi0) 334 QuickSort (sortable, lo, hi0); 335 } 336 } 337 338 349 public static void QuickSort (Sortable sortable) 350 { 351 QuickSort (sortable, sortable.first (), sortable.last ()); 352 } 353 354 361 public static Object [] QuickSort (Hashtable h) throws ClassCastException 362 { 363 Enumeration e; 364 boolean are_strings; 365 Object [] ret; 366 367 ret = new Ordered[h.size ()]; 369 e = h.keys (); 370 are_strings = true; for (int i = 0; i < ret.length; i++) 372 { 373 ret[i] = e.nextElement (); 374 if (are_strings && !(ret[i] instanceof String )) 375 are_strings = false; 376 } 377 378 if (are_strings) 380 QuickSort ((String [])ret); 381 else 382 QuickSort ((Ordered[])ret); 383 384 return (ret); 385 } 386 387 395 public static int bsearch (Sortable set, Ordered ref, int lo, int hi) 396 { int num; 397 int mid; 398 Ordered ordered; 399 int half; 400 int result; 401 int ret; 402 403 ret = -1; 404 405 num = (hi - lo) + 1; 406 ordered = null; 407 while ((-1 == ret) && (lo <= hi)) 408 { 409 half = num / 2; 410 mid = lo + ((0 != (num & 1)) ? half : half - 1); 411 ordered = set.fetch (mid, ordered); 412 result = ref.compare (ordered); 413 if (0 == result) 414 ret = mid; 415 else if (0 > result) 416 { 417 hi = mid - 1; 418 num = ((0 != (num & 1)) ? half : half - 1); 419 } 420 else 421 { 422 lo = mid + 1; 423 num = half; 424 } 425 } 426 if (-1 == ret) 427 ret = lo; 428 429 return (ret); 430 } 431 432 438 public static int bsearch (Sortable set, Ordered ref) 439 { 440 return (bsearch (set, ref, set.first (), set.last ())); 441 } 442 443 451 public static int bsearch (Vector vector, Ordered ref, int lo, int hi) 452 { int num; 453 int mid; 454 int half; 455 int result; 456 int ret; 457 458 ret = -1; 459 460 num = (hi - lo) + 1; 461 while ((-1 == ret) && (lo <= hi)) 462 { 463 half = num / 2; 464 mid = lo + ((0 != (num & 1)) ? half : half - 1); 465 result = ref.compare (vector.elementAt (mid)); 466 if (0 == result) 467 ret = mid; 468 else if (0 > result) 469 { 470 hi = mid - 1; 471 num = ((0 != (num & 1)) ? half : half - 1); 472 } 473 else 474 { 475 lo = mid + 1; 476 num = half; 477 } 478 } 479 if (-1 == ret) 480 ret = lo; 481 482 return (ret); 483 } 484 485 491 public static int bsearch (Vector vector, Ordered ref) 492 { 493 return (bsearch (vector, ref, 0, vector.size () - 1)); 494 } 495 496 504 public static int bsearch (Ordered[] array, Ordered ref, int lo, int hi) 505 { int num; 506 int mid; 507 int half; 508 int result; 509 int ret; 510 511 ret = -1; 512 513 num = (hi - lo) + 1; 514 while ((-1 == ret) && (lo <= hi)) 515 { 516 half = num / 2; 517 mid = lo + ((0 != (num & 1)) ? half : half - 1); 518 result = ref.compare (array[mid]); 519 if (0 == result) 520 ret = mid; 521 else if (0 > result) 522 { 523 hi = mid - 1; 524 num = ((0 != (num & 1)) ? half : half - 1); 525 } 526 else 527 { 528 lo = mid + 1; 529 num = half; 530 } 531 } 532 if (-1 == ret) 533 ret = lo; 534 535 return (ret); 536 } 537 538 544 public static int bsearch (Ordered[] array, Ordered ref) 545 { 546 return (bsearch (array, ref, 0, array.length - 1)); 547 } 548 } 549 550 | Popular Tags |