# Computer Science Algorithm Analysis Final Study Guide

**Topics:**Sorting algorithm, Big O notation, Heapsort

**Pages:**9 (1429 words)

**Published:**May 24, 2012

Big-O notation:

The formal method of expressing the upper bound of an algorithm’s running time (worst case) Big-Omega notation:

The formal method of expressing the lower bound of an algorithm’s running time (best case) Theta Notation:

The method of expressing that a given function is bounded from both top to bottom by the same function This exists if and only if f(n) is O(g(n)) and f(n) is Ω(g(n)) Little-O notation:

f(n) is little-O(g(n))--denoted o(g(n))--if and only if f(n) = O(g(n)), but f(n) ¹ Q(g(n)) Little-Omega notation:

f(n) is little-omega(g(n))—denoted w(g(n))—if and only if f(n)=Ω(g(n)), but f(n) ¹ Q(g(n))

Big-Oh:

http://www.youtube.com/watch?v=foUNGAsN-1M&feature=related Formal definition (symbols): f(n) = O(g(n)) if $ c, n0 | f(n) <= c * f(n) when n >= n0. Formal definition (words): f(n) = O(g(n)) if a real positive numbers c and a positive integer N exist such that f(n) <= c * g(n) for all n >= N. That is, c * g(n) is an upper bound on f(n) when n is sufficiently large. Reduction steps:

For the example code: Cost:

1) int sum(int n) {

2) int sum; 0

3) sum = 0; 1

4) for(int i = 1; i <= n.length; n++) 2n + 2

5) sum += i * i * i; 4n

6) return sum; + 1

Add up the total cost of the function = 6n + 4

c. Reduce all constants to 1 = 1n + 1

- Highest power will be Big-Oh

d. Apply O(f(n)) definition

- Ex) Let c = 6+1 = 7

Is 6n + 4 <= 7 (n) for large n?

Yes.

Program Analysis (Rules of thumb):

1. for loops: number of statements * number of iterations 2. nested loops: count inside out

Ex. O(n) for( int i = 0; i <count; i++) {

O(n) for( int j = 1; j<count; j++) {

O(n2) k++;

3. consecutive statements: just add up

4. conditionals: use larger of the branches (overestimate rather than underestimate) 5. recursion: use recurrence relations

Order of big-O functions (from least to most efficient): O(1) – constant

O(lg n) – logarithmic

O(lg 2 n) – iterated logarithmic

O(n) – linear

O(n lg n) – log linear

O(n2) – quadratic

O(n3) – cubic

O(2n) - exponential

- How to reduce function, e.g. 4n^3 + 6n^2 + 12n +36 -> O(n^3)

Order, e.g. f1 < f2 <= f3…

- Know formal definitions, be able to say what they mean in your own words

Recurrence Relations (30%):

Two Long Questions (maybe recurrence relations?):

Practice what is in the text:

Sorting Algorithms: (add how they compare)

1) Bubble Sort:

How it works:

Compare the first two elements, and if the first is greater than the second, they are swapped. Do this for each adjacent pair of elements to the end of the data set. Repeat from the beginning until no swaps have occurred. Cases:

Best: O(n) (list is already sorted)

Average: Θ(n2)

Worst: Ω(n2)

2) Selection Sort:

How it works:

Find the minimum value in the set.

Swap it with the first value in the set.

Repeat this process for the remainder of the list (starting at the 2nd position and advancing each time). Cases:

Best: O(n2)

Average: Θ(n2)...

Please join StudyMode to read the full document