home *** CD-ROM | disk | FTP | other *** search
/ Computer Life: Multimedia Mega Pac / Multimedia_Mega-Pac_Computer_Life_1996.iso / hotjava / demo / classes / qsortalg.jav < prev    next >
Text File  |  1995-05-19  |  2KB  |  63 lines

  1. /*
  2.  * @(#)QSortAlgorithm.java    1.6 95/01/31 James Gosling
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. /**
  21.  * A quick sort demonstration algorithm
  22.  * SortAlgorithm.java, Thu Oct 27 10:32:35 1994
  23.  *
  24.  * @author James Gosling
  25.  * @version     1.6, 31 Jan 1995
  26.  */
  27. class QSortAlgorithm extends SortAlgorithm {
  28.     void sort(int a[], int lo0, int hi0) {
  29.     int lo = lo0;
  30.     int hi = hi0;
  31.     pause(lo, hi);
  32.     if (lo >= hi) {
  33.         return;
  34.     }
  35.     int mid = a[(lo + hi) / 2];
  36.     while (lo < hi) {
  37.         while (lo<hi && a[lo] < mid) {
  38.         lo++;
  39.         }
  40.         while (lo<hi && a[hi] > mid) {
  41.         hi--;
  42.         }
  43.         if (lo < hi) {
  44.         int T = a[lo];
  45.         a[lo] = a[hi];
  46.         a[hi] = T;
  47.         pause();
  48.         }
  49.     }
  50.     if (hi < lo) {
  51.         int T = hi;
  52.         hi = lo;
  53.         lo = T;
  54.     }
  55.     sort(a, lo0, lo);
  56.     sort(a, lo == lo0 ? lo+1 : lo, hi0);
  57.     }
  58.  
  59.     void sort(int a[]) {
  60.     sort(a, 0, a.length-1);
  61.     }
  62. }
  63.