The path of data structure and algorithm learning: merge sort

Source: Internet
Author: User

What is a merge sort?

Merge sort: An efficient sorting algorithm based on the merging operation, which is a very typical application of divide-and-conquer method (Divide and Conquer).

The main idea is: to merge the ordered sub-sequences to get a completely ordered sequence.

To put it simply: the sequence to be sorted is divided into a number of the most small sequence (the subsequence length is 1 or 2), the first sequence of each subsequence, and then the sequence of sub-sequences between the order. If two ordered tables are combined into an ordered table, they are called two-way merging.

The merge sort process diagram is as follows:

Algorithmic thinking

As we know, the most common sort algorithm, the idea is similar to the poor lifting method, that is, to traverse to sort the sequence number, each element is compared with the elements in the sequence, find the corresponding position, so the time complexity is generally o (n^2). The merge sort adopts the idea of "divide and conquer", that is, the sequence of columns to be sorted is kept down two points, the final reproducibility sequence is obtained, the subsequence is sorted, and the subsequence is continuously merged.

What are the benefits of doing this? The downward dichotomy causes the decomposition time of the subsequence to be reduced to O (LGN), while the decomposition time of the traditional method is O (n). Many people may be very strange, why say the decomposition time of the traditional method is O (n) ah?

Take bubble sort to say, bubble sort, in fact is continuously the length of the number of n is divided into 1 length and length of n-1 two sub-sequence, take the length of the sequence of 1 is continuously with the n-1 sequence of each element of the comparison, find the most suitable position.

In the sequencing of sub-sequences, although the merge sort is similar to the traditional sort, the time complexity is O (n), but in fact, the elements in the merge sort are less mobile (of course, the optimized bubble sort effect is the same, see: Click Me!!! )。

Specific code

I just wrote a simple implementation, in the process of writing I considered a lot of optimization of the program, will share with you later ~

#include <stdio.h> #define MAX5    voidMergeintA[],intStartintEndintResult[]);intMain () {intStart =0;intEnd = MAX-1;intA[max] = {2,3,8,6,1};intResult[max]; Merge (A, start, end, result); for(inti =0; i < MAX; i++) {printf ("%d", Result[i]);    } getchar (); }voidMergeintA[],intStartintEndintResult[]) {intMid = (end + start)/2;if(End-start = =1){if(A[start] > A[end]) {inttemp = A[start];                A[start] = A[end];            A[end] = temp; }        }Else if(End-start <=0){        }Else{Merge (A, start, mid, result); Merge (A, Mid +1, end, result);intPointera = start;intPointerb = mid +1; for(inti = start; I < (End-start +1); i++) {if(A[pointera] < A[pointerb]) {if(Pointera <= mid)                        {Result[i] = A[pointera];                    pointera++; }Else{Result[i] = A[pointerb];                    pointerb++; }                }Else{if(Pointerb <= end)                        {Result[i] = A[pointerb];                    pointerb++; }Else{Result[i] = A[pointera];                    pointera++; }                }            }        }    }

The path of data structure and algorithm learning: merge sort

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.