Introduction to Computer Algorithms PDF
Document Details
Uploaded by ImprovingCrocus
كلية العلوم
Tags
Summary
This document provides an introduction to algorithms, discussing what algorithms are, how they work, and their characteristics. It also introduces pseudocode as a way to represent algorithms, and briefly goes over designing an algorithm, including its prerequisites. It ends by providing an illustrative example related to the addition of three numbers.
Full Transcript
Chapter 7 Algorithms What is Algorithm? Algorithm Basics The word Algorithm means “a process or set of rules to be followed in calculations or other problem-solving operations”. Algorithm refers to a set of rules/instructions that step-by-step define how a work...
Chapter 7 Algorithms What is Algorithm? Algorithm Basics The word Algorithm means “a process or set of rules to be followed in calculations or other problem-solving operations”. Algorithm refers to a set of rules/instructions that step-by-step define how a work is to be executed upon in order to get the expected results. It can be understood by taking an example of cooking a new recipe. To cook a new recipe, one reads the instructions and steps and execute them one by one, in the given sequence. Algorithm in The result thus obtained is the new dish cooked General Life perfectly. Similarly, algorithms help to do a task in programming to get the expected output. Pseudocode: Pseudocode is a simple way of writing programming code in English. Pseudocode is not actual programming language. It uses short phrases to write code for programs before you actually createit in a specific language. Examples of Pseudocode: Let'sreviewan example of pseudocode to create a program to add 2 numbers together and then display the result. Start Program Enter two numbers, A, B Add the numbers together Print Sum End Program Examples of Pseudocode: Here is a pseudocode to compute the area of a rectangle: Get the length, l, and width, w Compute the area = l*w Display the area Characteristics of an Algorithm Clear and Unambiguous: Algorithm should be clear and unambiguous. Each of its steps should be clear in all aspects and must lead to only one meaning. Well-Defined Inputs: If an algorithm says to take inputs, it should be well-defined inputs. Well-Defined Outputs: The algorithm must clearly define what output will be yielded and it should be well-defined as well. Finite-ness: The algorithm must be finite, i.e. it should not end up in an infinite loops or similar. Characteristics of an Algorithm Feasible: The algorithm must be simple, generic and practical, such that it can be executed upon will the available resources. It must not contain some future technology. Language Independent: The Algorithm designed must be language-independent, i.e. it must be just plain instructions that can be implemented in any language, and yet the output will be same, as expected. Advantages of Algorithms It helps to understand the problem. Algorithm is a step-wise representation of a solution to a given problem. In Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the programmer to convert it into an actual program. How to Design an Algorithm? In order to write an algorithm, following steps are needed as a pre- requisite: 1. The problem that is to be solved by this algorithm. 2. The constraints of the problem that must be considered while solving the problem. 3. The input to be taken to solve the problem. 4. The output to be expected when the problem the is solved. 5. The solution to this problem, in the given constraints. Add 3 numbers Example Step 1: Fulfilling the pre-requisites The problem that is to be solved by this algorithm: Add 3 numbers and print their sum. The constraints of the problem that must be considered while solving the problem: The numbers must contain only digits and no other characters. The input to be taken to solve the problem: The three numbers to be added. The output to be expected when the problem the is solved: The sum of the three numbers taken as the input. The solution to this problem, in the given constraints: The solution consists of adding the 3 numbers. It can be done with the help of ‘+’ operator, or bit-wise, or any other method. Continue…. Step 2: Designing the algorithm Now let’s design the algorithm with the help of above pre-requisites: Algorithm to add 3 numbers and print their sum: START Declare 3 integer variables num1, num2 and num3. Take the three numbers, to be added, as inputs in variables num1, num2, and num3 respectively. Declare an integer variable sum to store the resultant sum of the 3 numbers. Add the 3 numbers and store the result in the variable sum. Print the value of variable sum END Continue…. Step 3: Testing the algorithm by implementing it. Continue…