CS062, Lecture 7

  1. Complexity
  2. Searching
    1. Linear search
    2. Binary search

Complexity

Rather than keeping an exact count of operations, use order of magnitude count of complexity.

Ignore differences which are constant - e.g., treat n and n/2 as same order of magnitude.

Definition: We say that g(n) is O(f(n)) if there exist two constants C and k such that |g(n)| <= C |f(n)| for all n > k.

Most common are

O(1) - for any constant

O(log n), O(n), O(n log n), O(n2), ..., O(2n)

Usually use these to measure time and space complexity of algorithms.

Make table of values to show difference.

Suppose have operations with time complexity O(log n), O(n), O(n log n), O(n2), and O(2n).

And suppose all work on problem of size n in time t. How much time to do problem 10, 100, or 1000 times larger?

size 10 n 100 n 1000 n
O(log n) 3+t 7+t 10+t
O(n) 10t 100t 1,000t
O(n log n) >10t >100t >1,000t
O(n2) 100t 10,000t 1,000,000t
O(2n) ~t10 ~t100 ~t1000
TIME TO SOLVE PROBLEM

*Note that the last line depends on the fact that the constant is 1, otherwise the times are somewhat different.

Suppose get new machine that allows certain speed-up. How much larger problems can be solved? If original machine allowed solution of problem of size k in time t, then

speed-up 1x 10x 100x 1000x
O(log n) k k10 k100 k1000
O(n) k 10k 100k 1,000k
O(n log n) k <10k <100k <1,000k
O(n2) k 3k+ 10k 30k+
O(2n) k k+3 k+7 k+10
SIZE OF PROBLEM

We will use big Oh notation to help us measure complexity of algorithms.

Searching

Searching and sorting are important operations and also important example for use of complexity analysis.

Code for all searches is on-line in Sort program example SortingDemo.java.

Linear search is O(n), while binary search i O(log n) comparisons.

Concrete comparison of worst cases: # of comparisons:

Search\# elts 10 100 1000 1,000,000
linear 10 100 1000 1,000,000
binary 8 14 20 40