Algorithm
o BY : IHTISHAM UL HAQ
What is Algorithm?
 An algorithm is a well-defined procedure that allows
a computer to solve a problem.
 An algorithm is a sequence of unambiguous
instructions. ...
Histroy of Algorithm
 An algorithm (pronounced AL-go-rith-um) is a
procedure or formula for solving a problem.
 The word derives from the name of the
mathematician , Mohammad Ibn-Musa-al-Khwarizmi,
who was part of the royal court in Baghdad and who
lived from about 780 to 850. Al Khwarizmi’s work is
the likely source for the word algebra as well.
How to write an algorithm?
 Keep in mind that algorithm is a step-by-step
process.
 Depending upon programming language, include
syntax where necessary.
 Include variables and their usage.
 If they are any loops, try to give sub number lists.
 Try to give go back to step number if loop or
condition fails.
 Use jump statement to jump from one statement
to another.
How to write an algorithm?
 Try to avoid unwanted raw data in algorithm.
 Use break and stop to terminate the process.
Characteristics of Algorithm
 The characteristics of a good algorithm are:
 Precision – the steps are precisely stated(defined).
 Uniqueness – results of each step are
uniquely defined and only depend on
the input and the result of the preceding steps.
 Finiteness – the algorithm stops after
a finite number of instructions are executed.
Properties of Algorithm
 An algorithm must have five properties:
 Input specified.
 Output specified.
 Definiteness.
 Effectiveness.
 Finiteness.
Types of Algorithm
 Algorithm types we will consider include:
 Simple recursive algorithms.
 Backtracking algorithms.
 Divide and conquer algorithms.
 Dynamic programming algorithms.
 Greedy algorithms.
 Branch and bound algorithms.
 Brute force algorithms.
 Randomized algorithms.
Example
 Algorithm in daily life.
 Algorithm for making a Brownies
 Ingredients:
 ½ cup butter or margarine.
 1 tea spoon vanilla extract .
 1 cup sugar.
 ½ cup cocoa.
 2 eggs .
 ½ cup flour.
Cont…
Example
 Procedure:
 1. If butter not soft, then melt butter .
 2. Blend melted butter and sugar until mixture has
creamy consistency.
 3. Add eggs and vanilla; stir .
 4. Add cocoa and flour; mix until well blended .
 5. Pour into greased round glass cake pan.
 6. Microwave for 8-9 minutes.
 7.Brownies are ready.
Example
 Algorithm in Programming.
 Write an algorithm to determine a student’s final grade and
indicate whether it is passing or failing. The final grade is
calculated as the average of four marks.
 Pseudocode:
 Input a set of 4 marks .
 Calculate their average by summing and dividing by 4.
 if average is below 50
 Print “FAIL”
 else
 Print “PASS”
Example
 Detailed Algorithm :
 Step 1: Input M1,M2,M3,M4 .
 Step 2: GRADE (M1+M2+M3+M4)/4 .
 Step 3: if (GRADE < 50)
 then
 Print “FAIL”
 else
 Print “PASS”
 End if
Example
 Pseudocode:
 Input the length in feet (Lft).
 Calculate the length in cm (Lcm) by multiplying LFT
with 30.
 Print length in cm (LCM).
Example
 Algorithm :
 Step 1: Input Lft.
 Step 2: Lcm Lft x 30.
 Step 3: Print Lcm.
Thanks

Algorithm

  • 2.
    Algorithm o BY :IHTISHAM UL HAQ
  • 3.
    What is Algorithm? An algorithm is a well-defined procedure that allows a computer to solve a problem.  An algorithm is a sequence of unambiguous instructions. ...
  • 4.
    Histroy of Algorithm An algorithm (pronounced AL-go-rith-um) is a procedure or formula for solving a problem.  The word derives from the name of the mathematician , Mohammad Ibn-Musa-al-Khwarizmi, who was part of the royal court in Baghdad and who lived from about 780 to 850. Al Khwarizmi’s work is the likely source for the word algebra as well.
  • 5.
    How to writean algorithm?  Keep in mind that algorithm is a step-by-step process.  Depending upon programming language, include syntax where necessary.  Include variables and their usage.  If they are any loops, try to give sub number lists.  Try to give go back to step number if loop or condition fails.  Use jump statement to jump from one statement to another.
  • 6.
    How to writean algorithm?  Try to avoid unwanted raw data in algorithm.  Use break and stop to terminate the process.
  • 7.
    Characteristics of Algorithm The characteristics of a good algorithm are:  Precision – the steps are precisely stated(defined).  Uniqueness – results of each step are uniquely defined and only depend on the input and the result of the preceding steps.  Finiteness – the algorithm stops after a finite number of instructions are executed.
  • 8.
    Properties of Algorithm An algorithm must have five properties:  Input specified.  Output specified.  Definiteness.  Effectiveness.  Finiteness.
  • 9.
    Types of Algorithm Algorithm types we will consider include:  Simple recursive algorithms.  Backtracking algorithms.  Divide and conquer algorithms.  Dynamic programming algorithms.  Greedy algorithms.  Branch and bound algorithms.  Brute force algorithms.  Randomized algorithms.
  • 10.
    Example  Algorithm indaily life.  Algorithm for making a Brownies  Ingredients:  ½ cup butter or margarine.  1 tea spoon vanilla extract .  1 cup sugar.  ½ cup cocoa.  2 eggs .  ½ cup flour. Cont…
  • 11.
    Example  Procedure:  1.If butter not soft, then melt butter .  2. Blend melted butter and sugar until mixture has creamy consistency.  3. Add eggs and vanilla; stir .  4. Add cocoa and flour; mix until well blended .  5. Pour into greased round glass cake pan.  6. Microwave for 8-9 minutes.  7.Brownies are ready.
  • 12.
    Example  Algorithm inProgramming.  Write an algorithm to determine a student’s final grade and indicate whether it is passing or failing. The final grade is calculated as the average of four marks.  Pseudocode:  Input a set of 4 marks .  Calculate their average by summing and dividing by 4.  if average is below 50  Print “FAIL”  else  Print “PASS”
  • 13.
    Example  Detailed Algorithm:  Step 1: Input M1,M2,M3,M4 .  Step 2: GRADE (M1+M2+M3+M4)/4 .  Step 3: if (GRADE < 50)  then  Print “FAIL”  else  Print “PASS”  End if
  • 14.
    Example  Pseudocode:  Inputthe length in feet (Lft).  Calculate the length in cm (Lcm) by multiplying LFT with 30.  Print length in cm (LCM).
  • 15.
    Example  Algorithm : Step 1: Input Lft.  Step 2: Lcm Lft x 30.  Step 3: Print Lcm.
  • 16.