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