1 / 26

Algorithms and Programming

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).

lane-walter
Download Presentation

Algorithms and Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Algorithms and Programming Lect 3

  2. 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

  3. Finding the Oldest (1) • ask the first person to state his or her name and birthday, then write this information down on a piece of paper • 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 • at the end of the line, the name and birthday of the oldest person will be on the paper

  4. Oldest (1)

  5. Find the Oldest (2) • line up all the people along one wall • 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 • when there is only one person left in line, that person is the oldest

  6. Oldest(2)

  7. 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

  8. 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)

  9. 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

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

  11. Searching There are two commonly used algorithms for searching a list of items • sequential search • general purpose • relatively slow • binary search • data must be sorted • faster • guess my number

  12. Searching • Algorithm 1 takes time proportional to the number of values • Algorithm 2 is again proportional to the log2.

  13. 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).

  14. 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).

  15. 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.

  16. 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.

  17. 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

  18. 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

  19. Translating a speech • an interpreter can be used to provide real-time translation • the interpreter hears a phrase, translates, and immediately speaks the translation • ADVANTAGE: the translation is immediate • DISADVANTAGE: if you want to hear the speech again, must interpret all over again

  20. 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 quickly • DISADVANTAGE: must wait for the entire speech to be translated

  21. 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.

  22. 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.

  23. 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.

  24. 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

  25. 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.

  26. 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

More Related