1 27 28 package com.adventnet.jmx.utils; 29 30 42 public class Sorter implements java.io.Serializable 43 { 44 45 46 60 public void QuickSort(String a[], int lo0, int hi0) throws Exception 61 { 62 int lo = lo0; 63 int hi = hi0; 64 String mid; 65 66 if ( hi0 > lo0) 67 { 68 69 72 mid = a[ ( lo0 + hi0 ) / 2 ]; 73 74 while( lo <= hi ) 76 { 77 80 while( ( lo < hi0 ) && (compareTo(a[lo], mid) < 0)) 81 ++lo; 82 83 86 while( ( hi > lo0 ) && (compareTo(a[hi], mid) > 0)) 87 --hi; 88 89 if( lo <= hi ) 91 { 92 swap(a, lo, hi); 93 ++lo; 94 --hi; 95 } 96 } 97 98 101 if( lo0 < hi ) 102 QuickSort( a, lo0, hi ); 103 104 107 if( lo < hi0 ) 108 QuickSort( a, lo, hi0 ); 109 110 } 111 } 112 113 118 119 private int compareTo(String str1, String str2) 120 { 121 int ret = str1.compareTo(str2); 122 123 if(ret > 0) 124 return 1; 125 if(ret < 0) 126 return -1; 127 return 0; 128 } 129 130 private void swap(String a[], int i, int j) 131 { 132 Object T; 133 T = a[i]; 134 a[i] = a[j]; 135 a[j] = (String )T; 136 137 if(obj != null) 138 { 139 140 int max = obj.length; 141 142 for( int pos = 0; pos < max; pos++) 143 { 144 Object [] b = obj[pos]; 145 T = b[i]; 146 b[i] = b[j]; 147 b[j] = T; 148 } 149 } 150 151 152 } 153 154 Object [][] obj = null; 155 156 164 public void sort(String a[], Object [][] obj) throws Exception 165 { 166 this.obj = obj; 167 QuickSort(a, 0, a.length - 1); 168 } 169 170 171 185 public void QuickSort(int a[], int lo0, int hi0) throws Exception 186 { 187 int lo = lo0; 188 int hi = hi0; 189 int mid; 190 191 if ( hi0 > lo0) 192 { 193 194 197 mid = a[ ( lo0 + hi0 ) / 2 ]; 198 199 while( lo <= hi ) 201 { 202 205 while( ( lo < hi0 ) && ( a[lo] < mid )) 206 ++lo; 207 208 211 while( ( hi > lo0 ) && ( a[hi] > mid )) 212 --hi; 213 214 if( lo <= hi ) 216 { 217 swap(a, lo, hi); 218 ++lo; 219 --hi; 220 } 221 } 222 223 226 if( lo0 < hi ) 227 QuickSort( a, lo0, hi ); 228 229 232 if( lo < hi0 ) 233 QuickSort( a, lo, hi0 ); 234 235 } 236 } 237 238 private void swap(int a[], int i, int j) 239 { 240 int T; 241 T = a[i]; 242 a[i] = a[j]; 243 a[j] = T; 244 245 } 246 247 public void sort(int a[]) throws Exception 248 { 249 QuickSort(a, 0, a.length - 1); 250 } 251 } | Popular Tags |