Realization and discussion of merging and sorting of vector articles

Source: Internet
Author: User

First, preface

Merge sort was first implemented by von Neumann in 1945 on Edvac, and the concept of merge sort (mergesort) was simple but also profound, as an algorithm is both old and still alive. In the history of sorting algorithm development, merge sort has special status, it is the first deterministic sort algorithm that can still keep O (NLOGN) run time in the worst case.

Today, in the early development of the computer in the process of some of the problems that appeared on a larger scale again, merge sort so rejuvenated. For example, early computer storage capacity is limited, so that high-speed memory can not accommodate all the data, or can only use a tape drive or card, such as sequential storage devices, which both promote the emergence of merge sequencing, but also provide the stage for the algorithm. Information is ubiquitous today, we once again found that the vast amount of information that human beings have, not only forces us to store and organize them more on the distributed platform, but also the processing of massive information must first consider, how to work in a cross-node environment of efficient collaborative computing. So behind many new algorithms and techniques, you can see the shadow of the merge sort.

Second, the algorithm thought and the realization

First of all, to understand the two-way merging algorithm, two-way merge is an iterative algorithm. In each iteration, only the first element of the two-to-be-merged vector is compared, the small is taken out and appended to the end of the output vector, and the successor of the element in the original vector becomes the new first element. So reciprocating, until a certain amount is always empty. Finally, another non-empty vector is connected to the end of the output vector as a whole. Merge sort can also be understood to be achieved by repeatedly invoking the so-called two-way merge (2-way merge) algorithm. The so-called two-way merging is to merge two ordered sequences into an ordered sequence. The sequence here can be a vector, the time required to merge the sort, and the sum of the time required to merge the two paths.

It can be seen that the two-way merging algorithm only needs to load the first element of two vectors at any moment, so in addition to the vector of the merged output, only the auxiliary space of constant size is needed. In addition, the algorithm always strictly processed the input and output vectors sequentially, so it is especially suitable for the use of sequential memory such as tape drives.

The implementation is as follows:

1  //merge sort of vectors2Template <typename t>3 voidVector<t>::mergesort (rank lo, rank hi) {//0 <= Lo < hi <= size4     if(Hi-lo <2)return;//a single element must be ordered and used as a recursive base5     intMi = (lo + hi) >>1; 6MergeSort (lo, MI);//sort by the midpoint of the boundary7 mergesort (MI, hi);8Merge (lo, MI, hi);//Two-way merge9  }Ten  One //two-way merging algorithm for ordered vectors ATemplate <typename t> - voidVector<t>::merge (rank lo, rank mi, rank hi) {//respective ordered sub-vectors [Lo, MI) and [mi, hi] -t* A = _elem + lo;//combined Vector a[0, hi-lo) = _elem[lo, HI) the     intLB = Mi-Lo; -t* B =NewT[LB];//front sub-vector b[0, lb) = _elem[lo, MI) -      for(Rank i =0; i < lb; B[i] = a[i++]);//Pre-copy sub-vector -     intLC = Hi-mi; +t* C = _elem + mi;//posterior sub-vector c[0, LC) = _elem[mi, HI) -      for(Rank i =0, j =0, k =0; (J < lb) | | (k < LC); ) {//B[j] and C[k] continue to the end of a +         if((J < lb) && (! (K < LC) | | (B[j] <= c[k])) ) a[i++] = b[j++]; A         if(K < LC) && (! (J < lb) | | (C[k] < b[j])) ) a[i++] = c[k++]; at } -     Delete[] B;//Free temporary space B -}//Merge to get complete ordered vector [lo, hi]

Third, analysis and improvement

So, based on the above two-way merge linear algorithm, merge sorting algorithm time complexity is how much? It is advisable to use the recursive equation analysis method, which first deals with the time required for the merge sorting algorithm to process vectors of length n as t (n). According to the algorithm conception and the flow, in order to merge the vector of the length n, we should recursively merge the two sub-vectors with the length of each N/2, and then take the linear time to do a two-way merging. Thus, the following recursive relationships are available: T (n) = 2*t (N/2) + O (n) In addition, when the length of the sub-vector is shortened to 1 o'clock, recursion terminates and returns the vector directly.

Therefore, the boundary condition T (1) = O (1) is more than the recursive type, can be solved: t (n) = O (Nlogn) that is, the merge sort algorithm can be in O (NLOGN) time for the length of the vector of N to complete the sorting. Because the efficiency of the two-way merging algorithm is stable in O (n), it is more accurate to say that the time complexity of the merging sorting algorithm should be O (NLOGN). Re-analysis, if B has been exhausted, C can be directly transferred; If c is exhausted early, B can be transferred directly. And we don't have to consider the case of C running out prematurely. Then the refinement of the two-way merge is achieved:

1  for 0 0 0;  J <2         if ((K < LC) &&  (C[k] < b[j]) a[i++] = c[k++]; 3         if ( ! (K < LC) | | (B[j] <= c[k])  ) a[i++] = b[j++]; 4  }  // swap the two sentences in the loop body to remove redundant logic. 

Iv. Summary

This algorithm framework can also be applied to another class of typical sequence structure-list, and also can achieve linear time efficiency.

Realization and discussion of merging and sorting of vector articles

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.