1 package org.apache.turbine.util; 2 3 18 19 29 public class QuickSort 30 { 31 39 public static void quickSort(Object s[], 40 int lo, 41 int hi, 42 Comparable cmp) 43 { 44 if (lo >= hi) 45 return; 46 47 51 int mid = (lo + hi) / 2; 52 53 if (cmp.compare(s[lo], s[mid]) > 0) 54 { 55 Object tmp = s[lo]; 57 s[lo] = s[mid]; 58 s[mid] = tmp; 59 } 60 61 if (cmp.compare(s[mid], s[hi]) > 0) 62 { 63 Object tmp = s[mid]; 65 s[mid] = s[hi]; 66 s[hi] = tmp; 67 68 if (cmp.compare(s[lo], s[mid]) > 0) 69 { 70 Object tmp2 = s[lo]; 72 s[lo] = s[mid]; 73 s[mid] = tmp2; 74 } 75 } 76 77 int left = lo + 1; 79 80 int right = hi - 1; 82 83 if (left >= right) 85 return; 86 87 Object partition = s[mid]; 88 89 for (; ;) 90 { 91 while (cmp.compare(s[right], partition) > 0) 92 --right; 93 94 while (left < right && 95 cmp.compare(s[left], partition) <= 0) 96 ++left; 97 98 if (left < right) 99 { 100 Object tmp = s[left]; 102 s[left] = s[right]; 103 s[right] = tmp; 104 105 --right; 106 } 107 else 108 break; 109 } 110 quickSort(s, lo, left, cmp); 111 quickSort(s, left + 1, hi, cmp); 112 } 113 114 120 public void sort(Object [] data, 121 Comparable cmp) 122 { 123 QuickSort.quickSort(data, 124 0, 125 data.length - 1, 126 cmp); 127 } 128 } 129 | Popular Tags |