# 深入JDK源码之Arrays类中的排序查找算法

## binarySearch()方法

`//针对int类型数组的二分法查找，key为要查找数的下标     private static int binarySearch0(int[] a, int fromIndex, int toIndex, int key) {         int low = fromIndex;         int high = toIndex - 1;         while (low <= high) {             int mid = (low + high) >>> 1;//无符号左移一位，相当于除以二             int midVal = a[mid];              if (midVal < key)                 low = mid + 1;             else if (midVal > key)                 high = mid - 1;             else                 return mid; // key found         }         return -(low + 1);  // key not found.     }`

## sort()方法

`private static final int INSERTIONSORT_THRESHOLD = 7;//插入排序门槛     public static void sort(Object[] a) {         Object[] aux = (Object[])a.clone();         mergeSort(aux, a, 0, a.length, 0);     }     //归并排序     private static void mergeSort(Object[] src, Object[] dest, int low, int high, int off) {         int length = high - low;         if (length < INSERTIONSORT_THRESHOLD) { //若数组长度小于7，则用冒泡排序             for (int i=low; i<high; i++)                 for (int j=i; j>low && ((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)                     swap(dest, j, j-1);             return;         }          // Recursively sort halves of dest into src         int destLow  = low;         int destHigh = high;         low  += off;         high += off;         int mid = (low + high) >>> 1; //无符号左移一位，         mergeSort(dest, src, low, mid, -off);         mergeSort(dest, src, mid, high, -off);          // If list is already sorted, just copy from src to dest.  This is an         // optimization that results in faster sorts for nearly ordered lists.         if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) {             System.arraycopy(src, low, dest, destLow, length);             return;         }          // Merge sorted halves (now in src) into dest         for(int i = destLow, p = low, q = mid; i < destHigh; i++) {             if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0)                 dest[i] = src[p++];             else                 dest[i] = src[q++];         }     }`

## sort()方法

`/**      * Swaps x[a] with x[b].      */     private static void swap(int x[], int a, int b) {         int t = x[a];         x[a] = x[b];         x[b] = t;     }     public static void sort(int[] a) {         sort1(a, 0, a.length);     }      private static int med3(int x[], int a, int b, int c) {//找出三个中的中间值         return (x[a] < x[b] ?                 (x[b] < x[c] ? b : x[a] < x[c] ? c : a) :                 (x[b] > x[c] ? b : x[a] > x[c] ? c : a));     }      /**      * Sorts the specified sub-array of integers into ascending order.      */     private static void sort1(int x[], int off, int len) {         // Insertion sort on smallest arrays         if (len < 7) {//采用冒泡排序             for (int i=off; i<len+off; i++)                 for (int j=i; j>off && x[j-1]>x[j]; j--)                     swap(x, j, j-1);             return;         }         //采用快速排序         // Choose a partition element, v         int m = off + (len >> 1);       // Small arrays, middle element         if (len > 7) {             int l = off;             int n = off + len - 1;             if (len > 40) {        // Big arrays, pseudomedian of 9                 int s = len/8;                 l = med3(x, l,     l+s, l+2*s);                 m = med3(x, m-s,   m,   m+s);                 n = med3(x, n-2*s, n-s, n);             }             m = med3(x, l, m, n); // Mid-size, med of 3         }         int v = x[m];          // Establish Invariant: v* (<v)* (>v)* v*         int a = off, b = a, c = off + len - 1, d = c;         while(true) {             while (b <= c && x[b] <= v) {                 if (x[b] == v)                     swap(x, a++, b);                 b++;             }             while (c >= b && x[c] >= v) {                 if (x[c] == v)                     swap(x, c, d--);                 c--;         }             if (b > c)                 break;             swap(x, b++, c--);         }          // Swap partition elements back to middle         int s, n = off + len;         s = Math.min(a-off, b-a  );  vecswap(x, off, b-s, s);         s = Math.min(d-c,   n-d-1);  vecswap(x, b,   n-s, s);          // Recursively sort non-partition-elements         if ((s = b-a) > 1)             sort1(x, off, s);         if ((s = d-c) > 1)             sort1(x, n-s, s);     }`

## sort()方法

`public static void sort(double[] a) {         sort2(a, 0, a.length);     }     private static void sort2(double a[], int fromIndex, int toIndex) {         //static long doubleToLongBits(double value)          //根据 IEEE 754 浮点双精度格式 ("double format") 位布局，返回指定浮点值的表示形式。         final long NEG_ZERO_BITS = Double.doubleToLongBits(-0.0d);         /*          * The sort is done in three phases to avoid the expense of using          * NaN and -0.0 aware comparisons during the main sort.          */          /*          * Preprocessing phase:  Move any NaN's to end of array, count the          * number of -0.0's, and turn them into 0.0's.          */         int numNegZeros = 0;         int i = fromIndex, n = toIndex;         while(i < n) {             if (a[i] != a[i]) {  //这段搞不懂，源代码怪怪的，感觉多此一举                 double swap = a[i];                 a[i] = a[--n];                 a[n] = swap;             } else {                 if (a[i]==0 && Double.doubleToLongBits(a[i])==NEG_ZERO_BITS) {                     a[i] = 0.0d;                     numNegZeros++;                 }                 i++;             }         }          // Main sort phase: quicksort everything but the NaN's         sort1(a, fromIndex, n-fromIndex);          // Postprocessing phase: change 0.0's to -0.0's as required         if (numNegZeros != 0) {             int j = binarySearch0(a, fromIndex, n, 0.0d); // posn of ANY zero             do {                 j--;             } while (j>=0 && a[j]==0.0d);              // j is now one less than the index of the FIRST zero             for (int k=0; k<numNegZeros; k++)                 a[++j] = -0.0d;         }     }`