/** * Sorts the integer array of the specified range in ascending order. * X [] array to be sorted * Off: starts sorting from the off element of the array. * Len array Length */ Private static void sort1 (int x [], int off, int len ){ // Optimization 1: in small-scale (size <7) arrays, direct insertion of sorting is more efficient than fast sorting. 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; } // Optimization 2: carefully select the partitioning element, that is, pivot // If it is a small-scale array (size <= 7), take the intermediate element as the pivot. // If it is an array of medium size (7 = <size <= 40), take the number of the intermediate size in the number at the first, middle, and last positions of the array as the pivot. // If it is a large-scale array (size> 40), take a pseudo-medium number (in the middle of the number s) from the nine specified numbers) Int m = off + (len> 1 ); If (len> 7 ){ Int l = off; Int n = off + len-1; If (len> 40 ){ 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 ); } Int v = x [m]; // Optimization 3: each pivot v Division forms a form such as (<v) * v * (> v )* // Stage 1: form an array of 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 --); } // Phase 2, swap the pivot and element equal to the pivot to the center of the array 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 ); // Phase 3: recursive sorting and pivot are not equal to element intervals If (s = B-a)> 1) Sort1 (x, off, s ); If (s = d-c)> 1) Sort1 (x, n-s, s ); }
|