1.package net.okren.java; 2.import java.util.*; 3.import java.io.*; 4. 5.public class SortTest{ 6. 7. //插入排序; 8. public static void insertionSort(Comparable[] data){ 9. System.out.println("insertSort() ..."); 10. int j; 11. for(int p = 1; p < data.length; p++){ 12. Comparable temp = data[p]; 13. for(j = p; j > 0 && temp.compareTo(data[j - 1]) < 0; j--){ 14. data[j] = data[j - 1]; 15. } 16. 17. data[j] = temp; 18. } 19. } 20. 21. 22. //希尔排序; 23. public static void shellSort(Comparable[] data){ 24. System.out.println("shellSort() ..."); 25. int j; 26. for(int gap = data.length / 2; gap > 0; gap /= 2){ 27. 28. for(int i = gap; i < data.length; i++){ 29. Comparable temp = data[i]; 30. for(j = i; j >= gap && temp.compareTo(data[j - gap]) < 0; j -= gap){ 31. data[j] = data[j - gap]; 32. } 33. data[j] = temp; 34. } 35. } 36. } 37. 38. 39. //堆排序; 40. public static void heapSort(Comparable[] data){ 41. System.out.println("heapSort() ..."); 42. //buildHeap, 43. for(int i = data.length / 2; i >= 0; i--){ 44. shiftDown(data, i, data.length); 45. 46. } 47. 48. //deleteMax, 49. for(int i = data.length - 1; i > 0; i--){ 50. swap(data, 0, i); 51. shiftDown(data, 0, i); 52. } 53. 54. } 55. 56. private static void shiftDown(Comparable[] data, int i, int n){ 57. int child; 58. Comparable temp; 59. for(temp = data[i]; leftChild(i) < n; i = child){ 60. child = leftChild(i); 61. if(child != n - 1 && data[child].compareTo(data[child + 1]) < 0){ 62. child++; 63. } 64. if(temp.compareTo(data[child]) < 0){ 65. data[i] = data[child]; 66. }else{ 67. break; 68. } 69. } 70. data[i] = temp; 71. } 72. 73. private static int leftChild(int i){ 74. return 2 * i + 1; 75. } 76. 77. 78. 79. 80. 81. 82. //冒泡排序 83. public static void bubbleSort(Comparable[] data){ 84. System.out.println("bubbleSort() ..."); 85. for(int i = data.length - 1; i > 0; i--){ 86. boolean isChanged = false; 87. for(int j = 0; j < i; j++){ 88. if(data[j].compareTo(data[j + 1]) > 0){ 89. Comparable temp = data[j]; 90. data[j] = data[j + 1]; 91. data[j + 1] = temp; 92. isChanged = true; 93. } 94. 95. } 96. if(!isChanged) break; 97. } 98. 99. } 100. 101. //快速排序; 102. 103. 104. public static void quickSort(Comparable[] data){ 105. System.out.println("quickSort() ..."); 106. 107. quickSort(data, 0, data.length - 1); 108. } 109. 110. private static void quickSort(Comparable[] data, int left, int right){ 111. if(left + 10 <= right){ 112. Comparable pivot = middle3(data, left, right);//执行三数中值分割法, 113. int i = left, j = right - 1; 114. for(;;){ 115. while(data[++i].compareTo(pivot) < 0){} 116. while(data[--j].compareTo(pivot) > 0){} 117. if(i < j){ 118. swap(data, i, j); 119. }else{ 120. break; 121. } 122. } 123. 124. swap(data, i, right - 1); 125. quickSort(data, left, i -1); 126. quickSort(data, i + 1, right); 127. 128. }else{ 129. System.out.println("insertionSort() ..."); 130. insertionSort(data); 131. } 132. } 133. 134. private static Comparable middle3(Comparable[] data, int left, int right){ 135. int center = (left + right) / 2; 136. if(data[center].compareTo(data[left]) < 0){ 137. swap(data, left, center); 138. } 139. if(data[right].compareTo(data[left]) < 0){ 140. swap(data, left, right); 141. } 142. if(data[right].compareTo(data[center]) < 0){ 143. swap(data, center, right); 144. } 145. swap(data, center, right - 1); 146. return data[right - 1]; 147. } 148. 149. private static void swap(Comparable[] data, int i, int j){ 150. Comparable temp = data[i]; 151. data[i] = data[j]; 152. data[j] = temp; 153. } 154. 155. //归并排序; 156. 157. public static void mergeSort(Comparable[] data){ 158. System.out.println("mergeSort() ..."); 159. Comparable[] tempData = new Comparable[data.length]; 160. mergeSort(data, tempData, 0, data.length - 1); 161. 162. 163. } 164. 165. 166. private static void mergeSort(Comparable[] data, Comparable[] tempData, int left, int right){ 167. 168. if(left < right){ 169. int center = (left + right) / 2; 170. mergeSort(data, tempData, left, center); 171. mergeSort(data, tempData, center + 1, right); 172. merge(data,tempData, left, center + 1, right); 173. } 174. } 175. 176. private static void merge(Comparable[] data, Comparable[] tempData, int leftPos, int rightPos, int rightEnd){ 177. int leftEnd = rightPos - 1; 178. int tempPos = leftPos; 179. int num = rightEnd - leftPos + 1; 180. 181. while(leftPos <= leftEnd && rightPos <= rightEnd){ 182. if(data[leftPos].compareTo(data[rightPos]) <= 0){ 183. tempData[tempPos++] = data[leftPos++]; 184. }else{ 185. tempData[tempPos++] = data[rightPos++]; 186. 187. } 188. } 189. 190. while(leftPos <= leftEnd){ 191. tempData[tempPos++] = data[leftPos++]; 192. } 193. 194. while(rightPos <= rightEnd){ 195. tempData[tempPos++] = data[rightPos++]; 196. } 197. 198. for(int i = 0; i < num; i++, rightEnd--){ 199. data[rightEnd] = tempData[rightEnd]; 200. } 201. } 202. 203. 204. // 构建初始数据 205. public static Comparable[] getData(int num){ 206. Comparable[] data = new Comparable[num]; 207. Random r = new Random(); 208. for(int i = 0; i < data.length; i++){ 209. data[i] = r.nextInt(num); 210. } 211. 212. return data; 213. } 214. 215. //从键盘输入 构建初始数据; 216. public static Comparable[] getInputData(){ 217. 218. 219. System.out.println("eg.: 3,6,1,2,7,2,6, ..."); 220. try{ 221. 222. BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 223. String line = in.readLine(); 224. StringTokenizer st = new StringTokenizer(line, ","); 225. Comparable[] data = new Comparable[st.countTokens()]; 226. int i = 0; 227. while(st.hasMoreTokens()){ 228. data[i++] = Integer.parseInt(st.nextToken()); 229. } 230. 231. return data; 232. 233. 234. }catch(IOException e){ 235. e.printStackTrace(); 236. return null; 237. } 238. 239. } 240. 241. 242. public static void main(String[] args){ 243. 244. //Comparable[] data = getData(20); 245. Comparable[] data = getInputData(); 246. //排序前输出 247. for(int i = 0; i < data.length; i++){ 248. System.out.print(data[i] + ", "); 249. } 250. System.out.println(); 251. //排序 252. //insertionSort(data); 253. //shellSort(data); 254. //bubbleSort(data); 255. //mergeSort(data); 256. //quickSort(data); 257. heapSort(data); 258. //排序后输出 259. for(int i = 0; i < data.length; i++){ 260. System.out.print(data[i] + ", "); 261. } 262. System.out.println(); 263. 264. } 265.}