Bottom-up merge sorting algorithm

Source: Internet
Author: User

1, what is the bottom-up of the merge sort?

In the final analysis, whether it is the top-down merge sort or the now speaking of the bottom-up of the merge sort, its essence is to merge.

Take a look at a demonstration process:

This is the sequence of arrays to be sorted.

The array is divided into 4 groups with 2 elements, and each group is internally divided into 2 sub-sequences to merge up

This is the effect after the merger.

Then a group is divided into 2 groups with 4 elements, and each group is internally divided into 2 sub-sequences to merge up

This is the effect after the merger.

Then a group is divided into 1 groups with 8 elements, and each group is internally divided into 2 sub-sequences to merge up

Finally, the whole sequence is programmed in order.

In fact, from this point of view, this is like the front of the top-down sort process in the back of a process.

2. Complexity of Time

Same as top-down merge sort, time complexity is O (NLOGN) level.

3, the implementation of the algorithm (based on C + +)

#define MIN (A, B) ((a) < (b)? (a): (b))

1 /***************************** Bottom-up merge sort algorithm realizes ***********************************/2Template<typename t>3 voidMERGESORTBU (T arr[],intcount)4 {5      for(intSize =1; Size <= count; Size + = size) {//Outer Loop Grouping of sub-sequences6          for(inti =0; i + size < count; i + = size + size) {//the inner loop iterates through all the sub-sequences of the well-grouped groups and merges them up sequentially7__merge<t> (arr, I, i + Size-1, MIN (i + size + size-1, Count-1));//This function is implemented in the top-down merge sort __merge function9         }Ten     } One } A /*******************************************************************************************/

4. Performance test (compared to optimized top-down merge sorting algorithm)

Test Data Volume 50000:

Test Data Volume 100000:

5. Algorithm optimization

In fact, the optimization algorithm is the same as the top-down merge sort

1 /*optimization ********************************** of **************************** bottom-up merging sorting algorithm*/2Template<typename t>3 voidMERGESORTBU (T arr[],intcount)4 {5      for(intSize =1; Size <= count; Size + = size) {//Outer Loop Grouping of sub-sequences6          for(inti =0; i + size < count; i + = size + size) {//the inner loop iterates through all the sub-sequences of the well-grouped groups and merges them up sequentially7             if(MIN (i + size + size-1, Count-1)-I < -) {//Optimization measures 28__insertsortmg<t> (arr, I, MIN (i + size + size-1, Count-1));//This function is the __INSERTSORTMG function implemented in the top-down merge sort9                 Continue;Ten             } One  A             if(Arr[i + Size-1] > arr[i + size]) {//Optimization measures 1 -__merge<t> (arr, I, i + Size-1, MIN (i + size + size-1, Count-1));//This function is implemented in the top-down merge sort __merge function -             }    the         } -     } - } - /*******************************************************************************************/

Performance testing after optimization:

Test Data Volume 50000:

Test Data Volume 100000:

Summary: From the above data can be seen, after optimization, the performance of the bottom-up sorting has been a great improvement, but still more than the top-down merge sort slow,

There is a reason for this, and there is no analysis here.

It is also worth mentioning that the code of the non-optimized algorithm shows that the code does not use the attributes of the array (using the index to find the corresponding element), so we can

Sort a list by the bottom-up merge sort.

Bottom-up merge sorting algorithm

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.