home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / java / io / example-1.1 / SortThread.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  2.7 KB  |  94 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.io.*;
  18.  
  19. class SortThread extends Thread {
  20.     PrintWriter pw;
  21.     BufferedReader br;
  22.  
  23.     SortThread(PrintWriter pw, BufferedReader br) {
  24.         this.pw = pw;
  25.         this.br = br;
  26.     }
  27.  
  28.     public void run() {
  29.          int MAXWORDS = 50;
  30.  
  31.         if (pw != null && br != null) {
  32.             try {
  33.                 String[] listOfWords = new String[MAXWORDS];
  34.                 int numwords = 0, i = 0;
  35.  
  36.                 while ((listOfWords[numwords] = br.readLine()) != null) {
  37.                     numwords++;
  38.                 }
  39.                 quicksort(listOfWords, 0, numwords-1);
  40.                 for (i = 0; i < numwords; i++) {
  41.                     pw.println(listOfWords[i]);
  42.                 }
  43.                 pw.close();
  44.             } catch (IOException e) {
  45.                 System.err.println("WriteReversedThread run: " + e);
  46.             }
  47.         }
  48.     }
  49.  
  50.     protected void finalize() {
  51.         try {
  52.             if (pw != null) {
  53.                 pw.close();
  54.                 pw = null;
  55.             }
  56.             if (br != null) {
  57.                 br.close();
  58.                 br = null;
  59.             }
  60.         } catch (IOException e) {
  61.             System.err.println("WriteReversedThread finalize: " + e);
  62.         }
  63.     }
  64.  
  65.     private static void quicksort(String[] a, int lo0, int hi0) {
  66.         int lo = lo0;
  67.         int hi = hi0;
  68.         if (lo >= hi) {
  69.             return;
  70.         }
  71.         String mid = a[(lo + hi) / 2];
  72.         while (lo < hi) {
  73.             while (lo<hi && a[lo].compareTo(mid) < 0) {
  74.                 lo++;
  75.             }
  76.             while (lo<hi && a[hi].compareTo(mid) > 0) {
  77.                 hi--;
  78.             }
  79.             if (lo < hi) {
  80.                 String T = a[lo];
  81.                 a[lo] = a[hi];
  82.                 a[hi] = T;
  83.             }
  84.         }
  85.         if (hi < lo) {
  86.             int T = hi;
  87.             hi = lo;
  88.             lo = T;
  89.         }
  90.         quicksort(a, lo0, lo);
  91.         quicksort(a, lo == lo0 ? lo+1 : lo, hi0);
  92.     }
  93. }
  94.