¸üÐÂʱ¼ä:2019Äê04ÔÂ17ÈÕ15ʱ08·Ö À´Ô´:javaÅàѵ»ú¹¹ÀÖÓã²¥¿Í ä¯ÀÀ´ÎÊý:
¡¡¡¡ÉîÈëÀí½âJava ÖеÄArrays.sort()·½·¨
¡¡¡¡JavaµÄArraysÀàÖÐÓÐÒ»¸ösort()·½·¨£¬¸Ã·½·¨ÊÇArraysÀàµÄ¾²Ì¬·½·¨£¬ÔÚÐèÒª¶ÔÊý×é½øÐÐÅÅÐòʱ£¬·Ç³£µÄºÃÓá£
¡¡¡¡µ«ÊÇsort()µÄ²ÎÊýÓкü¸ÖÖ£¬»ù±¾ÉÏÊÇ´óͬСÒ죬ÏÂÃæÊÇÒÔintÐÍÊý×éΪÀýµÄArrays.sort()µÄµäÐÍÓ÷¨
¡¡¡¡import java.util.Arrays;
¡¡¡¡import java.util.Comparator;
¡¡¡¡/**
¡¡¡¡* Arrays.sort()ÅÅÐò
¡¡¡¡*/
¡¡¡¡public class SortTest
¡¡¡¡{
¡¡¡¡public static void main(String []args)
¡¡¡¡{
¡¡¡¡int[] ints=new int[]{2,324,4,57,1};
¡¡¡¡System.out.println("ÔöÐòÅÅÐòºó˳Ðò");
¡¡¡¡Arrays.sort(ints);
¡¡¡¡for (int i=0;i
¡¡¡¡{
¡¡¡¡System.out.print(ints[i]+" ");
¡¡¡¡}
¡¡¡¡System.out.println("\n¼õÐòÅÅÐòºó˳Ðò");
¡¡¡¡//ҪʵÏÖ¼õÐòÅÅÐò£¬µÃͨ¹ý°ü×°ÀàÐÍÊý×飬»ù±¾ÀàÐÍÊý×éÊDz»ÐеÎ
¡¡¡¡Integer[] integers=new Integer[]{2,324,4,4,6,1};
¡¡¡¡Arrays.sort(integers, new Comparator()
¡¡¡¡{
¡¡¡¡/*
¡¡¡¡* ´Ë´¦Óëc++µÄ±È½Ïº¯Êý¹¹³É²»Ò»ÖÂ
¡¡¡¡* c++·µ»ØboolÐÍ,¶øJava·µ»ØµÄΪintÐÍ
¡¡¡¡* µ±·µ»ØÖµ>0ʱ
¡¡¡¡* ½øÐн»»»£¬¼´ÅÅÐò(Ô´ÂëʵÏÖΪÁ½ÊàÖá¿ìËÙÅÅÐò)
¡¡¡¡*/
¡¡¡¡public int compare(Integer o1, Integer o2)
¡¡¡¡{
¡¡¡¡return o2-o1;
¡¡¡¡}
¡¡¡¡public boolean equals(Object obj)
¡¡¡¡{
¡¡¡¡return false;
¡¡¡¡}
¡¡¡¡});
¡¡¡¡for (Integer integer:integers)
¡¡¡¡{
¡¡¡¡System.out.print(integer+" ");
¡¡¡¡}
¡¡¡¡System.out.println("\n¶Ô²¿·ÖÅÅÐòºó˳Ðò");
¡¡¡¡int[] ints2=new int[]{212,43,2,324,4,4,57,1};
¡¡¡¡//¶ÔÊý×éµÄ[2,6)λ½øÐÐÅÅÐò
¡¡¡¡Arrays.sort(ints2,2,6);
¡¡¡¡for (int i=0;i
¡¡¡¡{
¡¡¡¡System.out.print(ints2[i]+" ");
¡¡¡¡}
¡¡¡¡}
¡¡¡¡}
¡¡¡¡ÅÅÐò½á¹ûÈçÏÂ
¡¡¡¡ÔöÐòÅÅÐòºó˳Ðò
¡¡¡¡1 2 4 57 324
¡¡¡¡¼õÐòÅÅÐòºó˳Ðò
¡¡¡¡324 6 4 4 2 1
¡¡¡¡¶Ô²¿·ÖÅÅÐòºó˳Ðò
¡¡¡¡212 43 2 4 4 324 57 1
¡¡¡¡´ò¿ªArrays.sort()Ô´Â룬»¹ÊÇÒÔintÐÍΪÀý£¬ÆäËûÀàÐÍÒ²ÊÇ´óͬСÒì
¡¡¡¡public static void sort(int[] a) {
¡¡¡¡DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
¡¡¡¡}
¡¡¡¡public static void sort(int[] a, int fromIndex, int toIndex) {
¡¡¡¡rangeCheck(a.length, fromIndex, toIndex);
¡¡¡¡DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
¡¡¡¡}
¡¡¡¡´ÓÔ´ÂëÖз¢ÏÖ£¬Á½ÖÖ²ÎÊýÀàÐ͵Äsort·½·¨¶¼µ÷ÓÃÁË DualPivotQuicksort.sort()·½·¨
¡¡¡¡¼ÌÐø¸ú×ÙÔ´Âë
¡¡¡¡static void sort(int[] a, int left, int right,
¡¡¡¡int[] work, int workBase, int workLen) {
¡¡¡¡// Use Quicksort on small arrays
¡¡¡¡if (right - left < QUICKSORT_THRESHOLD) {
¡¡¡¡sort(a, left, right, true);
¡¡¡¡return;
¡¡¡¡}
¡¡¡¡/*
¡¡¡¡* Index run[i] is the start of i-th run
¡¡¡¡* (ascending or descending sequence).
¡¡¡¡*/
¡¡¡¡int[] run = new int[MAX_RUN_COUNT + 1];
¡¡¡¡int count = 0; run[0] = left;
¡¡¡¡// Check if the array is nearly sorted
¡¡¡¡for (int k = left; k < right; run[count] = k) {
¡¡¡¡if (a[k] < a[k + 1]) { // ascending
¡¡¡¡while (++k <= right && a[k - 1] <= a[k]);
¡¡¡¡} else if (a[k] > a[k + 1]) { // descending
¡¡¡¡while (++k <= right && a[k - 1] >= a[k]);
¡¡¡¡for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) {
¡¡¡¡int t = a[lo]; a[lo] = a[hi]; a[hi] = t;
¡¡¡¡}
¡¡¡¡} else { // equal
¡¡¡¡for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {
¡¡¡¡if (--m == 0) {
¡¡¡¡sort(a, left, right, true);
¡¡¡¡return;
¡¡¡¡}
¡¡¡¡}
¡¡¡¡}
¡¡¡¡/*
¡¡¡¡* The array is not highly structured,
¡¡¡¡* use Quicksort instead of merge sort.
¡¡¡¡*/
¡¡¡¡if (++count == MAX_RUN_COUNT) {
¡¡¡¡sort(a, left, right, true);
¡¡¡¡return;
¡¡¡¡}
¡¡¡¡}
¡¡¡¡// Check special cases
¡¡¡¡// Implementation note: variable "right" is increased by 1.
¡¡¡¡if (run[count] == right++) { // The last run contains one element
¡¡¡¡run[++count] = right;
¡¡¡¡} else if (count == 1) { // The array is already sorted
¡¡¡¡return;
¡¡¡¡}
¡¡¡¡// Determine alternation base for merge
¡¡¡¡byte odd = 0;
¡¡¡¡for (int n = 1; (n <<= 1) < count; odd ^= 1);
¡¡¡¡// Use or create temporary array b for merging
¡¡¡¡int[] b; // temp array; alternates with a
¡¡¡¡int ao, bo; // array offsets from 'left'
¡¡¡¡int blen = right - left; // space needed for b
¡¡¡¡if (work == null || workLen < blen || workBase + blen > work.length) {
¡¡¡¡work = new int[blen];
¡¡¡¡workBase = 0;
¡¡¡¡}
¡¡¡¡if (odd == 0) {
¡¡¡¡System.arraycopy(a, left, work, workBase, blen);
¡¡¡¡b = a;
¡¡¡¡bo = 0;
¡¡¡¡a = work;
¡¡¡¡ao = workBase - left;
¡¡¡¡} else {
¡¡¡¡b = work;
¡¡¡¡ao = 0;
¡¡¡¡bo = workBase - left;
¡¡¡¡}
¡¡¡¡// Merging
¡¡¡¡for (int last; count > 1; count = last) {
¡¡¡¡for (int k = (last = 0) + 2; k <= count; k += 2) {
¡¡¡¡int hi = run[k], mi = run[k - 1];
¡¡¡¡for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) {
¡¡¡¡if (q >= hi || p < mi && a[p + ao] <= a[q + ao]) {
¡¡¡¡b[i + bo] = a[p++ + ao];
¡¡¡¡} else {
¡¡¡¡b[i + bo] = a[q++ + ao];
¡¡¡¡}
¡¡¡¡}
¡¡¡¡run[++last] = hi;
¡¡¡¡}
¡¡¡¡if ((count & 1) != 0) {
¡¡¡¡for (int i = right, lo = run[count - 1]; --i >= lo;
¡¡¡¡b[i + bo] = a[i + ao]
¡¡¡¡);
¡¡¡¡run[++last] = right;
¡¡¡¡}
¡¡¡¡int[] t = a; a = b; b = t;
¡¡¡¡int o = ao; ao = bo; bo = o;
¡¡¡¡}
¡¡¡¡}
¡¡¡¡½áºÏÎĵµÒÔ¼°Ô´´úÂ룬ÎÒÃÇ·¢ÏÖ£¬jdkÖеÄArrays.sort()µÄʵÏÖÊÇͨ¹ýËùνµÄË«Öá¿ìÅŵÄËã·¨
¡¡¡¡/**
¡¡¡¡* This class implements the Dual-Pivot Quicksort algorithm by
¡¡¡¡* Vladimir Yaroslavskiy, Jon Bentley, and Josh Bloch. The algorithm
¡¡¡¡* offers O(n log(n)) performance on many data sets that cause other
¡¡¡¡* quicksorts to degrade to quadratic performance, and is typically
¡¡¡¡* faster than traditional (one-pivot) Quicksort implementations.
¡¡¡¡*
¡¡¡¡* All exposed methods are package-private, designed to be invoked
¡¡¡¡* from public methods (in class Arrays) after performing any
¡¡¡¡* necessary array bounds checks and expanding parameters into the
¡¡¡¡* required forms.
¡¡¡¡*
¡¡¡¡* @author Vladimir Yaroslavskiy
¡¡¡¡* @author Jon Bentley
¡¡¡¡* @author Josh Bloch
¡¡¡¡*
¡¡¡¡* @version 2011.02.11 m765.827.12i:5\7pm
¡¡¡¡* @since 1.7
¡¡¡¡*/
¡¡¡¡Java1.8µÄ¿ìÅÅÊÇÒ»ÖÖË«Öá¿ìÅÅ£¬¹ËÃû˼Ò壺˫Öá¿ìÅÅÊÇ»ùÓÚÁ½¸öÖáÀ´½øÐбȽϣ¬¸úÆÕͨµÄÑ¡ÔñÒ»¸öµãÀ´×÷ΪÖáµãµÄ¿ìÅÅÊÇÓкܴóµÄÇø±ðµÄ£¬Ë«ÖáÅÅÐòÀûÓÃÁËÇø¼äÏàÁÚµÄÌØÐÔ£¬¶ÔÔ±¾µÄ¿ìÅŽøÐÐÁËЧÂÊÉϵÄÌá¸ß£¬ºÜ´ó³Ì¶ÈÉÏÊÇÀûÓÃÁËÊýѧµÄÒ»Ð©ÌØÐÔ¡£¡£¡£¡£¡£àÅ¡£¡£¡£·´ÕýºÜ¸ßÉîµÄÑù×Ó
¡¡¡¡Ëã·¨²½Öè
¡¡¡¡1.¶ÔÓÚºÜСµÄÊý×é(³¤¶ÈСÓÚ27)£¬»áʹÓòåÈëÅÅÐò¡£
¡¡¡¡2.Ñ¡ÔñÁ½¸öµãP1,P2×÷ΪÖáÐÄ£¬±ÈÈçÎÒÃÇ¿ÉÒÔʹÓõÚÒ»¸öÔªËØºÍ×îºóÒ»¸öÔªËØ¡£
¡¡¡¡3.P1±ØÐë±ÈP2ҪС£¬·ñÔò½«ÕâÁ½¸öÔªËØ½»»»£¬ÏÖÔÚ½«Õû¸öÊý×é·ÖΪËIJ¿·Ö£º
¡¡¡¡(1)µÚÒ»²¿·Ö£º±ÈP1СµÄÔªËØ¡£
¡¡¡¡(2)µÚ¶þ²¿·Ö£º±ÈP1´óµ«ÊDZÈP2СµÄÔªËØ¡£
¡¡¡¡(3)µÚÈý²¿·Ö£º±ÈP2´óµÄÔªËØ¡£
¡¡¡¡(4)µÚËIJ¿·Ö£ºÉÐδ±È½ÏµÄ²¿·Ö¡£
¡¡¡¡ÔÚ¿ªÊ¼±È½Ïǰ£¬³ýÁËÖáµã£¬ÆäÓàÔªËØ¼¸ºõ¶¼ÔÚµÚËIJ¿·Ö£¬Ö±µ½±È½ÏÍêÖ®ºóµÚËIJ¿·ÖûÓÐÔªËØ¡£
¡¡¡¡4.´ÓµÚËIJ¿·ÖÑ¡³öÒ»¸öÔªËØa[K]£¬ÓëÁ½¸öÖáÐıȽϣ¬È»ºó·Åµ½µÚÒ»¶þÈý²¿·ÖÖеÄÒ»¸ö¡£
¡¡¡¡5.ÒÆ¶¯L£¬K£¬GÖ¸Ïò¡£
¡¡¡¡6.ÖØ¸´ 4 5 ²½£¬Ö±µ½µÚËIJ¿·ÖûÓÐÔªËØ¡£
¡¡¡¡7.½«P1ÓëµÚÒ»²¿·ÖµÄ×îºóÒ»¸öÔªËØ½»»»¡£½«P2ÓëµÚÈý²¿·ÖµÄµÚÒ»¸öÔªËØ½»»»¡£
¡¡¡¡8.µÝ¹éµÄ½«µÚÒ»¶þÈý²¿·ÖÅÅÐò¡£
ÍÆ¼öÔĶÁ£º
JavaÊÓÆµ½Ì³Ì
±±¾©Ð£Çø