Data Structure Course (40 courses in total) Lesson 3 algorithm and algorithm design requirements

Source: Internet
Author: User

Subject:Algorithm and algorithm design requirements

Purpose:Understanding the definition and features of algorithms and the requirements of Algorithm Design

Teaching focus:Algorithm features and design requirements

Teaching difficulties:Algorithm Design Requirements

Course content:

I. Definition and features of Algorithms

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)/* One command, multiple operations */

Return 0;

Return 1;
}/* The above is an algorithm similar to that used to determine whether the game is over */

An algorithm is a description of the specific process of solving a problem. It is a finite sequence of commands. Each Command represents one or more operations. In addition, an algorithm also has the following five important features:

2. Five features of the algorithm:

Poor An algorithm must always end after execution of a poor step (for any valid input value), and each step can be completed within a short time;
Certainty Each instruction in an algorithm must have a definite meaning, and readers will not understand it. Under any condition, the algorithm has only one execution path, that is, the same output can be obtained for the same input.
Feasibility An algorithm can do this, that is, the operations described in the algorithm can be implemented through the implementation of basic operations for a limited number of times.
Input An algorithm has zero or multiple inputs, which are taken from a set of specific objects.
Output An algorithm has one or more outputs. These outputs have some specific relationships with the input.

Example:

Poor

Haha ()
{/* Only a joke, do nothing .*/
}
Main ()
{Printf ("Please wait... you will know the days before the world ...");
While (1)
Haha ();
}

Certainty

Float 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, 4, 5, 6, 7, 8, 9, 0 };
Printf ("% F", average (score, 10 );
}

Feasibility  
Input  
Output

Getsum (INT num)
{
Int I, sum = 0;
For (I = 1; I <= num; I ++)
Sum + = I;
}/* Algorithms without output have no significance,

Ii. Requirements for Algorithm Design

1. correctness

Four levels of algorithm correctness
The program does not contain syntax errors.

Max (int A, int B, int C)
{
If (A> B)
{If (a> C) return C;
Else return;
}
}

The program can obtain results that meet the specification requirements for several sets of input data. Max (int A, int B, int C)
{
If (A> B)
{If (a> C) return;
Else return C;
}
}/* 8, 6, 7 * // * 9, 3, 2 */
The program can produce results that meet the specification requirements for the typical, demanding, and difficult input data sets carefully selected.

Max (int A, int B, int C)
{
If (A> B)
{If (a> C) return;
Else return C;
}
Else
{If (B> C) return B;
Else return C;
}
}

The program can produce results that meet the specification requirements for all valid input data.  

2. Readability

3. robustness

4. Efficiency and low storage requirements

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

The storage capacity requirement refers to the maximum storage space required during Algorithm Execution.

Both are related to the problem scale.

  Algorithm 1 Algorithm 2
Evaluate the operator in three Integers Max (int A, int B, int C)
{If (A> B)
{If (a> C) return;
Else return C;
}
Else
{If (B> C) return B;
Else return C;
}/* No additional storage space required. Only two comparisons are required */
Max (int A [3])
{Int C, int I;
C = A [0];
For (I = 1; I <3; I ++)
If (A [I]> C) C = A [I];
Return C;
}
/* Requires two additional buckets, two comparisons, at least one assignment */

/* A total of five integer data spaces are required */

Evaluate the iterator In the integer 100 The same algorithm is hard to write and hard 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;
}
/* Requires a total of 102 integer data spaces */

Iii. Summary

1. algorithm features

2. algorithm design requirements: correctness, readability, robustness, efficiency, and low storage capacity.

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.