⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 arrays.java

📁 java源代码 请看看啊 提点宝贵的意见
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
     * @param fromIndex the index of the first element (inclusive) to be     *        sorted.     * @param toIndex the index of the last element (exclusive) to be sorted.     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or     *	       <tt>toIndex &gt; a.length</tt>     */    public static void sort(float[] a, int fromIndex, int toIndex) {        rangeCheck(a.length, fromIndex, toIndex);	sort2(a, fromIndex, toIndex);    }    private static void sort2(double a[], int fromIndex, int toIndex) {        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 = binarySearch(a, 0.0d, fromIndex, n-1); // 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;        }    }    private static void sort2(float a[], int fromIndex, int toIndex) {        final int NEG_ZERO_BITS = Float.floatToIntBits(-0.0f);        /*         * 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]) {		float swap = a[i];                a[i] = a[--n];                a[n] = swap;            } else {                if (a[i]==0 && Float.floatToIntBits(a[i])==NEG_ZERO_BITS) {                    a[i] = 0.0f;                    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 = binarySearch(a, 0.0f, fromIndex, n-1); // posn of ANY zero            do {                j--;            } while (j>=0 && a[j]==0.0f);            // j is now one less than the index of the FIRST zero            for (int k=0; k<numNegZeros; k++)                a[++j] = -0.0f;        }    }    /*     * The code for each of the seven primitive types is largely identical.     * C'est la vie.     */    /**     * Sorts the specified sub-array of longs into ascending order.     */    private static void sort1(long 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	}	long 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);    }    /**     * Swaps x[a] with x[b].     */    private static void swap(long x[], int a, int b) {	long t = x[a];	x[a] = x[b];	x[b] = t;    }    /**     * Swaps x[a .. (a+n-1)] with x[b .. (b+n-1)].     */    private static void vecswap(long x[], int a, int b, int n) {	for (int i=0; i<n; i++, a++, b++)	    swap(x, a, b);    }    /**     * Returns the index of the median of the three indexed longs.     */    private static int med3(long 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);    }    /**     * 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;    }    /**     * Swaps x[a .. (a+n-1)] with x[b .. (b+n-1)].     */    private static void vecswap(int x[], int a, int b, int n) {	for (int i=0; i<n; i++, a++, b++)	    swap(x, a, b);    }    /**     * Returns the index of the median of the three indexed integers.     */    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 shorts into ascending order.     */    private static void sort1(short 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	}	short 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);    }    /**     * Swaps x[a] with x[b].     */    private static void swap(short x[], int a, int b) {	short t = x[a];	x[a] = x[b];	x[b] = t;    }    /**     * Swaps x[a .. (a+n-1)] with x[b .. (b+n-1)].     */    private static void vecswap(short x[], int a, int b, int n) {	for (int i=0; i<n; i++, a++, b++)	    swap(x, a, b);    }    /**     * Returns the index of the median of the three indexed shorts.     */    private static int med3(short 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 chars into ascending order.     */    private static void sort1(char 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);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -