KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > trading > client > util > QuickSort


1
2 // Copyright (C) 1998-1999
3
// Object Oriented Concepts, Inc.
4

5 // **********************************************************************
6
//
7
// Copyright (c) 1997
8
// Mark Spruiell (mark@intellisoft.com)
9
//
10
// See the COPYING file for more information
11
//
12
// **********************************************************************
13

14 package org.jacorb.trading.client.util;
15
16
17 public class QuickSort
18 {
19   private QuickSort()
20   {
21   }
22
23
24   public static void sort(String JavaDoc[] arr)
25   {
26     sortImpl(arr, 0, arr.length - 1);
27   }
28
29
30   protected static void sortImpl(String JavaDoc[] arr, int l, int h)
31   {
32     int low = l;
33     int hi = h;
34     String JavaDoc mid;
35
36     if (h > l) {
37       mid = arr[(l + h) / 2];
38
39       while (low <= hi) {
40         while (low < h && arr[low].compareTo(mid) < 0)
41           low += 1;
42
43         while (hi > l && arr[hi].compareTo(mid) > 0)
44           hi -= 1;
45
46         if (low <= hi) {
47           String JavaDoc tmp = arr[low];
48           arr[low] = arr[hi];
49           arr[hi] = tmp;
50
51           low += 1;
52           hi -= 1;
53         }
54       }
55
56       if (l < hi)
57         sortImpl(arr, l, hi);
58
59       if (low < h)
60         sortImpl(arr, low, h);
61     }
62   }
63 }
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
Popular Tags