Docsity

Lecture 3: Analysis of simple algorithms, Schemes and Mind Maps of Algorithms and Programming

Partial preview of the text

Download Lecture 3: Analysis of simple algorithms and more Schemes and Mind Maps Algorithms and Programming in PDF only on Docsity!

Lecture 3: Analysis of simple algorithms

  • Quicksort and its complexity
  • Bisection and its complexity

Quicksort algorithm

  • a vector of numbers c of length n, start location for sort p, end location for sort q
  • a recursive algorithm using “divide-and-conquer”
  • quicksort peudocode quicksort(c, p, q) r := findpivot(c, p, q) quicksort(c, p, r-1) quicksort(c, r+1, q) Demo: http://me.dt.in.th/page/Quicksort/

Complexity

  • analysis of algorithm: to determine the amount of resources (time and storage) needed to execute an algorithm
  • time complexity: count the # of operations (flops)
  • asymptotic analysis and big O notation e.g. f (n) = 9 log n + 5(log n) 3 + 3n 2 + 2n 3 = O(n 3 ), as n → ∞
  • average complexity of quicksort algorithm: O(n log n) Proof

Bisection

  • for a non-decreasing function g(x), find x such that g(x) = 0
  • bisection:
    • start with g(a) < 0 < g(b)
    • take c = 1 2 (a + b)
    • if g(c) < 0 , consider right half interval [c, b]
    • if g(c) > 0 , consider left half interval [a, c]
    • repeat the above corresponding subinterval