[5] Multiple sort
When I learned the linked list, we have done the practice of synthesizing two ordered chain tables. At that time, we knew that it was characterized by synthesizing the overall ordered sequence of segmented sequences. In the internal sorting, the position of the merge is not very important, mainly because of the additional O (N) storage space; however, the return is the outside of the outside, we can only use the inner segmentation The sequence, in order to obtain the final ordered sequence, a method of merge must be used.
Iterative 2 ways to sort
2 is the simplest, and simple to data operation 2 on memory is often the best (such as balanced tree, AVL tree is often better than the balance tree of M fork). The so-called iteration is N / 2 sequences of Len = 2, then N / 4 sequences of LEN = 4, and then completed 2 sequences. When you are actually written, you need a temporary array as the original sequence. Perform an even number of "one 趟 并" can make the final result in the original array.
// Iterate 2 ways and sorted and the subroutine required
Template
Void Merge (T S [], T D [], INT L, INT M, INT N, INT & KCN, INT & RMN)
{
// s [] Source Table, D [] Monatic table, L Source Table The starting number of the first segment, the start number of the M source table, the length of the N source table
INT i = L, j = m, k = L; // i 1 pointer, J second paragraph, K target table pointer
For (; i IF ( KCN && S [i]> s [j]) {d [k] = s [j]; j ;} else {d [k] = s [i]; i ;} IF (i For (; i Else For (; J } Template Void MergePass (T s [], T D [], INT LEN, INT N, INT & KCN, INT & RMN { INT i = 0; For (; i 2 * len IF (i len Else for (; i } Template Void Mergesort (T A [], INT N, INT & KCN, INT & RMN) { KCN = 0; RMN = 0; T * TEMP = New T [N]; int LEN = 1; While (Len { MergePass (A, TEMP, LEN, N, KCN, RMN); LEN * = 2; MergePass (TEMP, A, LEN, N, KCN, RMN); len * = 2; } DELETE [] TEMP; } Test results, take directly N = 100000: Sort ascending n = 100000 Timespared: 210ms KCN = 877968 KCN / N = 8.77968 KCN / N ^ 2 = 8.77968E-005KCN / NLOGN = 0.528589 RMN = 1800000 rmn / n = 18 RMN / N ^ 2 = 0.00018 RMN / NLOGN = 1.08371 Sort randomness n = 100000 Timespared: 230ms KCN = 1529317 kcn / n = 15.2932 kcn / n ^ 2 = 0.000152932kcn / nlogn = 0.920741 RMN = 1800000 rmn / n = 18 RMN / N ^ 2 = 0.00018 RMN / NLOGN = 1.08371 Sort descending n = 100000 Timespared: 201ms KCN = 815024 kcn / n = 8.15024 kcn / n ^ 2 = 8.15024e-005kcn / nlogn = 0.490693 RMN = 1800000 rmn / n = 18 RMN / N ^ 2 = 0.00018 RMN / NLOGN = 1.08371 It can be seen that the RMN is a set value, and the value of RMN / N is not less than the minimum number of log2n. It is interested in comparing the difference between n = 1 and n = 2. Compared with the quick rules (n = 100,000, the order), although the merged KCN and RMN have fewer, the speed of the fast-row is still twice as fast as the altering, and the extraction of the description is more than one movement). The phenomenon is indeed worth thinking, this is also an unexpected harvest in the KCN and RMN statistics - the summary is not because KCN and RMN are more fast, but some extra things. Careful analysis will find that the amount of merged time consumption is mainly in the small segment. If we use the most efficient direct insertion in N very small, it will replace the return at this time and should be able to bring efficiency. As described below, first use the direct plug to generate the initial merged segment of Len = 32, and then return to: Template Void Mergesort (T A [], INT N, INT & KCN, INT & RMN) { KCN = 0; RMN = 0; T * Temp = new T [N]; int LEN = 32, i, J, k; // segmentation is inserted and sorted, generating the initial LEN long merged segment