Insertion Sort and Merge Sort

来源:互联网 发布:jquery easing.js下载 编辑:程序博客网 时间:2024/05/17 08:43

Analysis of Algorithms

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

What is more important than performance?

user-friendliness
stability
security
……

Why study algorithms and performance?

1.measures the line between the feasible and infeasible.
real-time requirement
2.algorithm is a language for talking about program behavior.

Java is slower than C

universal
3.fun

Problem:Sorting

Input:sequence a1,a2...an of numbers
Ouput:permutation a1,a2...an

a1<=a2<=an
increasing

Insertion sort

Intertion sort
intertion sort

running time

  • depends on input(eg.already sortd)
  • depends on input size(6 elements vs 6 billion)
    • parameterize in input size
  • upper bounds of the time
    • guarantee for the users

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 a assumption of statistical distribution
Best-case (bogus)
Cheat

What is insertion sorts worst-case time?

Depends on the computer.
- relative speed (on same machine)
- absolute speed(on different machines)

Big Idea!

Asymptotic Analysis

  1. Ignore machine-dependent constants
  2. Look a t the growth of T(n) as n→∞

Asymptotic notation

θ notation:Drop low-order terms Ignore leading constants.
It’s a weak notation.
Ex. 3n3+90n25n+6046 =Θ(n3)
As n→∞ Θ(n2) alg always beats a Θ(n3) alg.

asymptotic notation

Insertion sort analysis

worst-case:input reverse sorted
T(n)=n2Θ(j)=Θ(n2)

arithmetic series
Is insertion sort fast?
- Moderately fast for small n.
- Not at all for large n.

Merge sort

Merge sort
1. T(n)=Θ(1)is a constant time.
2. 2T(n/2)
3. T(n)=Θ(n) on n total elements.

Recurrence
T(n)=

{Θ(1),2T(n/2)+Θ(n)if n=1(usu.omit)if n is >1

Recursion tree:T(n)=2T(n/2)+cn, constant c >0
recursion tree

Code for Insertion Sort:
Insertion Sort code in github
Code for Merge Sort:
Merge Sort code in github

0 0