On one of the strategies of divide and conquer: merge sort

Source: Internet
Author: User

Divide-and-conquer strategy is an effective way to large-scale problems, converting some of the big problems into small problems, divide and conquer, before we talked about the insertion sort, the insertion sort is an incremental method, when the program scale increases efficiency will decline, this time we can take the method of division, this series is to merge sort as the introduction, To give you a detailed explanation of the division and Treatment method.

(a) Recursive algorithm structure to solve the problem of divide and conquer

The idea of divide-and-conquer law is to transform several large-scale problems into small-scale sub-problems, and the rational use of recursion can solve these sub-problems. Finally, merge these sub-problems.

(ii) The three steps of the split-rule model in the case of recursion

Decomposition

Solve

Merge

Merge sort

Decomposition

Solve

Merge

Specific questions we can use the merger sort of question to explain

Or use the idea of playing cards, we now have two stacks of cards, face up, the smallest in the top, now the two stacks of cards merged, side by side good order, card facing down on the table. The basic step is to pick the smallest cards from the two stacks, then arrange them in order, and put them on the table facing down to make up a deck of cards. Let's implement it in code.

We need to create two functions, merge functions, and merge sort functions.

First, we first result in the merging function:

void merge (int *a,int p,int q,int R)

{

int *tmp = new Int[r-p + 1];

int i = P;

int j = Q +1;

int k = 0;

while (i <= q&&j <= R)

{

if (A[i] <= a[j])

tmp[k++] = a[i++];

Else

tmp[k++] = a[j++];

}

while (I<=Q)

tmp[k++] = a[i++];

while (J <= R)

tmp[k++] = a[j++];

for (i = 0;i <= k;i++)

A[p +i] = tmp[i];

Delete[] tmp;

}

A dynamic array is used to temporarily store the sorted array, and finally the contents of the temporary array are stored in array a.

Next we want to write a function to merge the sort, here will use the recursive function, see the code

void mergesort (int *a,int p,int R)

{

if (P < r)

int q = (p + r)/2;

MergeSort (A,p, q);

MergeSort (a,p + 1, R);

Merge (A, p, Q, R);

}

By constantly invoking itself, a sequence containing only one item is formed, merging them into a sequence of length two, and so on, resulting in a complete sequence.

The test results are as follows:



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.