1 2 12 package com.versant.core.util; 13 14 import java.util.Comparator ; 15 16 22 public class Quicksort { 23 24 private Quicksort() { 25 } 26 27 30 public static void quicksort(Object [] a, int size) { 31 quicksort(a, 0, size - 1); 32 } 33 34 37 public static void quicksort(Object [] a, int left, int right) { 38 int size = right - left + 1; 39 switch (size) { 40 case 0: 41 case 1: 42 break; 43 case 2: 44 if (compare(a[left], a[right]) > 0) swap(a, left, right); 45 break; 46 case 3: 47 if (compare(a[left], a[right - 1]) > 0) swap(a, left, right - 1); 48 if (compare(a[left], a[right]) > 0) swap(a, left, right); 49 if (compare(a[left + 1], a[right]) > 0) swap(a, left + 1, right); 50 break; 51 default: 52 int median = median(a, left, right); 53 int partition = partition(a, left, right, median); 54 quicksort(a, left, partition - 1); 55 quicksort(a, partition + 1, right); 56 } 57 } 58 59 private static int compare(Object a, Object b) { 60 if (a == null) { 61 return b == null ? 0 : -1; 62 } else if (b == null) { 63 return +1; 64 } else { 65 return ((Comparable )a).compareTo(b); 66 } 67 } 68 69 private static void swap(Object [] a, int left, int right) { 70 Object t = a[left]; 71 a[left] = a[right]; 72 a[right] = t; 73 } 74 75 private static int median(Object [] a, int left, int right) { 76 int center = (left + right) / 2; 77 if (compare(a[left], a[center]) > 0) swap(a, left, center); 78 if (compare(a[left], a[right]) > 0) swap(a, left, right); 79 if (compare(a[center], a[right]) > 0) swap(a, center, right); 80 swap(a, center, right - 1); 81 return right - 1; 82 } 83 84 private static int partition(Object [] a, int left, int right, int pivotIndex) { 85 int leftIndex = left; 86 int rightIndex = right - 1; 87 while (true) { 88 while (compare(a[++leftIndex], a[pivotIndex]) < 0); 89 while (compare(a[--rightIndex], a[pivotIndex]) > 0); 90 if (leftIndex >= rightIndex) { 91 break; } else { 93 swap(a, leftIndex, rightIndex); 94 } 95 } 96 swap(a, leftIndex, right - 1); return leftIndex; } 99 100 103 public static void quicksort(Object [] a, int size, Comparator c) { 104 quicksort(a, 0, size - 1, c); 105 } 106 107 110 public static void quicksort(Object [] a, int left, int right, Comparator c) { 111 int size = right - left + 1; 112 switch (size) { 113 case 0: 114 case 1: 115 break; 116 case 2: 117 if (c.compare(a[left], a[right]) > 0) swap(a, left, right); 118 break; 119 case 3: 120 if (c.compare(a[left], a[right - 1]) > 0) swap(a, left, right - 1); 121 if (c.compare(a[left], a[right]) > 0) swap(a, left, right); 122 if (c.compare(a[left + 1], a[right]) > 0) swap(a, left + 1, right); 123 break; 124 default: 125 int median = median(a, left, right, c); 126 int partition = partition(a, left, right, median, c); 127 quicksort(a, left, partition - 1, c); 128 quicksort(a, partition + 1, right, c); 129 } 130 } 131 132 private static int median(Object [] a, int left, int right, Comparator c) { 133 int center = (left + right) / 2; 134 if (c.compare(a[left], a[center]) > 0) swap(a, left, center); 135 if (c.compare(a[left], a[right]) > 0) swap(a, left, right); 136 if (c.compare(a[center], a[right]) > 0) swap(a, center, right); 137 swap(a, center, right - 1); 138 return right - 1; 139 } 140 141 private static int partition(Object [] a, int left, int right, 142 int pivotIndex, Comparator c) { 143 int leftIndex = left; 144 int rightIndex = right - 1; 145 while (true) { 146 while (c.compare(a[++leftIndex], a[pivotIndex]) < 0); 147 while (c.compare(a[--rightIndex], a[pivotIndex]) > 0); 148 if (leftIndex >= rightIndex) { 149 break; } else { 151 swap(a, leftIndex, rightIndex); 152 } 153 } 154 swap(a, leftIndex, right - 1); return leftIndex; } 157 158 } 159 | Popular Tags |