algorithms-notes.doc

Embed Size (px)

Citation preview

  • 7/27/2019 algorithms-notes.doc

    1/6

    COMPUTER STUDIESO-LEVEL

    1. Introduction

    In general, an 'algorithm' is the name given to a defined set of steps used to complete a

    task.

    For instance you could define an algorithm to make a cup of tea. You start by filling thekettle, then place a teabag in the cup and so on.

    In computer terms, an algorithm describes the set of steps needed to carry out asoftware task. This mini-web takes you through the topic of algorithm

    2. Using algorithms

    In computer terms, an algorithm describes the set of steps needed to carry out a

    software task

    For example there are algorithms worked out for sorting a list efficiently. There arealgorithms to create random numbers, how to shade a pixel, how to calculate something.

    Many books have been written containing ready-made algorithms. This is excellent newsfor a programmer as it means they do not have to re-invent the wheel every time theywant to carry out a common task.

    Important note: Algorithms are *not* computer code, a programmer using 'C++' can usethe same algorithm as someone programming in 'Java', they use the same steps, justcoded differently.

    3. Variables

    Before we set out an example, you need to know about a vital part of programming.Namely the meaning of 'variable'.

    A variable is to computing as a brick is to building. It is absolutely basic to computing.

    A variable is a named value that can be changed as the program runs.

    For instance define a 'variable' called A and set it to a starting value such as 10 i.e. A =10

    Now anywhere in the program that needs to change that value can refer to A

    For example

    Let A = 10

    COMPILED BY : NISAR ALI - 1 -BEACONHOUSE-S. TOWN BOYS BRANCH

  • 7/27/2019 algorithms-notes.doc

    2/6

    COMPUTER STUDIESO-LEVEL

    then another part of the program changes it by doing something to it, like this

    A = A + 1 which adds 1 to the variable called A

    or

    A = 3 which re-defines the value of A

    or

    A = B + 1 which copies the value of another variable called B and adds 1 to it.

    A variable is actually a location in memory within the computer. The program changesthe data held in that location by refering to the variable name.

    Variables can be almost any name but it makes sense to use sensible description suchas Sales_Price and not be too long.

    It is also a good idea to avoid spaces as it can make spotting a problem very difficult.

    4. An example algorithm

    Set out an algorithm to count to 10

    1. Set a variable to a starting value of 0 (let's call the variable 'X')2. Add 1 to the current value of X3. If ten steps have taken place then finish4. If not then repeat step 2

    This does the job - it counts to 10 - but this is actually a poor algorithm because it is toospecific.

    What if the programmer wants to add to 11 instead?

    A good algorithm will be as general as possible to allow for the widest use. For instancechange the algorithm above to add up to *any* number

    1. Set a variable Y to hold the number of counts needed (say 11)2. Set a variable X to a starting value of 03. Add 1 to the current value of X

    4. Is X the same as Y yet?5. If not then repeat step three otherwise finish

    Just by using an extra variable to hold the number of additions the algorithm can nowdeal with any count.

    COMPILED BY : NISAR ALI - 2 -BEACONHOUSE-S. TOWN BOYS BRANCH

  • 7/27/2019 algorithms-notes.doc

    3/6

    COMPUTER STUDIESO-LEVEL

    5. Pseudo code

    Algorithms are often described by setting out 'pseudo-code'

    Pseudo-code does not follow any particular computer language. It lays out the algorithm

    as a series of statements written in English (or any local language). Some statementswill test for some condition and branch to different parts of the algorithm.

    Example.Write pseudo code for an algorithm that adds data to an existing file.

    1. Ensure the data to be added is present in memory2. Identify the file to be opened3. Open the file4. Move to the end of the file5. Append the data from memory to the end of the file6. Save and close the file

    6. Pseudo-code continued

    From the previous page ....

    1. Ensure the data to be added is present in memory2. Identify the file to be opened3. Open the file ........

    As it stands this pseudo code example is a bit too 'high level'. It makes a lot ofassumptions, for instance, that the person knows how to 'Ensure the data is present in

    memory'.

    If more detail is needed the pseudo code can be expanded:

    1. Ensure the data to be added is present in memory byif data is present in memory then

    carry onelse

    load data into memory

    2. Identify the file to be opened

    3. Open the file

    4. Move to the end of the file

    5. Append the data from memory to the end of the file6. Save and close the file

    Notice that the word 'if' was used in section 1 above and the term 'else'. These terms arecalled 'conditional' statements and they cause the algorithm to branch depending onsome condition being met. This kind of statement is called a 'selection' because itdetermines the next instruction to be carried out.

    COMPILED BY : NISAR ALI - 3 -BEACONHOUSE-S. TOWN BOYS BRANCH

  • 7/27/2019 algorithms-notes.doc

    4/6

  • 7/27/2019 algorithms-notes.doc

    5/6

    COMPUTER STUDIESO-LEVEL

    Start/End - the terminator symbol marks the starting or ending point of the process. Itusually contains the word "Start" or "End."

    Action or Process - a box can represent a single step ("add two cups of flour"), or an

    entire sub-process ("make bread") within a larger process.

    Decision - a decision or branching point. Lines representing different decisions emergefrom different points of the diamond.

    Input/Output - represents material or information entering or leaving the process, suchas customer order (input) or a product (output).

    Data storage -indicates a step where data gets stored. For Example "Save Orders"

    Delay - indicates a delay in the process. For example "Wait 1 day"

    9. Creating an algorithm

    It takes a bit of practice to lay out an algorithm, but the key is to apply some logicalthinking to the problem.

    For instance, find the largest number in a list

    Consider a list like this: 3,6,2,67,9,1

    Clearly 67 is the largest number. One algorithm that would find this is

    store first list item in variable largest_itemFor each item in the list

    store item in variable current_itemif current_item > largest_item then

    largest_item = current_item

    End

    COMPILED BY : NISAR ALI - 5 -BEACONHOUSE-S. TOWN BOYS BRANCH

  • 7/27/2019 algorithms-notes.doc

    6/6

    COMPUTER STUDIESO-LEVEL

    This algorithm takes the first item in the list and sets it to be the largest. Then it loopsthrough the other items and checks each one in turn, this is done by the FOR EACHstatement. The IF statement checks to see if a larger item has been found.

    COMPILED BY : NISAR ALI - 6 -BEACONHOUSE-S. TOWN BOYS BRANCH