KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junit > runner > Sorter


1 package junit.runner;
2
3 import java.util.Vector JavaDoc;
4
5 /**
6  * A custom quick sort with support to customize the swap behaviour.
7  * NOTICE: We can't use the the sorting support from the JDK 1.2 collection
8  * classes because of the JDK 1.1.7 compatibility.
9  */

10 public class Sorter {
11     public static interface Swapper {
12         public void swap(Vector JavaDoc values, int left, int right);
13     }
14         
15     public static void sortStrings(Vector JavaDoc values , int left, int right, Swapper swapper) {
16         int oleft= left;
17         int oright= right;
18         String JavaDoc mid= (String JavaDoc)values.elementAt((left + right) / 2);
19         do {
20             while (((String JavaDoc)(values.elementAt(left))).compareTo(mid) < 0)
21                 left++;
22             while (mid.compareTo((String JavaDoc)(values.elementAt(right))) < 0)
23                 right--;
24             if (left <= right) {
25                 swapper.swap(values, left, right);
26                 left++;
27                 right--;
28             }
29         } while (left <= right);
30         
31         if (oleft < right)
32             sortStrings(values, oleft, right, swapper);
33         if (left < oright)
34              sortStrings(values, left, oright, swapper);
35     }
36 }
Popular Tags