Lecture 1 Analysis of Algorithms

Analysis of Algorithmsspa

The theoretical study of computer program performance and resource usage.orm

Performance: timeblog

Resource: communication, RAM memory or disk memory and so on.ci

 

What's more important than performance?input

Correctness, Simplicity, Maintainability, Robustness, Features, Functionality, Modularity, Security, User-friendliness.it

Why study algorithm and performance?io

They are the fundamental thing for above.form

 

Problem: sortingperformance

Input: sequence <a1, a2, ..., an> of numbersimport

Output: permutation <a1’, a2’, ..., an’>, s.t. a1’ <= a2’ <= ... <= an’

 

Insertion Sort:

Insertion-Sort(A,n) //Sorts A[1...n]

for j <- 2 to n

    do key <- A[j]

        i <- j - 1

        while i > 0 and A[i] > key

            do A[i+1] <- A[i]

                i <- i - 1

        A[i+1] <- key

 

 

Running time:

-Depends on input (eg. Already sorted)

-Depends on input size (6 elem. VS 6*10**9)

    -parameterize in input size

-Want upper bounds

    -guarantee to the user

 

 

Kinds of analysis

Worst-case (usually): T(n) = max time on any input of size n.

Average-case (sometimes): T(n) = expected time over all inputs of size n.

(Need assumption of statistic of distribution: uniform distribution)

Best-case (bogus) cheat

 

What is insertion sort's worst-case time?

Depends on computer

-relative speed (on same machine)

-absolute speed (on different machine)

 

BIG IDEA!  Asymptotic analysis

1. Ignore machine dependent constants;

2. Look at growth of T(n) as n -> infinity.

 

相關文章
相關標籤/搜索