1. int[] nums = {1,24,6,7,23,6,2,4,7,9,3,53,123}
    2. Arrays.sort(nums);

    方法调用后,会进入DualPivotQuicksort.sort(nums, 0, nums.length - 1, null, 0, 0);方法,源码如下:

    1. static void sort(int[] a, int left, int right,
    2. int[] work, int workBase, int workLen) {
    3. // Use Quicksort on small arrays
    4. // 快排的阈值是 286, 数组长度低于 286 直接使用快排
    5. if (right - left < QUICKSORT_THRESHOLD) {
    6. sort(a, left, right, true);
    7. return;
    8. }
    9. /*
    10. * Index run[i] is the start of i-th run
    11. * (ascending or descending sequence).
    12. */
    13. // 记录整个数组中有序部分的起点位置
    14. int[] run = new int[MAX_RUN_COUNT + 1];
    15. int count = 0; run[0] = left;
    16. // Check if the array is nearly sorted
    17. // 遍历整个数组, 令数组变的基本有序
    18. for (int k = left; k < right; run[count] = k) {
    19. if (a[k] < a[k + 1]) { // ascending
    20. while (++k <= right && a[k - 1] <= a[k]);
    21. } else if (a[k] > a[k + 1]) { // descending
    22. while (++k <= right && a[k - 1] >= a[k]);
    23. for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) {
    24. int t = a[lo]; a[lo] = a[hi]; a[hi] = t;
    25. }
    26. } else { // equal
    27. for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {
    28. if (--m == 0) {
    29. sort(a, left, right, true);
    30. return;
    31. }
    32. }
    33. }
    34. /*
    35. * The array is not highly structured,
    36. * use Quicksort instead of merge sort.
    37. */
    38. // 若有序度比较低, 就直接进行快排
    39. if (++count == MAX_RUN_COUNT) {
    40. sort(a, left, right, true);
    41. return;
    42. }
    43. }
    44. // Check special cases
    45. // Implementation note: variable "right" is increased by 1.
    46. // 最后一组的时候超出
    47. if (run[count] == right++) { // The last run contains one element
    48. run[++count] = right;
    49. } else if (count == 1) { // The array is already sorted
    50. return;
    51. }
    52. // Determine alternation base for merge
    53. byte odd = 0;
    54. for (int n = 1; (n <<= 1) < count; odd ^= 1);
    55. // Use or create temporary array b for merging
    56. int[] b; // temp array; alternates with a
    57. int ao, bo; // array offsets from 'left'
    58. int blen = right - left; // space needed for b
    59. if (work == null || workLen < blen || workBase + blen > work.length) {
    60. work = new int[blen];
    61. workBase = 0;
    62. }
    63. if (odd == 0) {
    64. System.arraycopy(a, left, work, workBase, blen);
    65. b = a;
    66. bo = 0;
    67. a = work;
    68. ao = workBase - left;
    69. } else {
    70. b = work;
    71. ao = 0;
    72. bo = workBase - left;
    73. }
    74. // Merging
    75. for (int last; count > 1; count = last) {
    76. for (int k = (last = 0) + 2; k <= count; k += 2) {
    77. int hi = run[k], mi = run[k - 1];
    78. for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) {
    79. if (q >= hi || p < mi && a[p + ao] <= a[q + ao]) {
    80. b[i + bo] = a[p++ + ao];
    81. } else {
    82. b[i + bo] = a[q++ + ao];
    83. }
    84. }
    85. run[++last] = hi;
    86. }
    87. if ((count & 1) != 0) {
    88. for (int i = right, lo = run[count - 1]; --i >= lo;
    89. b[i + bo] = a[i + ao]
    90. );
    91. run[++last] = right;
    92. }
    93. int[] t = a; a = b; b = t;
    94. int o = ao; ao = bo; bo = o;
    95. }
    96. }

    快速排序源码:

    1. private static void sort(int[] a, int left, int right, boolean leftmost) {
    2. int length = right - left + 1;
    3. // Use insertion sort on tiny arrays
    4. // 长度太小的话就用插入排序
    5. if (length < INSERTION_SORT_THRESHOLD) {
    6. if (leftmost) {
    7. /*
    8. * Traditional (without sentinel) insertion sort,
    9. * optimized for server VM, is used in case of
    10. * the leftmost part.
    11. */
    12. for (int i = left, j = i; i < right; j = ++i) {
    13. int ai = a[i + 1];
    14. while (ai < a[j]) {
    15. a[j + 1] = a[j];
    16. if (j-- == left) {
    17. break;
    18. }
    19. }
    20. a[j + 1] = ai;
    21. }
    22. } else {
    23. /*
    24. * Skip the longest ascending sequence.
    25. */
    26. do {
    27. if (left >= right) {
    28. return;
    29. }
    30. } while (a[++left] >= a[left - 1]);
    31. /*
    32. * Every element from adjoining part plays the role
    33. * of sentinel, therefore this allows us to avoid the
    34. * left range check on each iteration. Moreover, we use
    35. * the more optimized algorithm, so called pair insertion
    36. * sort, which is faster (in the context of Quicksort)
    37. * than traditional implementation of insertion sort.
    38. */
    39. for (int k = left; ++left <= right; k = ++left) {
    40. int a1 = a[k], a2 = a[left];
    41. if (a1 < a2) {
    42. a2 = a1; a1 = a[left];
    43. }
    44. while (a1 < a[--k]) {
    45. a[k + 2] = a[k];
    46. }
    47. a[++k + 1] = a1;
    48. while (a2 < a[--k]) {
    49. a[k + 1] = a[k];
    50. }
    51. a[k + 1] = a2;
    52. }
    53. int last = a[right];
    54. while (last < a[--right]) {
    55. a[right + 1] = a[right];
    56. }
    57. a[right + 1] = last;
    58. }
    59. return;
    60. }
    61. // Inexpensive approximation of length / 7
    62. int seventh = (length >> 3) + (length >> 6) + 1;
    63. /*
    64. * Sort five evenly spaced elements around (and including) the
    65. * center element in the range. These elements will be used for
    66. * pivot selection as described below. The choice for spacing
    67. * these elements was empirically determined to work well on
    68. * a wide variety of inputs.
    69. */
    70. int e3 = (left + right) >>> 1; // The midpoint
    71. int e2 = e3 - seventh;
    72. int e1 = e2 - seventh;
    73. int e4 = e3 + seventh;
    74. int e5 = e4 + seventh;
    75. // Sort these elements using insertion sort
    76. if (a[e2] < a[e1]) { int t = a[e2]; a[e2] = a[e1]; a[e1] = t; }
    77. if (a[e3] < a[e2]) { int t = a[e3]; a[e3] = a[e2]; a[e2] = t;
    78. if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
    79. }
    80. if (a[e4] < a[e3]) { int t = a[e4]; a[e4] = a[e3]; a[e3] = t;
    81. if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
    82. if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
    83. }
    84. }
    85. if (a[e5] < a[e4]) { int t = a[e5]; a[e5] = a[e4]; a[e4] = t;
    86. if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t;
    87. if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
    88. if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
    89. }
    90. }
    91. }
    92. // Pointers
    93. int less = left; // The index of the first element of center part
    94. int great = right; // The index before the first element of right part
    95. if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) {
    96. /*
    97. * Use the second and fourth of the five sorted elements as pivots.
    98. * These values are inexpensive approximations of the first and
    99. * second terciles of the array. Note that pivot1 <= pivot2.
    100. */
    101. int pivot1 = a[e2];
    102. int pivot2 = a[e4];
    103. /*
    104. * The first and the last elements to be sorted are moved to the
    105. * locations formerly occupied by the pivots. When partitioning
    106. * is complete, the pivots are swapped back into their final
    107. * positions, and excluded from subsequent sorting.
    108. */
    109. a[e2] = a[left];
    110. a[e4] = a[right];
    111. /*
    112. * Skip elements, which are less or greater than pivot values.
    113. */
    114. while (a[++less] < pivot1);
    115. while (a[--great] > pivot2);
    116. /*
    117. * Partitioning:
    118. *
    119. * left part center part right part
    120. * +--------------------------------------------------------------+
    121. * | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 |
    122. * +--------------------------------------------------------------+
    123. * ^ ^ ^
    124. * | | |
    125. * less k great
    126. *
    127. * Invariants:
    128. *
    129. * all in (left, less) < pivot1
    130. * pivot1 <= all in [less, k) <= pivot2
    131. * all in (great, right) > pivot2
    132. *
    133. * Pointer k is the first index of ?-part.
    134. */
    135. outer:
    136. for (int k = less - 1; ++k <= great; ) {
    137. int ak = a[k];
    138. if (ak < pivot1) { // Move a[k] to left part
    139. a[k] = a[less];
    140. /*
    141. * Here and below we use "a[i] = b; i++;" instead
    142. * of "a[i++] = b;" due to performance issue.
    143. */
    144. a[less] = ak;
    145. ++less;
    146. } else if (ak > pivot2) { // Move a[k] to right part
    147. while (a[great] > pivot2) {
    148. if (great-- == k) {
    149. break outer;
    150. }
    151. }
    152. if (a[great] < pivot1) { // a[great] <= pivot2
    153. a[k] = a[less];
    154. a[less] = a[great];
    155. ++less;
    156. } else { // pivot1 <= a[great] <= pivot2
    157. a[k] = a[great];
    158. }
    159. /*
    160. * Here and below we use "a[i] = b; i--;" instead
    161. * of "a[i--] = b;" due to performance issue.
    162. */
    163. a[great] = ak;
    164. --great;
    165. }
    166. }
    167. // Swap pivots into their final positions
    168. a[left] = a[less - 1]; a[less - 1] = pivot1;
    169. a[right] = a[great + 1]; a[great + 1] = pivot2;
    170. // Sort left and right parts recursively, excluding known pivots
    171. sort(a, left, less - 2, leftmost);
    172. sort(a, great + 2, right, false);
    173. /*
    174. * If center part is too large (comprises > 4/7 of the array),
    175. * swap internal pivot values to ends.
    176. */
    177. if (less < e1 && e5 < great) {
    178. /*
    179. * Skip elements, which are equal to pivot values.
    180. */
    181. while (a[less] == pivot1) {
    182. ++less;
    183. }
    184. while (a[great] == pivot2) {
    185. --great;
    186. }
    187. /*
    188. * Partitioning:
    189. *
    190. * left part center part right part
    191. * +----------------------------------------------------------+
    192. * | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 |
    193. * +----------------------------------------------------------+
    194. * ^ ^ ^
    195. * | | |
    196. * less k great
    197. *
    198. * Invariants:
    199. *
    200. * all in (*, less) == pivot1
    201. * pivot1 < all in [less, k) < pivot2
    202. * all in (great, *) == pivot2
    203. *
    204. * Pointer k is the first index of ?-part.
    205. */
    206. outer:
    207. for (int k = less - 1; ++k <= great; ) {
    208. int ak = a[k];
    209. if (ak == pivot1) { // Move a[k] to left part
    210. a[k] = a[less];
    211. a[less] = ak;
    212. ++less;
    213. } else if (ak == pivot2) { // Move a[k] to right part
    214. while (a[great] == pivot2) {
    215. if (great-- == k) {
    216. break outer;
    217. }
    218. }
    219. if (a[great] == pivot1) { // a[great] < pivot2
    220. a[k] = a[less];
    221. /*
    222. * Even though a[great] equals to pivot1, the
    223. * assignment a[less] = pivot1 may be incorrect,
    224. * if a[great] and pivot1 are floating-point zeros
    225. * of different signs. Therefore in float and
    226. * double sorting methods we have to use more
    227. * accurate assignment a[less] = a[great].
    228. */
    229. a[less] = pivot1;
    230. ++less;
    231. } else { // pivot1 < a[great] < pivot2
    232. a[k] = a[great];
    233. }
    234. a[great] = ak;
    235. --great;
    236. }
    237. }
    238. }
    239. // Sort center part recursively
    240. sort(a, less, great, false);
    241. } else { // Partitioning with one pivot
    242. /*
    243. * Use the third of the five sorted elements as pivot.
    244. * This value is inexpensive approximation of the median.
    245. */
    246. int pivot = a[e3];
    247. /*
    248. * Partitioning degenerates to the traditional 3-way
    249. * (or "Dutch National Flag") schema:
    250. *
    251. * left part center part right part
    252. * +-------------------------------------------------+
    253. * | < pivot | == pivot | ? | > pivot |
    254. * +-------------------------------------------------+
    255. * ^ ^ ^
    256. * | | |
    257. * less k great
    258. *
    259. * Invariants:
    260. *
    261. * all in (left, less) < pivot
    262. * all in [less, k) == pivot
    263. * all in (great, right) > pivot
    264. *
    265. * Pointer k is the first index of ?-part.
    266. */
    267. for (int k = less; k <= great; ++k) {
    268. if (a[k] == pivot) {
    269. continue;
    270. }
    271. int ak = a[k];
    272. if (ak < pivot) { // Move a[k] to left part
    273. a[k] = a[less];
    274. a[less] = ak;
    275. ++less;
    276. } else { // a[k] > pivot - Move a[k] to right part
    277. while (a[great] > pivot) {
    278. --great;
    279. }
    280. if (a[great] < pivot) { // a[great] <= pivot
    281. a[k] = a[less];
    282. a[less] = a[great];
    283. ++less;
    284. } else { // a[great] == pivot
    285. /*
    286. * Even though a[great] equals to pivot, the
    287. * assignment a[k] = pivot may be incorrect,
    288. * if a[great] and pivot are floating-point
    289. * zeros of different signs. Therefore in float
    290. * and double sorting methods we have to use
    291. * more accurate assignment a[k] = a[great].
    292. */
    293. a[k] = pivot;
    294. }
    295. a[great] = ak;
    296. --great;
    297. }
    298. }
    299. /*
    300. * Sort left and right parts recursively.
    301. * All elements from center part are equal
    302. * and, therefore, already sorted.
    303. */
    304. sort(a, left, less - 1, leftmost);
    305. sort(a, great + 1, right, false);
    306. }