Quick sorting is a grouping algorithm that divides arrays into several parts, including sorting and recursive sorting. Algorithm time complexity O (nlgn ). This is the fastest sorting algorithm. Counting sorting, base sorting, and bucket sorting algorithms are non-Comparative sorting algorithms. Their complexity is O (n ). Quick sorting algorithms must be skillful in selecting pivot points, so it is best to meet immediate requirements.
1. Fast Sorting Algorithm:
Normal quick sorting
1 typedef struct int_array
2 {
3 int * pA; // array. For any type of data, you can use char * PC or Int typelen.
4 int array_size; // Number of array elements
5 Int capacity; // cache size
6} int_array; // a bit similar to a vector
7
8
9int partition (int_array * Pia, int P, int R)
10 {
11 int X, I, J;
12 x = Pia-> pa [p];
13 I = p-1;
14 J = R + 1;
15 while (1)
16 {
17 -- J;
18 while (Pia-> pa [J]> X & J --> = P)
19;
20 + I;
21 While (Pia-> pa [I] <X & I ++ <= r)
22;
23
24 if (I <j)
25 {
26 swap (& Pia-> pa [I], & Pia-> pa [J]);
27}
28 else
29 {
30 return J;
31}
32}
33}
34
35 void quick_sort (int_array * Pia, int P, int R)
36 {
37 int Q;
38 If (P <R)
39 {
40 q = partition (PIA, P, R );
41 quick_sort (PIA, p, q );
42 // P = q + 1;
43 quick_sort (PIA, q + 1, R );
44}
45}
2. Improved quick sorting of versions (quick sorting of randomly distributed versions)
Fast sorting of random distribution
1int random_partition (int_array * Pia, int P, int R)
2 {
3 int I = (Mrand () % (r-p + 1) + P;
4 swap (& Pia-> pa [I], & Pia-> pa [p]);
5 return partition (PIA, P, R );
6}
7 void random_quick_sort (int_array * Pia, int P, int R)
8 {
9 int Q;
10 if (P <R)
11 {
12 q = random_partition (PIA, P, R );
13 random_quick_sort (PIA, p, q );
14 random_quick_sort (PIA, q + 1, R );
15}
16}
3. Counting Sorting Algorithm
The counting sorting algorithm needs to know the upper and lower limits of array elements, and it is best to sort the integer elements and count the number of each element, which requires a large amount of space.
Count sorting
1 void counting_sort (int_array * Pia, int max)
2 {
3 int I, C;
4 int * pb, * PC;
5
6 // malloc can be up to unit_max bytes, So C can be up to uint_max/sizeof (INT );
7 Pb = (int *) malloc (sizeof (INT) * Pia-> array_size );
8 If (! PB)
9 {
10 return;
11}
12 C = MAX + 1;
13 Pc = (int *) malloc (sizeof (INT) * C );
14 if (! PC)
15 {
16 free (PB );
17 return;
18}
19 memset (char *) Pb, 0xcc, sizeof (INT) * Pia-> array_size );
20 memset (char *) PC, 0xcc, sizeof (INT) * C );
21 For (I = 0; I <Pia-> array_size; ++ I)
22 {
23 Pb [I] = Pia-> pa [I];
24}
25 For (I = 0; I <C; ++ I)
26 {
27 PC [I] = 0;
28}
29 for (I = 0; I <Pia-> array_size; ++ I)
30 {
31 PC [Pb [I] ++;
32}
33 for (I = 1; I <C; ++ I)
34 {
35 PC [I] + = pc [I-1];
36}
37 For (I = Pia-> array_size-1; I> = 0; -- I)
38 {
39 Pia-> pa [PC [Pb [I]-1] = Pb [I];
40 PC [Pb [I] --;
41}
42 free (PB );
43 free (PC );
44} 4, test code
When testing an integer, A 1kw integer is used for fast sorting of 5 seconds, a heap sorting of 20 seconds, and a count sorting of 0.43 seconds.