What is an algorithm

Source: Internet
Author: User
What is a program? program = data structure + algorithm.

For object-oriented programming, emphasis is on the data structure, but for the process-oriented programming language such as C, P a s c a l, F O RT R a N and other languages, the main concern is the algorithm. Master algorithm, but also for object-oriented programming to lay a solid foundation. So, what is an algorithm?

When people use computers, they use computers to deal with different kinds of problems, to do this, people must be in advance to all kinds of problems to analyze, to determine the specific methods and steps to solve the problem, and then compiled a set of instructions to the computer to execute the program, to the computer, so that the computer in accordance with the steps specified by people to work effectively. These concrete methods and steps, in fact, is to solve a problem of the algorithm. According to the algorithm, writing a sequence of commands that the computer executes according to some rules is the programming, and the rules that should be followed when writing is the grammar of a language.

This shows that one of the key to program design is to solve the method and step, is the algorithm. The key point of learning high-level language is to master analysis and problem solving methods, that is, exercise analysis, decomposition, finally summed up the ability of the algorithm. corresponding to it, the specific language, such as C language syntax is a tool, is a concrete implementation of the algorithm. So in advanced language learning, on the one hand should be proficient in the language of the grammar, because it is the basis for the implementation of the algorithm, on the other hand must recognize the importance of the algorithm, strengthen thinking training to write high-quality procedures.

Here's an example to illustrate how to design an algorithm:

[Example 1-4] Enter three numbers, and then output the largest number.

First of all, there is a place to install these three numbers, we define three variables a, B, C, and then enter three numbers in a, B, C, in addition, prepare a m A x to the maximum number. Since the computer can only compare two digits at a time, we first put A and B, the large number into m a x, and then the M a X and C, and then put the large number into m a x. Finally, the M a x is output, at which point m A x is loaded with the largest number in a, B, c three. Algorithm can table

Shown below:

1 Enter a, B, C.

2 A and B of the large one into M a x.

3 Put the large one in C and m A x into M a x.

4 The output M a x,m a X is the maximum number.

2, 3) Two steps are still ambiguous, can not be directly converted to the program statement, you may continue to refine:

2 Put A and b of the large one into m a x, if a > B, then max←a, otherwise max←b.

3 The C and m A x of the large one into the M a x, if C > m A x, then m a x←c.

So the algorithm can finally be written:

1) input a,b,c.

2 if A > B, then max←a;

otherwise m A x←b.

3 if C > M a X, then m a x←c.

4 The output M a x,m a X is the maximum number.
Such an algorithm can be easily converted into the corresponding program statement.

[Example 1-5] monkey eating peach problem: There are a lot of peaches do not know the number, the monkey ate half of the first day, feel not happy, and eat a more, the next day according to this processing, eat half of the remaining Peaches plus one, every day, to the tenth morning, the monkey found only a peach, asked how many peaches originally?

This problem seems to be somewhat impossible to begin with, so how to start? Suppose the first day begins with a A1 peach, and the next day there is A2 only, ..., the 9th Day has A9 only, 1th 0 days is A1 0 only, in A1, A2, ..., A1 0, only a1 0 = 1 is known, now requires A1, and we can see, A1, A2,. ., there is a simple relationship between A1 0:

A9= 2 * (A1 0+ 1)

A8= 2 * (a9+ 1)

A1= 2 * (a2+ 1)

namely: ai= 2 * (AI + 1+1) i=9,8,7,6,..., 1

This is the mathematical model of this problem.

Then, it is a recursive process, which is often used in computer problem solving, to study the calculation process from a9,a8 to A1. On the other hand, the nine-step operation from the form exactly the same, different is only the AI subscript. As a result, we introduce the method of cyclic processing, and unify the A0 to represent the number of peaches the previous day, A1 the number of peaches in the days after that, rewrite the algorithm as follows:

1) a1=1; {The number of peaches in the 1th 0 days, the initial value of A1}

i = 9. {Counter initial value is 9}

2) a0= 2 * (a1+ 1). {Calculate the number of peaches for the day}

3) a1= A0. {The number of peaches for the day is the initial value for the next calculation}

4) i=i-1.

5 if I > = 1, ext. 2).

6) The output A0 value.

of which 2) ~5) steps are loops.

This is a process from the concrete to the abstract, the specific method is:

1 figure out what steps should be taken if the person does it.

2 The steps are summarized and the mathematical models are abstracted.

3 The repetition of the steps, through the use of the same variable and other ways to obtain the form of unity, and then concisely used to solve the cycle.

The description method of the algorithm has natural language description, pseudo code, flowchart, N-S graph, PA D diagram and so on.

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.