Sie sind auf Seite 1von 26

Lecture 1

Goal
What is an algorithm? Why do we analyze them? Introduction to analysis of algorithms Insertion sort Merge sort notation

Lecture 1-1

What is an algorithm?

A well-defined computational procedure that takes some value as input and produces some value as output. (Also, a sequence of computational steps that transforms the input into the output.)

Lecture 1-2

Analysis of Algorithms
ProblemSorting

How do we describe an algorithm?

Lecture 1-3

Pseudocode notations
liberal use of English use of indentation for block structure employ any clear and concise expressive methods typically are not concerned with software engineering issues error handling data abstraction modularity. ...with the exception of adding good comments.

Lecture 1-4

Insertion Sort

Lecture 1-5

Insertion SortMain Idea

Lecture 1-6

Operation of Insertion Sort Algorithm

Lecture 1-7

Running time

For a given input, we can characterize an algorithms running time. What do we mean by time? CPU? Wall-clock? doesnt it depend on implementation?

Lecture 1-8

Running time
Typically, count primitive operations Examples Indexing an array Moving an item Loop accounting Makes characterization independent of relative speed (on same machine) absolute speed (on different machines) So, for sorting that leaves input size (e.g. 6 elements vs.6000) input itself (e.g. partially sorted already) Generally want upper bound Experiments suggest performance, but analysis gives user a guarantee about performance

Lecture 1-9

Running time
One natural simplification Express running time as a function of input size, rather than of a particular input. But now there is more than one answer (one for each distinct input size). What to do?

Lecture 1-10

Kinds of analysis
Express as function T(n) of input size n (usually)Worst case T(n)=max time on any input of size n (sometimes)Average case T(n)=average time over all inputs of size n (assumes statistical distribution of inputs) (almost never)Best caseBad Give slow algorithm that is fast on some input. Useful only for showing lousy lower bound. For insertion sort, what is worst-case time?

Lecture 1-11

Asymptotic analysis
Ignore machine-dependent constants Look at growth of T(n) as n notation Drop low-order terms Ignore leading constants

Lecture 1-12

Lecture 1-13

Insertion Sort Pseudocode

Lecture 1-14

Insertion Sort Analysis


Write time equations by looking at pseudocode

Lecture 1-15

Lecture 1-16

Is this a fast sorting algorithm?


Yes, for small n. No, for large n.

Lecture 1-17

Merge Sort
To sort n numbers

Lecture 1-18

Basic step
Merge two already sorted lists into one sorted list

Lecture 1-19

Recursive algorithm.

Lecture 1-20

Operation of Merge Sort

Lecture 1-21

Recurrence
describes a function recursively in terms of itself. describes performance of recursive algorithms.

Lecture 1-22

How do we find a good upper bound on T(n) in closed form?

Lecture 1-23

Recursion tree

Lecture 1-24

Recursion tree for Merge-Sort

Lecture 1-25

Conclusions

Lecture 1-26

Das könnte Ihnen auch gefallen