26
Algorithms and Programming Lect 3

Algorithms and Programming

Embed Size (px)

DESCRIPTION

Algorithms and Programming. Lect 3. Algorithm. An algorithm is a finite step-by-step sequence of instructions for carrying out some task Algorithms predate computers, e.g. Euclid's algorithm for finding the greatest common factor Find average of some values. Finding the Oldest (1). - PowerPoint PPT Presentation

Citation preview

Algorithms and Programming

Lect 3

Algorithm

An algorithm is a finite step-by-step sequence of instructions for carrying out some task

Algorithms predate computers, e.g. Euclid's algorithm for finding the greatest

common factor Find average of some values

Finding the Oldest (1)1. ask the first person to state his or her

name and birthday, then write this information down on a piece of paper

2. for each successive person in line:• ask the person for his or her name

and birthday• if the stated birthday is earlier than

the birthday on the paper, cross out old information and write down the new

3. at the end of the line, the name and birthday of the oldest person will be on the paper

Oldest (1)

Find the Oldest (2)1. line up all the people along one wall

2. as long as there is more than one person in the line, repeatedly

• have the people pair up– if there is an odd number of people, the last person will be without a partner

• ask each pair of people to compare their birthdays and request that the younger of the two leave the line

3. when there is only one person left in line, that person is the oldest

Oldest(2)

Algorithm Analysis algorithm 1 involves asking each person’s

birthday and then comparing it to the birthday written on the page the amount of time to find the oldest

person is proportional to the number of people

if you double the amount of people, the time needed to find the oldest person will also double

Algorithm Analysis algorithm 2 allows you to perform multiple

comparisons simultaneously the time needed to find the oldest person is

proportional to the number of rounds it takes to shrink the line down to one person -- the logarithm (base 2) of the number of people

if you double the number of people, the time increases by the cost of one more comparison

requires carrying out comparisons in parallel (special hardware is the price you have to pay)

algorithm vs. logarithm

the words algorithm and logarithm are similar – do not be confused

algorithm: a finite step-by-step sequence of instructions for carrying out a task logarithm: the exponent to which a base is raised to produce a number

e.g., 210 = 1024, so log2(1024) = 10

Searching

A common problem in computer science involves storing and maintaining large amounts of data, and then searching the data for particular value.

SearchingThere are two commonly used algorithms for

searching a list of items

1. sequential search • general purpose• relatively slow

2. binary search • data must be sorted • faster• guess my number

Searching

Algorithm 1 takes time proportional to the number of values

Algorithm 2 is again proportional to the log2.

Algorithms

There may be many algorithms to solve a given problem. Some algorithms may be faster than others some algorithms may require different

resources (memory, special hardware).

Programming

In order to use a computer to solve a problem, we go through a series of steps. Determine how to solve the problem and

come up with an algorithm (a finite sequence of steps to solve the problem).

Formulate the algorithm in a way that a computer can use to carry out the steps.

A program is a set of instructions to solve a problem written in a manner that a computer can use (not in English).

Programming

A computer understands only its own machine language.

We write the program in a high-level language. It uses English-like words.

A program called a compiler or interpreter "translates" the program written in the high-level programming language to machine language.

Machine Language machine language consists of instructions that

correspond directly to the hardware operations of a particular machine i.e., instructions deal directly with the computer’s physical components machine language instructions are written in

binary programming in machine language is tedious

and error prone code is nearly impossible to understand and

debug Each machine has its own machine language.

Languages in the early 1950’s, assembly languages evolved

from machine languages substitute words for binary codes much easier to remember and use, but still a

low level of abstraction in the late 1950's, high-level languages were

introduced allow the programmer to write code closer to

the way humans think a much more natural way to solve problems programs are machine independent

Translation Using a high-level language, the programmer is

able to reason at a high-level of abstraction But programs must still be translated into

machine language that the computer hardware can understand/execute. Inside a computer, everything is binary.

Two standard approaches to program translation Interpreter – translate and execute each

instruction Compiler -- translate entire program

Translating a speech an interpreter can be used to provide real-time

translation the interpreter hears a phrase, translates,

and immediately speaks the translationADVANTAGE: the translation is immediateDISADVANTAGE: if you want to hear the

speech again, must interpret all over again

Translating a speech a translator (or compiler) translates the entire

speech offline the translator takes a copy of the speech

and translates the entire speech ADVANTAGE: once translated, it can be

read over and over very quicklyDISADVANTAGE: must wait for the entire

speech to be translated

Compiler vs. Interpreter

Compilation is done once and then the compiled version can be reused many times. Often, that’s what’s sold.

Interpretation can be done before the whole program is available, but has to be repeated each time the program is used.

Interpreter is suitable for Javascript, because the program can be interpreted as the page is being loaded, doesn't have to wait for the whole page to download.

Debugging

A bug is an error in a program Debugging is removing the errors without

introducing new ones. The compiler may find errors - mistakes in

the way the program was formulated - and will not be able to complete the translation process. Fix the errors and repeat the process until the program compiles successfully.

Debugging

Run (or execute) the program. At this point, we may find other errors and we may have to correct the program again.

Test the program. Test it and see if there are any mistakes. Fix errors and test again.

Software Life-Cycle Determine an algorithm to solve the problem Write a program to implement the algorithm:

use very specific, detailed instructions use a “language” the computer understands.

Compile the program: A compiler translates the program into machine language. Fix any syntax errors that the compiler finds.

Run (or execute) the program: At this time we may find other errors that must be corrected.

Test the program: Test the program and fix any errors.

Maintain the program: Keep monitoring the program for unexpected errors. Make changes

Examples of Bugs

The Mars Climate Orbiter was lost in space in 1999. In the calculation of its route, some programmers used the metric system while others used English units!

A Patriot missile failed to intercept a scud fired at US troops in 1991. The time in tenths of a second, as measured by the system’s internal clock, was multiplied by 10 to produce the time in seconds.

For the future

A recipe is a good analogy for a program: ingredients

Constants, variables data types, integer and real numbers, characters

detailed step-by-step-instructions statements

repeated actions (e.g., separate 5 eggs) loops

decisions (e.g., bake until brown, beat until firm, etc.) pre defined actions (e.g., saute, puree, etc.)

functions, parameters