VII. Algorithm Complexity (Chap. 7) : of Magnitude F (N), Written T (N) Is O (F (N) )
VII. Algorithm Complexity (Chap. 7) : of Magnitude F (N), Written T (N) Is O (F (N) )
7)
3. Definition of "big-O notation: The computing time of an algorithm is said to have order
of magnitude f(n), written T(n) is O(f(n))
if there is some constant C such that
T(n) ≤ C. f(n) for all sufficiently large values of n.
4. The arrangement of the input items may affect the computing time. For example, it may
take more time to sort a list of element that are nearly in order than for one that are
completely out of order. We might measure it in the best case or in the worst case or try
for the average. Usually best-case isn't very informative, average-case is too difficult to
calculate; so we usually measure worst-case performance.
5. Example:
a. LINEAR SEARCH ALGORITHM on p. 354
/* Algorithm to perform a linear search of the list a[0], . . . , a[n – 1].
Receive: An integer n and a list of n elements stored in array elements
a[0], . . . , a[n – 1], and item of the same type as the array elements.
Return: found = true and loc = position of item if the search is successful;
otherwise, found is false.
-------------------------------------------------------------------------------------------*/
1. Set found = false.
2. Set loc = 0.
3. While loc < n and not found do the following:
4. If item = a[loc] then // item found
5. Set found = true.
6. Else // keep searching *)
Increment loc by 1.
T(n) = O(2n)