Loser tree k-road Merge Sorting-heaven Earth-blog channel-csdn. net

Source: Internet
Author: User

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?
  1. # Include <iostream>
  2. Using NamespaceSTD;
  3. # Define Len 10 // Maximum length of merged segments
  4. # Define Minkey-1 // by default, all values are positive.
  5. # Define maxkey 100 // maximum value, which is assigned after all output of a segment
  6. StructArray
  7. {
  8. IntArr [Len];
  9. IntNum;
  10. IntPos;
  11. } *;
  12. IntK, count;
  13. Int* Losertree, * external;
  14. VoidAdjust (IntS)
  15. {
  16. IntT = (S + k)/2;
  17. IntTemp;
  18. While(T> 0)
  19. {
  20. If(External [s]> external [losertree [T])
  21. {
  22. Temp = s;
  23. S = losertree [T];
  24. Losertree [T] = temp;
  25. }
  26. T = T/2;
  27. }
  28. Losertree [0] = s;
  29. }
  30. VoidCreatelosertree ()
  31. {
  32. External [k] = Minkey;
  33. IntI;
  34. For(I = 0; I <K; I ++) losertree [I] = K;
  35. For(I = K-1; I> = 0; I --) adjust (I );
  36. }
  37. VoidK_merge ()
  38. {
  39. IntI, P;
  40. For(I = 0; I <K; I ++)
  41. {
  42. P = A [I]. Pos;
  43. External [I] = A [I]. Arr [p];
  44. // Cout <external [I] <",";
  45. A [I]. Pos ++;
  46. }
  47. Createlosertree ();
  48. IntNo = 0;
  49. While(No <count)
  50. {
  51. P = losertree [0];
  52. Cout <external [p] <",";
  53. No ++;
  54. If(A [p]. Pos> = A [p]. Num) External [p] = maxkey;
  55. Else
  56. {
  57. External [p] = A [p]. Arr [A [p]. Pos];
  58. A [p]. Pos ++;
  59. }
  60. Adjust (P );
  61. }
  62. Cout <Endl;
  63. }
  64. IntMain ()
  65. {
  66. Freopen ("In.txt","R", Stdin );
  67. IntI, J;
  68. Count = 0;
  69. Cin> K;
  70. A = (array *) malloc (Sizeof(Array) * k );
  71. For(I = 0; I <K; I ++)
  72. {
  73. Cin> A [I]. Num;
  74. Count = count + A [I]. Num;
  75. For(J = 0; j <A [I]. Num; j ++)
  76. {
  77. Cin> A [I]. Arr [J];
  78. }
  79. A [I]. Pos = 0;
  80. }
  81. Losertree = (Int*) Malloc (Sizeof(Int) * K );
  82. External = (Int*) Malloc (Sizeof(Int) * (K + 1 ));
  83. K_merge ();
  84. Return0;
  85. }
# 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.