1 11 package org.eclipse.team.internal.core; 12 13 19 public abstract class Sorter { 20 27 public abstract boolean compare(Object elementOne, Object elementTwo); 28 31 private Object [] quickSort(Object [] sortedCollection, int left, int right) { 32 int originalLeft = left; 33 int originalRight = right; 34 Object mid = sortedCollection[ (left + right) / 2]; 35 do { 36 while (compare(sortedCollection[left], mid)) 37 left++; 38 while (compare(mid, sortedCollection[right])) 39 right--; 40 if (left <= right) { 41 Object tmp = sortedCollection[left]; 42 sortedCollection[left] = sortedCollection[right]; 43 sortedCollection[right] = tmp; 44 left++; 45 right--; 46 } 47 } while (left <= right); 48 if (originalLeft < right) 49 sortedCollection = quickSort(sortedCollection, originalLeft, right); 50 if (left < originalRight) 51 sortedCollection = quickSort(sortedCollection, left, originalRight); 52 return sortedCollection; 53 } 54 58 public Object [] sort(Object [] unSortedCollection) { 59 int size = unSortedCollection.length; 60 Object [] sortedCollection = new Object [size]; 61 System.arraycopy(unSortedCollection, 0, sortedCollection, 0, size); 63 if (size > 1) 64 quickSort(sortedCollection, 0, size - 1); 65 return sortedCollection; 66 } 67 } 68 | Popular Tags |