O (worker _ worker) O ~, This name sounds yellow at first glance. In fact, it is to sort data quickly and sort data first. When the data is smaller than a certain scale, for example, K, the data is inserted for sorting. Because the data is smaller than a certain scale, the insertion sorting is more efficient. I also wrote an algorithm for merging, inserting, and sorting. The idea is similar to that.
The total time complexity is O (NK + NLG (N/K), which is a good proof:
First, perform the division by two points. When the division scale is K, the Division is stopped. If the depth is h, then t (n/2 ^ h) = K, then H = lg (N/K ), the number of leaf nodes with K at the bottom layer is 2 ^ lg (N/K) = N/K, and each k is inserted and sorted internally. The worst case is O (K ^ 2 ), the total insertion sorting time is (N/K) * (K ^ 2) = NK. The Division cost of each layer is N, and a total of lg (N/K) layers need to be divided, and the consumption is O (NLG (N/K )). The total time complexity is that the two are added to the total O (NK + NLG (N/K )).
The Code is as follows:
# Include <string. h> # include <time. h> # define buffer_size 10int partition (int * a, int P, int R) {int I = 0; Int J = 0; int TMP = 0; int x = 0; I = p-1; X = A [R]; for (j = P; j <r; j ++) {if (a [J] <= X) {I ++; TMP = A [I]; A [I] = A [J]; A [J] = TMP ;}} TMP = A [I + 1]; A [I + 1] = A [R]; A [R] = TMP; return I + 1;} int randompartition (int * a, int P, int R) {int I = 0; int TMP = 0; srand (unsigned) Time (null); I = rand () % (r-p + 1) + P; TMP = A [I]; A [I] = A [R]; A [R] = TMP; return partition (A, P, R );} void insertionsort (int * a, int P, int R) {int n = r-p + 1; int B [N]; int I = 0; Int J = 0; B [0] = A [p]; for (j = p + 1; j <= r; j ++) {I = J-1; while (I> = 0 & A [J] <= B [I]) {B [I + 1] = B [I]; I --;} B [I + 1] = A [J] ;}for (j = P, I = 0; j <= r; j ++) {A [J] = B [I]; I ++ ;}} void randomquicksort (int * a, int P, int R, int K) {int q = 0; if (P> = r) {return;} If (r-p + 1 <k) {insertionsort (A, P, R);} else {q = randompartition (, p, R); randomquicksort (A, P, Q-1, k); randomquicksort (A, q + 1, R, k) ;}} int main () {int I = 0; Int J = 0; int A [buffer_size]; // randomly generated array srand (unsigned) Time (null); For (j = 0; j <buffer_size; j ++) {A [J] = rand () % 100;} printf ("randomly generated array: \ n"); for (I = 0; I <buffer_size; I ++) {printf ("% d", a [I]);} printf ("\ n"); randomquicksort (A, 0, BUFFER_SIZE-1, 1); printf ("Sort arrays quickly inserted: \ n"); for (I = 0; I <buffer_size; I ++) {printf ("% d ", A [I]);} system ("pause"); Return 0 ;}