Into the algorithm

Source: Internet
Author: User

The algorithm is considered as the cornerstone of computer science, and the algorithm theory is the design technology and analysis technology. The former answer is "to the question to be determined, how to propose an algorithm to solve?" "The question is how to design an effective algorithm to solve a pending problem," the latter replied, "is the algorithm good enough?" , that is, how to evaluate or judge the design algorithm, or how to compare and evaluate multiple algorithms for solving the same problem. The two are interdependent, the design of the algorithm needs to be tested and evaluated, the analysis of the algorithm in turn can help improve the design of the algorithm.

The algorithm design techniques are mainly divided-treatment, dynamic programming, greedy, backtracking, branch-boundary, probabilistic and approximate algorithms. For all the algorithm, rice teacher think all are used in the division of thought, beginning I do not agree, but later through the teacher explained after Rice, found that the world algorithm is a AH.

Today we will start with the first Division. Division is the idea of divide and conquer, a big problem we can't solve, then we can divide the big problem into small problems, and then to solve.

In general, the divide-and-conquer algorithm has 3 steps on each level of recursion.

1. Decomposition. Decompose the original problem into a series of questions.

2. Solve. To solve the problem of the elder brother recursively. If the sub-problem is small enough, it is solved directly.

3. Merger. The solution of the sub-problem is merged into the solution of the original problem.

The merging algorithm is a perfect example of the successful application of divide-and-conquer method, let's take a look at how he divided the rule today.

(1) Decomposition. Divides n elements into sub-sequences containing N/2 elements.

(2) solving. Recursively sorts two sub-sequences with a merge sort.

(3) merger. Merge two sorted sub-sequences to get the sorting result.

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >void mergesort (int a[],int p,int r) {int q;if (p<r) {q= (p+r)/2; MergeSort (A,P,Q); MergeSort (A,Q+1,R); Merge (A,p,q,r);}} Void Merge (int a[],int p,int q,int r) {int N1=q-p+1,n2=r-q,i,j,k;int l[50],r[50];for (i=0;i<n1;i++) l[i]=a[p+i];for (j =0;j<n2,j++) r[j]=a[q+j+1]; L[n1]=int_max; R[n2]=int_max;i=0;j=0;for (k=p;k<r+1;k++) {if (L[i]<r[i];) {a[k]=l[i];} else{a[k]=r[j];j++;}}} </span>

For this sort of merging, if we find it difficult to understand, we can understand that. First of all, we have two stacks of cards here, and they are all ordered by order. Ask us to arrange from large to small.

1. Take out the first one to compare, the big take away, the small left motionless.

2. Continue to take out the first minor and second comparison on the other side. Large to take away.

3. Proceed sequentially until you finish a stack. The rest is in order.

From the above we can also see that the algorithm is not as difficult as we imagined, afraid of fear in, we have not yet to learn, on their own to scare back.

Into the algorithm

Related Article

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.