yt

Header Ads

Chapter -1 Introduction of Algorithm and Data Structure -Technology369kk

  Introduction of Algorithm and Data Structure

Post Intro :-In this post we will learn algorithm and data structure in DSA Tutorial in details So let's go start now. 

Table of Content


Introduction of Algorithm 

The Study of algorithm is fundamental to computer science curricula. The word algorithms is derived from the name of Abu Ja far Mohammad ibn al Khwarizmi, an 825 A.D. Persian mathematician. The dictionary meaning of algorithm given in Webster's Ninth New Collegiate Dictionary is A Procedure for solving a  mathematical problem(as of finding the greatest common divisor) in a finite number of steps that frequently involves repetition of an operations. Broadly: a  step - by - step producers for solving a problem or accomplishing some end.  
                    The meaning of algorithm has gone through many changes but what we are interested in it, its meaning that helps us write better computer programs. 

For the purpose of the subject of "Data Structures and Algorithms", algorithm can be defined as - a finite sequence of computational steps that takes a finite set of values as input and produces a finite set of values as input and solves some computational problem in finite amount of time. Usually a computational procedure also does the same thing but it may or may not terminate. As per the definition, an algorithm has to terminate after finite time. There are no fixed values for finiteness of input, output, sequence of steps and time involved These can be as large as required but must not be infinite. Accordingly, an algorithm may take years to terminate but we must be able to prove analytically that the algorithm would terminate.

Definition:  

  • An set of rules is a step by step manner to solve a trouble, where every step suggests an intermediate undertaking. Set of rules carries finite variety of steps that results in the solution of the problem..

 Features of an Algorithm:

Algorithm has the following basic properties 

  • Input-Output:- Algorithm takes ‘0’ or more input and produces the required output. This is the basic characteristic of an algorithm. 
  • Finiteness:- An algorithm must terminate in countable number of steps. 
  • Definiteness: Each step of an algorithm must be stated clearly and unambiguously. 
  • Effectiveness: Each and every step in an algorithm can be converted in to programming language statement. 
  • Generality: Algorithm is generalized one. It works on all set of inputs and provides the required output. In other words it is not restricted to a single input value

Types of Algorithm 

  • Sequence
  • Selection and 
  • Iteration 

Sequence:

  • The steps described in an algorithm are performed successively one by one without skipping any step. 
  • The sequence of steps defined in an algorithm should be simple and easy to understand. Each instruction of such an algorithm is executed, because no selection procedure or conditional branching exists in a sequence algorithm.

Example: 

  • Step 1: Start
  • Step 2: read a, b, c;
  • Step 3: sum =a + b;
  • Step 4: write sum
  • Step 5: Stop  

Selection : 

  • The sequence type of algorithms are not sufficient to solve the problems, which involves decision and conditions.
  •  In order to solve the problem which involve decision making or option selection, we go for Selection type of algorithm. 
  • The general format of Selection type of statement is as shown below.

Example: 

if(condition) 
Statement-1; 
else 
Statement-2; 


Example Explanation :- The above syntax specifies that if the condition is true, statement-1 will be executed otherwise statement-2 will be executed. In case the operation is unsuccessful. Then sequence of algorithm should be changed/ corrected in such a way that the system will re execute until the operation is successful.

Example 1: Check the age of a persons eligibility for vote. 

  • Step 1: Start
  • Step 2: read age
  • Step 3: if age >= 18 then steps_4 run else steps_5 run  ;
  • Step 4: Write "You are eligible for vote"
  • Step 5: Write "You are not eligible for vote"
  • Step 6: Stop

Example 2 : Check Biggest number between two numbers 

  • Step 1: Start
  • Step 2: read a, b
  • Step 3: if a >b then
  • Step 4: Write "A is  biggest number of B "
  • Step 5: else
  • Step 5: Write "B is biggest number of A " 
  • Step 6: Stop

Iteration:

  • Iteration type algorithms are used in solving the problems which involves repetition of statement. In this type of algorithms, a particular number of statements are repeated ‘n’ no. of times. 

Example : Sum of 10 natural number

  • Step 1 : Start
  • Step 2 : read n 
  • Step 3 : repeat Step 4 until n>0
  • Step 4:      (a) r=n mod 10 
  •                  (b) s=s+r 
  •                  (c) n=n/10 
  • Step 5 : write s 
  • Step 6 : Stop 

Complexity of Algorithm: 

In this case of Complexity of algorithm can be efficiency of an algorithm can be measured by the two types :
  1. Time Complexity   
  2. Space  Complexity

1. Time Complexity: 

  • Time Complexity of a computer programs tells us the amount of time the program would take to complete a job, Usually, the program takes time on an advanced computer than on older.
  • Time Complexity of a program  depends on number of things like, time a complier takes to translate the program into machine code, exception time, number and types of mathematical computers carried out, etc. 
  •  The amount of time required for an algorithm to complete its execution is its time complexity.
  • An algorithm is said to be efficient if it takes the minimum (reasonable) amount of time to complete its execution.

2. Space Complexity:

  • The amount of space occupied by an algorithm is known as Space Complexity. 
  • An algorithm is said to be efficient if it occupies less space and required the minimum amount of time to complete its execution.

Chapter Exercise:

 Q1. Write a algorithm for roots of a Quadratic Equations?   

  • Step 1 : START 
  • Step 2 : reads a, b, c 
  • Step 3 : if(a = 0) then execute  steps_4 else step_5
  • Step 4 : Write "Given Numbers is a linear equations 
  • Step 5 : d=(b*b)_(4*a*c) 
  • Step 6 : if (d>0) then step_7 else_8 
  • Step 7 : Write "Roots are real and Distinct"
  • Step 8 : if (d>0) then step_9 else step_10
  • Step 9 : Write "Roots are real and equal "
  • Step 10 : Write "Roots are imaginary"
  • Step 11 : Stop

Q2. Write an algorithm find the largest among three numbers.

  • Step 1: START 
  • Step 2: declared variable a, b and c
  • Step 3: read a, b, c 
  • Step 4:
  •  if a>b
    • if a>c
    • Write " A is greatest " 
    • else 
    • Write " C is greatest " 
  • else 
    • if b>c
    • Write " B is greatest "
    • else 
    • Write " C is greatest"
  • Step 5: STOP

Q3. Write an algorithm find the factorial numbers. 

  • Step 1: START 
  • Step 2: declared variable num, fact and i
  • Step 3: Initialize variables  in fact = 1 and i= 1 
  • Step 4:Read value num
  • Step 5: 
    • Repeat the steps until i=num
    • fact = fact *i
    • i=i+1
  • Step 6: Write "Your Factorial value is "
  • Step 7: Display fact
  • Step 8: STOP

Q4. Write an algorithm find the simple interest .

  • Step 1: START 
  • Step 2: read P, R, T, store, 
  • Step 3: Calculate  store = (P*T*R)/100
  • Step 4: Print store 
  • Step 5: STOP
I Hope this post is helpful for you, Next post is similar uploaded.. 

No comments

Theme images by Dizzo. Powered by Blogger.