Divide and conquer method and recursive programming step

Source: Internet
Author: User

Divide and conquer method is a very powerful algorithm design method. The basic idea is: the original problem is decomposed into several small but similar to the original problem of sub-problem, the recursive solution of these sub-problems, and then merge the solutions of these sub-problems to establish the solution of the original problem.

In the divide-and-conquer strategy, one problem is solved recursively, and the following three steps are applied to each level of recursion:

(1) Decomposition (Divide): The original problem is decomposed into some sub-problems, the form of sub-problems as the original problem, but smaller.

(2) Settlement (conquer): Solve the sub-problem recursively. If the sub-problem size is small enough, then stop recursion and solve directly.

(3) Merging (Combine): The solution of the sub-problem is combined into the solution of the original problem.

The thought of divide and cure is embodied in the coding, often is the form of recursion. In the actual coding, you can follow the following steps:

(1) Design the function prototype carefully, including the entry, the parameter and the return value.

(2) Call the function in (1) to handle each sub-problem, and assume that the sub-problem has been resolved.

(3) deal with the specific details of the sub-problem merging.

(4) To deal with the basic situation.

(5) Move the code in (4) to the front of the function body and organize the code structure.

Example 1: Recursive insert sort

In order to sort A[1..N], we recursively sort a[1..n-1], and then insert a[n] into the sorted array a[1..n-1].

First step: Design function prototypes

void my_insertion_sort (intintint// to sort elements between array a[left...right], Left and right are subscripts, starting with 0. {}

Step Two: Call the function to solve the sub-problem

a void my_insertion_sort (intintint// ) Sorts the elements between an array a[left, ... right], Left, and right are subscripts, starting with 0.  {    1// call function to array a[left. right-1] Element sort         // array a[ Left: RIGHT-1] has been sequenced, and the next step is to insert a[right] in the appropriate position in A[left. Right-1]. }

Step Three: Merging sub-issues

The next code to be written is to insert a[right] into the appropriate location in A[left. Right-1], as follows:

voidMy_insertion_sort (intA[],intLeftintRight//sorts the elements between the array a[left, ... right], the left, and all the subscripts, starting with the value 0. {My_insertion_sort (A, left, right-1);//call function to sort elements between array a[left. right-1]//Insert a[right] into a[left. right-1]    intj = Right-1; inttemp =A[right];  while(j >= Left && temp <A[j]) {A[j+1] =A[j]; --J; } a[j+1] =temp; //at this point, a[right] has been inserted into the appropriate position in A[left. right-1] .}

Fourth step: Handling the basic situation

View function prototype void my_insertion_sort (int a[],int left,in right); found that when left equals right, the sort interval is an element and is returned directly.

voidMy_insertion_sort (intA[],intLeftintRight//sorts the elements between the array a[left, ... right], the left, and all the subscripts, starting with the value 0. {My_insertion_sort (A, left, right-1);//call function to sort elements between array a[left. right-1]//Insert a[right] into a[left. right-1]    intj = Right-1; inttemp =A[right];  while(j >= Left && temp <A[j]) {A[j+1] =A[j]; --J; } a[j+1] =temp; //at this point, a[right] has been inserted into the appropriate position in A[left. right-1] .//when left = = right is the basic situation, it is returned directly.     if(left = =Right )return;}

Fifth step: Optimize the code structure and move the code for step fourth to the front.

voidMy_insertion_sort (intA[],intLeftintRight//sorts the elements between the array a[left, ... right], the left, and all the subscripts, starting with the value 0. {    //when left = = right is the basic situation, it is returned directly.     if(left = =Right )return; My_insertion_sort (A, left, right)-1);//call function to sort elements between array a[left. right-1]//Insert a[right] into a[left. right-1]    intj = Right-1; inttemp =A[right];  while(j >= Left && temp <A[j]) {A[j+1] =A[j]; --J; } a[j+1] =temp; //at this point, a[right] has been inserted into the appropriate position in A[left. right-1] .}

or preferably,

voidMy_insertion_sort (intA[],intLeftintRight//sorts the elements between the array a[left, ... right], the left, and all the subscripts, starting with the value 0. {    if(Left < right)//working with boundary conditions{My_insertion_sort (A, left, right-1);//call function to sort elements between array a[left. right-1]//Insert a[right] into a[left. right-1]        intj = Right-1; inttemp =A[right];  while(j >= Left && temp <A[j]) {A[j+1] =A[j]; --J; } a[j+1] =temp; //at this point, a[right] has been inserted into the appropriate position in A[left. right-1] .    }    }

Example 2: Merge sort

Merge sort is a typical example of divide-and-conquer thought.

The first step: Design the function prototype:

For example, to sort the elements between the array a[left...right], you can design the following prototype:

void my_merge_sort (int a[],int left,in//  and right are subscript for the sort interval, The value starts at 0. {}

Step Two: Call the function to solve the sub-problem:

We will divide the sort interval into two parts, and call our function to solve it on these two parts.

void my_merge_sort (int a[],int left,in//  and right are subscript for the sort interval, The value starts at 0.  {    int2;    // the array to be sorted is divided into two parts, and the recursive solution    My_merge_sort (a,left,mid);      // Sort the first half of an array    My_merge_sort (a,mid+1, right); // to sort    the second half of an array // the first and last parts of the array are already sorted, followed by merging. }  

Step Three: Merging sub-issues

The

Design a function merge (int a[],int l,int m,int R) to handle the merging of two sorted arrays, which is not an implementation.

void my_merge_sort (int a[],int left,in//  and right are subscript for the sort interval, The value starts at 0.  {    int2;    // the array to be sorted is divided into two parts, and the recursive solution    My_merge_sort (a,left,mid);      // Sort the first half of an array    My_merge_sort (a,mid+1// sort the second half of the array
// call a well-written function to merge the front and back parts of the array }

Fourth step: Handling the basic situation

For the prototype of the previously designed sort function: void my_merge_sort (int a[],int left,in right); When left equals right, the sort interval is an element and is returned directly.

voidMy_merge_sort (intA[],intLeftinchright);//left and right are the subscripts for the sort interval, starting with a value of 0. {     intMid = (left + right)/2;//the array to be sorted is divided into two parts, and the recursive solutionMy_merge_sort (A,left,mid);//sort the first half of an arrayMy_merge_sort (a,mid+1, right);//to sort the second half of an arraymerge (a,left,mid,right);//call a well-written function to merge the front and back parts of the array//when left = = right is the basic situation, it is returned directly.     if(left = =Right )return;}

Fifth step: Optimize the code structure and move the code for step fourth to the front. The code structure at this point is as follows:

voidMy_merge_sort (intA[],intLeftinchright);//left and right are the subscripts for the sort interval, starting with a value of 0. {    //when left = = right is the basic situation, it is returned directly.     if(left = =Right )return; intMid = (left + right)/2;//the array to be sorted is divided into two parts, and the recursive solutionMy_merge_sort (A,left,mid);//sort the first half of an arrayMy_merge_sort (a,mid+1, right);//to sort the second half of an arraymerge (a,left,mid,right);//call a well-written function to merge the front and back parts of the array  }

or preferably,

voidMy_merge_sort (intA[],intLeftinchright);//left and right are the subscripts for the sort interval, starting with a value of 0. {    if(Left <Right ) {        intMid = (left + right)/2;//the array to be sorted is divided into two parts, and the recursive solutionMy_merge_sort (A,left,mid);//sort the first half of an arrayMy_merge_sort (a,mid+1, right);//to sort the second half of an arraymerge (a,left,mid,right);//call a well-written function to merge the front and back parts of the array    }  }

Divide and conquer method and recursive programming step

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.