Multi-way Merge Sorting multi-way merge sorting algorithms are involved in common data structure books. From 2 to multi-channel (k), increasing k can reduce the read and write time of the external storage information, but the selection of the minimum records in k merge segments needs to be compared with the K-1, in order to obtain a sequential segment of u records (U-1) (k-1) times, if the number of merged records is s times, then the files of n records are discharged, the total number of comparisons performed in the internal merging process is s (n-1) (k-1). If there are m merge segments, s = logkm, so the total number of comparisons is: (rounded up) (logkm) (k-1) (n-1) = (rounded up) (log2m/log2k) (k-1) (n-1) while (k-1) /log2k increases with k, so the internal merge time increases with k, which offsets the time when external memory reads and writes are reduced, this leads to the use of the "loser tree" tree of loser. In the internal merging process, use the loser tree to reduce the number of times that the minimum record comparison is selected from k merging segments to (rounded up) (log2k) so that the total number of comparisons is (rounded up) (log2m) (n-1), regardless of k.
Loser tree
The leaf node records the minimum data in k segments, and then plays the game in pairs. The loser tree records the loser who has just finished the competition in the parent node, allowing the winner to participate in a higher level of competition. In the final stage, the root node records the loser. Therefore, a new root node is required to record the winner (for example, node 0 ).
Example: here we use a four-way merge as an example. Assume that each merge segment is already in the input buffer zone.
The first element of each route is the leaf node of the victory tree. (5, 7) compared to 5 wins 7 and fails to become its root node. () compared with 9 wins 29 and fails to become its root node, the winner (5, 9) fails in the next match and becomes its root node 5 wins and outputs it to the output buffer. Output from the first merge segment. Add the second element of the first merge segment to the leaf node, for example:
Add leaf node 16 for the second comparison. Like the victory tree, the right subtree does not need to be compared because the leaf node of the right subtree does not change.
Implementation of K-path Merge Sorting
# Include
Using namespace std; # define LEN 10 // Maximum length of the merged segment # define MINKEY-1 // by default, all values are positive # define MAXKEY 100 // maximum value, returns the value of struct Array {int arr [LEN]; int num; int pos;} * A; int k, count; int * LoserTree, * External; void Adjust (int s) {int t = (s + k)/2; // ls [t] is the subscript of the parent node of B [s] int temp; while (t> 0) {if (External [s]> External [LoserTree [t]) {temp = s; s = LoserTree [t]; loserTree [t] = temp;} t = t/2;} LoserTree [0] = s;} void CreateLoserTree () {External [k] = MINKEY; int I; for (I = 0; I
= 0; I --) Adjust (I);} void K_Merge () {int I, p; // initialize the External array to create the LoserTreefor (I = 0; I
= A [p]. num) External [p] = MAXKEY; else {External [p] = A [p]. arr [A [p]. pos]; A [p]. pos ++;} Adjust (p) ;}cout <
> K; A = (Array *) malloc (sizeof (Array) * k); for (I = 0; I
> A [I]. num; count = count + A [I]. num; for (j = 0; j> A [I]. arr [j];} A [I]. pos = 0;} LoserTree = (int *) malloc (sizeof (int) * k); External = (int *) malloc (sizeof (int) * (k + 1); K_Merge (); system ("pause"); return 0 ;}