Algorithms and Flowcharts

 

Mr Proddaturi

Writing algorithms

•One of the oldest known algorithms was written by Euclid over 2000 years ago

•It’s designed to find the greatest common divisor of two numbers (the largest number which divides both of them)

The GCD algorithm

  • •Suppose x and y represent two whole numbers

    •Euclid’s algorithm goes like this:

      r = 1

      WHILE r <> 0 

      r = x mod y

      x = y

      y = r

      ENDWHILE

      OUTPUT y

    •A flowchart is a visualisation of an algorithm

     

It’s designed to find the greatest common divisor of two numbers (the largest number which divides both of them)

Strategies for algorithm design

•‘Divide and conquer’ is a very efficient strategy

•This involves finding a solution to a sequence of smaller, related problems until the instance is small enough to be solved directly

•Suppose you want to look up the meaning of “meander” in a hard-copy dictionary

•What would be your strategy to find the right page?

Binary search

•This is the principle of the binary search

•The middle name is examined first, and if that is not the name sought, half the list is discarded

•Which items would need to be examined to find the name Zane in this list of names?

Arthur  Chloe  Daisy  Eric  Holly  Jon  Liam  Mark  Rowan  Zane 

Try it out!

•Ask a partner to think of a number between 1 and 100

•For each guess you make, your partner will tell you whether you are too high, too low or on the button

•How many guesses will you need?

•How many if the number is between 1 and 1024?

•How many guesses, on average, using a sequential search?

Sample problem

A well-known celebrity is among a group of people at a gathering. The celebrity knows none of the other guests, but everybody knows the celebrity

•How can you identify the celebrity by repeatedly asking the question “Do you know this person?”

The other guests may, or may not know each other

Solution

•If there are n (where n = 2 or more) people at the gathering, you can select two people from the group, and ask one of them (Person A)  “Do you know this person?” (Person B)

•If A knows B, then remove A from the people who could be the celebrity. If A does not know B, remove B from the group who could be the celebrity.

•Then solve the problem for the remaining group of

n-1 people

•Repeat until there are only 2 people left

Sample problem

•You are given nine sweets and four paper bags

•You are required to place
an odd number of sweets in each bag

•Can you come up with a solution?

•Note: All sweets are to be used and none eaten!

Draw a flowchart of GCD algorithm

It’s designed to find the greatest common divisor of two numbers (the largest number which divides both of them)

The GCD algorithm & Flowchart

  • •Suppose x and y represent two whole numbers

    •Euclid’s algorithm goes like this:

      r = 1

      WHILE r <> 0 

      r = x mod y

      x = y

      y = r

      ENDWHILE

      OUTPUT y

    •A flowchart is a visualisation of an algorithm

Algorithms and Flowcharts

By nproddaturi

Algorithms and Flowcharts

  • 26