算法導論 算法
Algorithms are an integral part of the development world. Before starting coding of any software first an effective algorithm is designed to get desired outputs. In this article, we will understand what are algorithms, characteristics of algorithms, some examples of famous algorithms, Types of algorithms etc..
算法是開發領域不可或缺的一部分。 在開始對任何軟件進行編碼之前,必須先設計一種有效的算法以獲得所需的輸出。 在本文中,我們將了解什么是算法,算法的特征,著名算法的一些示例,算法的類型等。
Let's get started...
讓我們開始吧...
什么是算法? (What is an Algorithm?)
It is a combination of a sequence of finite steps to solve a particular problem. or, It is a well-defined procedure which takes zero or more input and must produce at least one output to solve a particular problem.
它是解決特定問題的一系列有限步驟的組合。 或者,這是一個定義明確的過程,需要零個或多個輸入,并且必須產生至少一個輸出才能解決特定問題。
算法的特性/特征 (Properties/Characteristics of Algorithms)
Input: It may take zero or more input.
輸入:可能需要零個或多個輸入。
Output: It must produce at least one output.
輸出:它必須產生至少一個輸出。
Definiteness (Unambiguous): Every step in algorithm should be well defined, unique, precise.
確定性(明確):算法中的每個步驟都應定義明確,唯一,精確。
Finiteness (Limited): Every algorithm should contain a finite number of steps and should produce a result infinite amount of time.
有限(有限):每種算法都應包含有限數量的步驟,并且應產生無限長的時間。
Effectiveness: Operations used in algorithm must be simple and easy to understand.
有效性:算法中使用的運算必須簡單易懂。
Language independent.
語言無關。
Note:
注意:
An algorithm is a step by step procedure to solve a particular problem whereas a program is an algorithm that is encoded in any programming language.
算法是解決特定問題的逐步過程,而程序是以任何編程語言編碼的算法。
Program is language dependent and algorithm is language independent.
程序與語言有關,而算法與語言無關。
算法符號 (Notation of an Algorithm)
Name of the algorithm: It should specify the problem to be solved.
算法名稱:應該指定要解決的問題。
Step no.: It is an identification tag ( step numbering ) that specify the numbering of steps/statements. It is a positive integer.
步驟編號:這是一個標識標簽(步驟編號),用于指定步驟/語句的編號。 它是一個正整數。
Explanatory comments: It is used to specify the meaning of instruction that is used in the algorithm. It is used to understand the logic of operations by the use of [ ] for comments in the algorithm.
解釋性注釋:它用于指定算法中使用的指令的含義。 通過在算法中使用[]進行注釋,可以理解操作的邏輯。
Termination: Generally it is a STOP statement and the last statement of an algorithm that denoted ending of the algorithm.
終止:通常,它是STOP語句,并且是算法的最后一條語句,表示該算法的結尾。
例 (Example)
Algorithm for addition of two numbers:
兩個數相加的算法:
ADD( A , B )
Step 1: Read A,B
Step 2: sum=A+B [ A & B are added and their value is stored in sum ]
Step 3: PRINT ‘Sum of A & B =’, sum
Step 4: STOP
This is an algorithm, the corresponding program will be different for different languages like for C language it is:
這是一種算法,不同的語言(例如C語言)的相應程序將有所不同:
#include<stdio.h>
int main()
{
int num1,num2,opt;
printf("Enter the first Integer:\n");
scanf("%d",&num1);
printf("Enter the second Integer:\n");
scanf("%d",&num2);
printf("Enter an correct option -> 1:addition 2: subtraction 3: multiplication 4: division -> \n");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("\nAddition of %d and %d is: %d",num1,num2,num1+num2);
break;
case 2:
printf("\nSubstraction of %d and %d is: %d",num1,num2,num1-num2);
break;
case 3:
printf("\nMultiplication of %d and %d is: %d",num1,num2,num1*num2);
break;
case 4:
if(num2==0)
{
printf("OOps Devide by zero\n");
}
else
{
printf("\n Division of %d and %d is: %d",num1,num2,num1/num2);
}
break;
default:
printf("\n Enter correct option\n");
}
return 0;
}
Output
輸出量
Enter the first Integer: 10
Enter the second Integer: 20
Enter an correct option -> 1:addition 2: subtraction 3: multiplication 4: division -> 3Multiplication of 10 and 20 is: 200
算法類型 (Types of Algorithm)
Divide and conquer algorithm
分而治之算法
Greedy algorithm
貪心算法
Dynamic programming
動態編程
Branch and bound algorithm
分支定界算法
Back Tracking
回溯
Simple Recursive algorithm
簡單遞歸算法
Randomized algorithm
隨機算法
Brute force algorithm
蠻力算法
This was just the basic understanding of algorithm world. Read more articles/tutorials on Algorithms.
這只是對算法世界的基本了解。 閱讀有關算法的更多文章/教程 。
翻譯自: https://www.includehelp.com/algorithms/introduction-to-algorithms.aspx
算法導論 算法