Introduction to Algorithms I

- Insertion Sort and Merge Sort

made by Joanne Tseng 2014/08/24

What is analysis of algorithm?

The theoretical study of computer-program

performance and resource usage

Performance : How to make things fast.

Sorting Problem

 

  • Input : Sequence <a1, a2, a3, ... ,an> of numbers
     
  • Output : permutation <a1', a2', a3', ... , an'>
    such that  a1' <= a2' <= ... <= an'

     
  • ps. We use pseudo code to write algorithms.

     

Insertion Sort

Insertion Sort

//pseudo code of Insertion Sort

for j=2 to n
   { key <- A[j]
     i <- j-1
     while (i > 0 and A[i] > key)
       { 
         A[i+1] <- A[i]
          i <- i-1
       }
     A[i+1] <- key
   }

Example of Insertion Sort

original list: 8,2,4,9,3,6

j=2 : 8, 2, 4, 9, 3, 6 --> 2, 8, 4, 9, 3, 6

j=3 : 2, 8, 4, 9, 3, 6 --> 2, 4, 8, 9, 3, 6

j=4 : 2, 4, 8, 9, 3, 6 --> 2, 4, 8, 9, 3, 6

j=5 : 2, 4, 8, 9, 3, 6 --> 2, 3, 4, 8, 9, 6

j=6 : 2, 3, 4, 8, 9, 6 --> 2, 3, 4, 6, 8, 9

Hands-On Practice

Use Python to write Insertion Sort 
by referencing pseudo code.

How to determine
the efficiency of algorithms?

- Running Time
 

  • input itself - Best case and Worst case
     
  • input size

    --> Generally , we want upper bounds of running time (i.e. worst case ), and we use T(n) to describe maximum time on any input of size n.

Big Idea of Asymptotic Analysis

  • Ignore machine dependent constants
  • Look at growth of the running time T(n) as n ->infinity

Asymptotic Notation (theta)

  • theta-notation :
    Drop low-order terms, ignore leading constants.

What's Insertion sort's worst case time? 

  • Depends on computer
    - relative speed ( on same computer )
    - absolute speed (on different computer )

Insertion sort Analysis

  • worst case : reversed sort
    T(n) = theta(n^2) -->order of growth [計算見白板兒]

Merge Sort

Before understanding Merge Sort...

We should know HOW TO MERGE

Example of Merge 

  • 1 vs. 2 : 小的拿出來放第一個 --> choose 1
  • 2 vs. 9 : choose 2
  • 7 vs. 9 : choose 7
  • 13 vs. 9 : choose 9
  • 13 vs. 11 : choose 11
  • ... and so on...

- merge [1,7,13,20] and [1,9,11,12]

Merge 

// pseudo code of Merge

Merge(A, p, q, r)
n1 = q - p + 1
n2 = r - q
Let L[1,...,n1+2] and R[1,...,n2+1] be new arrays

for i=1 to n1
    L[i] = A[p + i - 1]
for j=1 to n2
    R[j] = A[q + j]

L[n1+1] = inf
R[n2+1] = inf
i = 1
j = 1
for k=p to r
    if (L[i] <= R[j])
        A[k] = L[i]
        i = i+1
    else
        A[k] = R[j]
        j = j+1
       
    

Merge Sort 

// pseudo code of Merge Sort

MergeSort(A,p,r)
if (p<r)
{
    q = (p+r)/2
    MergeSort(A , p , q)
    MergeSort(A , q+1 , r)
    Merge(A , p , q , r)
}

Merge Sort Analysis(I)

 

  • Recurrence:
    If n=1 , T(n) = theta(1) 
    If n>1 , T(n) = 2 *T(n/2) + theta(n)
  • Recursion Tree:
    T(n) = 2 *T(n/2) + theta(n)
    [很難畫!見白板兒~~]

Merge Sort Analysis(II)

  • Add up total amount = (cn)*lg(n)+theta(n)
                                            = theta(nlg(n))
     
  • theta(nlg(n)) is asymptotically faster that theta(n^2)

    (i.e. Merge Sort is faster that Insertion Sort when input size is large enough.) 

Website suggestion

http://www.comp.nus.edu.sg/~stevenha/visualization/sorting.html

This is a basic introduction
​of two sorts  :)

下~~~課~~~

Introduction to Algorithms I ---- Insertion Sort and Merge Sort

By tseng0211

Introduction to Algorithms I ---- Insertion Sort and Merge Sort

  • 652