Loser tree k-road Merge Sorting-heaven Earth-blog channel-csdn. net
Loser tree k-path Merge Sorting Classification: algorithm learning sorting Read by 628 Comment (0) Favorites Report Data Structure merge algorithm storage structtree
External sorting refers to the sorting of large files, that is, the records to be sorted are stored in external storage, and files to be sorted cannot be loaded into memory at a time, data exchange between memory and external memory is required multiple times to sort the entire file. The most common external sortingAlgorithmIt is a multi-path Merge Sorting, which breaks down the original file into multiple parts that can be loaded into memory at a time, and transfers each part to the memory for sorting. Then, merge and sort sorted sub-files.
Multi-path merge sorting algorithms are involved in common data structures. 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 during the internal merge process is S (n-1) (k-1), that is (rounded up) (logkm) (k-1) (n-1) = (rounded up) (log2m/log2k) (k-1) (n-1), and (k-1)/log2k with K increase and increase so the internal merge Time with K growth and growth, offset the time when external memory reads and writes are reduced. This will lead 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.
The loser tree is a complete binary tree, so the data structure can adopt a one-dimensional array. The number of elements is K leaf nodes, K-1 comparison node, a champion node A total of 2 K. Ls [0] is the champion node, ls [1] -- ls [k-1] is the comparison node, ls [k] -- ls [2k-1] is the leaf node (index B [0] -- B [k-1] points at the same time with another pointer ). In addition, BK is an additional auxiliary space, which does not belong to the loser tree and stores the Minkey value during initialization.
The process of multi-path Merge Sorting Algorithm is roughly as follows: first, the first element keywords in K merge segments are stored in the leaf node space of B [0] -- B [k-1, call createlosertree to create the loser tree. After the creation, the smallest keyword subscript (that is, the serial number of the merged segment) is saved to LS [0. Then keep repeating: Obtain the serial number of the merge segment from which the minimum keyword stored in LS [0] is Q, and output the first element of the merge segment to the ordered merge segment, then put the keyword of the next element into the leaf node B [Q] where the previous element was originally located, call adjust to adjust the loser tree following the leaf node B [Q] until the new smallest keyword is selected. Its subscript also exists in LS [0. Loop this operation until all elements are written to the sorted merging segment.
ReferencesHttp://chenkegarfield.blog.163.com/blog/static/62330008200910249526638/
Refer to the data structure (C language version ).Code, Implemented in C ++
The Code is as follows:
[CPP] View plaincopyprint?
-
- # Include <iostream>
-
- Using NamespaceSTD;
-
- # Define Len 10 // Maximum length of merged segments
-
- # Define Minkey-1 // by default, all values are positive.
-
- # Define maxkey 100 // maximum value, which is assigned after all output of a segment
-
-
- StructArray
-
- {
-
- IntArr [Len];
- IntNum;
-
- IntPos;
-
- } *;
-
-
- IntK, count;
-
- Int* Losertree, * external;
-
-
- VoidAdjust (IntS)
-
- {
- IntT = (S + k)/2;
-
- IntTemp;
-
- While(T> 0)
-
- {
-
- If(External [s]> external [losertree [T])
-
- {
-
- Temp = s;
- S = losertree [T];
-
- Losertree [T] = temp;
-
- }
-
- T = T/2;
-
- }
-
- Losertree [0] = s;
-
- }
-
-
- VoidCreatelosertree ()
-
- {
- External [k] = Minkey;
-
- IntI;
-
- For(I = 0; I <K; I ++) losertree [I] = K;
-
- For(I = K-1; I> = 0; I --) adjust (I );
-
- }
-
-
- VoidK_merge ()
-
- {
-
- IntI, P;
- For(I = 0; I <K; I ++)
-
- {
-
- P = A [I]. Pos;
-
- External [I] = A [I]. Arr [p];
-
- // Cout <external [I] <",";
-
- A [I]. Pos ++;
-
- }
-
- Createlosertree ();
- IntNo = 0;
-
- While(No <count)
-
- {
-
- P = losertree [0];
-
- Cout <external [p] <",";
-
- No ++;
-
- If(A [p]. Pos> = A [p]. Num) External [p] = maxkey;
- Else
-
- {
-
- External [p] = A [p]. Arr [A [p]. Pos];
-
- A [p]. Pos ++;
-
- }
-
- Adjust (P );
-
- }
-
- Cout <Endl;
-
- }
-
- IntMain ()
-
- {
-
- Freopen ("In.txt","R", Stdin );
-
-
- IntI, J;
-
- Count = 0;
-
- Cin> K;
-
- A = (array *) malloc (Sizeof(Array) * k );
- For(I = 0; I <K; I ++)
-
- {
-
- Cin> A [I]. Num;
-
- Count = count + A [I]. Num;
-
- For(J = 0; j <A [I]. Num; j ++)
-
- {
-
- Cin> A [I]. Arr [J];
-
- }
- A [I]. Pos = 0;
-
- }
-
- Losertree = (Int*) Malloc (Sizeof(Int) * K );
-
- External = (Int*) Malloc (Sizeof(Int) * (K + 1 ));
-
-
- K_merge ();
-
- Return0;
-
- }
# Include <iostream> using namespace STD; # define Len 10 // Maximum length of merged segments # define Minkey-1 // all positive values by default # 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; 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 <K; I ++) losertree [I] = K; for (I = K-1; I> = 0; I --) adjust (I) ;}void k_merge () {int I, p; for (I = 0; I <K; I ++) {P = A [I]. pos; External [I] = A [I]. arr [p]; // cout <external [I] <","; A [I]. pos ++;} createlosertree (); int NO = 0; while (no <count) {P = losertree [0]; cout <external [p] <", "; no ++; if (a [p]. pos> = A [p]. num) External [p] = maxkey; else {external [p] = A [p]. arr [A [p]. pos]; A [p]. pos ++;} adjust (p) ;}cout <Endl ;}int main () {freopen ("in.txt", "r", stdin); int I, J; count = 0; CIN> K; A = (array *) malloc (sizeof (array) * k); for (I = 0; I <K; I ++) {CIN> A [I]. num; Count = count + A [I]. num; For (j = 0; j <A [I]. num; j ++) {CIN> A [I]. arr [J];} A [I]. pos = 0;} losertree = (int *) malloc (sizeof (INT) * k); External = (int *) malloc (sizeof (INT) * (k + 1); k_merge (); Return 0 ;}
The input file is as follows:
5
5
1 5 6 8 25
6
2 6 9 25 30 32
3
5 9 16
6
6 9 15 24 30 36
2
8 34