i206: Lecture 8: Sorting and Analysis of Algorithms. Marti Hearst Spring 2012. Definition of Big-Oh A running time is O (g( n) ) if there exist constants n 0 > 0 and c > 0 such that for all problem sizes n > n 0 , the running time for a problem of size n is at most c( g (n)) .
A running time is O(g(n)) if there exist constants n0 > 0 and c > 0 such that for all problem sizes n > n0, the running time for a problem of size n is at most c(g(n)).
In other words, c(g(n)) is an upper bound on the running time for sufficiently large n.
Example: the function f(n)=8n–2 is O(n)
The running time of algorithm arrayMax is O(n)
f(n) is orderg(n)
f(n) is big-Oh ofg(n)
f(n) is Oh ofg(n)
f(n) O(g(n)) (set notation)
Multiply both sides by
Take the log of both sides
Since m is the worst case time, the algorithm is O(logn)
Putting collections of things in order
The dark gray represents a comparison and the white represents a swap.
Brookshear Figure 5.11
Brookshear Figure 5.19
An average and worst-case performance of O(n log n).
Based on how the algorithm works, the recurrence
T(n) = 2T(n/2) + n = n log n describes the average running time.
(Apply the algorithm to two lists of half the size of the original list, and add the n steps taken to merge the resulting two lists.)
In the worst case, the number of comparisons merge sort makes is equal to or slightly smaller than
(n log n - 2log n + 1),
which is between (n log n - n + 1) and (n log n + n + O(lg n)).
Say you have a long string, such as "supercalifragilisticexpialidocious".
Say you want to print out all the substrings of this string, starting from the left, and always retaining the substring you have printed out so far, e.g. "s", "su", "sup", "supe", and so on.
Assuming this string is of length n, how many substrings will you print out in terms of big-O notation?
If you printed out all possible substrings, starting at any location, and of any length, how many are there?