首页 > 技术文章 > Analysis of Algorithms--preface

chuji1988 2014-10-06 11:43 原文

Analysis of Algorithms:

  • First part of the course is focused on analysis.
  • Second part of the course is focused on design.

The analysis of algorithm is the theoretical study.(算法分析是理论研究)

The theoretical study of computer-program performance and resource usage.(理论研究是关于计算机性能和资源利用的研究)

In programming, what is more important than performance?

  • correctness
  • simplicity
  • maintainability
  • cost
  • stability
  • functionablity
  • fearures
  • modularity
  • security
  • scalability
  • user-friendly

Why do we bother and why study algorithms and performance?

  • Algorithms is the feasible versus infeasible.
  • Algorithms give you a lauguage of talking about program behavior.
  • We study algorithms performance is it's tons of fun.

The problem of sorting(排序问题)

  • input:sequence<a1,a2,a3...an>
  • output:permutation<A1,A2...An>

Such that:A1<A2<...<An

Insertion-Sort:

for (int i = 1; i < a.length; i++) {
            key=a[i];
            int j=i-1;
            while (j>=0&&a[j]>key) {
                a[j+1]=a[j];
                a[j]=key;
                j--;
            }
            
            for (int k = 0; k < a.length; k++) {
                System.out.print(a[k]+" ");
            }
            System.out.println();
            
        }
}

running time:

  one thing it depends on is the input itself.

  • Depends on input self(eg:already sorted)
  • Depends on input size(eg:6 elements vs 6*109)

  --parameterize things in the input size.

  • want upper bounds.guarantee to the user

Kinds of analysis:

  • worst-case analysis(usually):T(n)=max time on any input of size n.
  • Average case analysis(sometimes):T(n)=expected time over all inputs of size n.
  • best-case analysis(bogus:假象)No good.

What is insertion sorts worst-case time?

  Depends n computer.

  • relative speed (on same machine) 
  • absolute speed (on defferent machine)

BIG Idea of algorithms:

on same machine analysis algorithms performance use asymptotic analysis(渐进分析)

asymptotic analysis:

  • ignore machine-dependent constants
  • look at the growth of the running time,look at growth of T(n) as n->∞

asymptotic notation(渐进符号Θ)

Θ-notation:

  • drop low order terms(弃去它的低阶级)
  • ignore leading constants(忽略前面的常量因子)
  • EX:3n3+90n2-5n+6046=Θ(n3)

Insertion-Sort worst-case sorted:T(n)=∑Θ(j)=Θ(n2)

Is insertion sort fast?

It's turns out for small n it is moderately fast;but it is not at all for large n.

 

推荐阅读