Data Structure Tutorial The third lesson algorithm and algorithm design requirements

Source: Internet
Author: User
Tags definition execution integer printf

the subject of this lesson: algorithm and algorithm design requirements

Teaching Purpose: to master the definition and characteristics of algorithm, the requirements of algorithm design

Teaching emphases: the characteristic of algorithm, the design requirement of algorithm

Teaching Difficulty: The requirement of algorithm design

Teaching Content:

Definition and characteristics of the algorithm

1. Definition:

Ispass (int num[4][4])

{int i,j;
for (i=0;i<4;i++)

for (j=0;j<4;j++)

if (num[i][j]!=i*4+j+1)/* An instruction, multiple operations * *

return 0;

return 1;
}/* above is a similar Huarong game to determine whether the game is the end of the algorithm * *

An algorithm is a description of a specific problem-solving step, which is a finite sequence of instructions, where each instruction represents one or more operations; In addition, an algorithm has the following five important features:

2, the algorithm of the five characteristics:

Have a poor sex An algorithm must always (for any valid input value) end after a poor execution, and each step can be completed in a poor time;
Certainty Each instruction in the algorithm must have the exact meaning, the reader does not produce two semantic when understanding. Under any condition, the algorithm has only one execution path, that is, only the same output can be obtained for the same input.
Feasibility An algorithm can be done, that is, the operation described in the algorithm can be implemented through the implementation of the basic operation of a finite number of times to achieve.
Input An algorithm has 0 or more inputs, which are taken from a collection of specific objects.
Output An algorithm has one or more outputs. These outputs are quantities that have certain relationships with the input.

Cases:

poor

haha ()
{/*only A Joke,do nothing.*/
}
Main ()
{printf (Please wait ...) You will know the day of the world ... ");
while (1)
haha ();
}

deterministic

Flo At average (int *a,int num)
{int I;long sum=0;
for (i=0;i<num;i++)
sum+=* (a++);
return sum/num;
}
Main ()
{int score[10]={1,2,3,4,5,6,7,8,9,0};
printf ("%f", average (score,10);
}

feasibility  
output

getsum (int num)
{
int i, sum=0;
for (i=1;i<=num;i++)
Sum+=i;
}/* No output algorithm has no meaning,

Second, the requirements of algorithm design

1. Correctness

Four levels of correctness of the algorithm
program Contains no syntax errors.

max (int a,int b,int c)
{
if (a>b)
{if (a>c) return C;
else return A;
}
}

program can produce results that meet specification requirements for several sets of input data. max (int a,int b,int c)
{
if (a>b)
{if (a>c) return A;
else return C;
}
}/* 8,6,7 *///* 9,3,2 */
program for carefully selected sets of typical, harsh, and difficult groups of inputs Data can be obtained to meet the specifications of the requirements of the results.

max (int a,int b,int c)
{
if (a>b)
{if (a>c) return A;
else return C;
}
Else
{if (b>c) return B;
else return C;
}
}

The program produces results that meet specification requirements for all legitimate input data. &NBSP;

2, readability

3, the robustness of

4. Efficiency and low storage demand

Efficiency refers to the execution time of an algorithm. For multiple algorithms that solve the same problem, the algorithm with short execution time is highly efficient.

Storage requirement refers to the maximum memory space needed in the algorithm execution.

Both are related to the size of the problem.

&NBSP; algorithm one algorithm two
max (int a,int b,int c)
{if (a>b)
{if (a>c) return A;
else return C;
}
Else
{if (b>c) return B;
else return C;
}/* eliminates the need for additional storage, only two comparisons */
1 The largest of the 00 integers The same algorithm is difficult to write, difficult to read max (int a[100] )
{int c,int i;
C=A[0];
for (i=1;i<100;i++)
if (a[i]>c) c=a[i];
return C;
}
/* A total of 102 integer spaces required/

Third, summary

1, the characteristics of the algorithm

2, the algorithm design requirements: Correctness, readability, robustness, efficiency and low storage requirements.

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.