Time complexity is usually calculated in terms of the number of times certain operations are carried out by an algorithm, e. G. , the number of additions or subtractions, the number of multiplications, the number of comparisons made (if statements). Typically, we count the number of times a certain operation, e. G, a comparison, and give a Big-O estimate. There are three possible cases of interest: Best-case: by sheer, dumb luck, what is the easiest problem we might have to solve? Worst-case: by sheer, bad luck, what is the worst problem we might have to solve?

Average-case: what is the mean complexity over all possible sets of inputs. CASES/209 ? Lecture 4: Best, Worst, and Average Case Complexity 3/1 1 Of these, “best-case” is the easiest and “average-case” is the hardest. “Worst-case” is the most important (why? ) and turns out to be asymptotically similar to average-case. Japan Bangladesh Economic Relationship By Kaisers k=l k-1 -1. Exercise Prove that IQ-I = k (k – 1)+1 Lecture 4: Best, Worst, and Average Case Complexity CASES/209 ? Example: Binary Search Recall the Binary Search algorithm?

The Binary Search Algorithm Find the index of the element x in the ordered list {al < a2 < a3 < an }. 1 Split the list into two sublists al ,a2,… ,aa n/2 +1 , and 2 lfx

We’ll assume that the list is of length n = k , and count the number of imprisons required. 6/11 Average case This will take is some more thought.. We’ll assume that the list is of length n = k -1. We will need to use that k 7/11 Exercise: Linear Search Describe the Linear Search Algorithm, e. G. , by giving a pseudopodia description of it, Show how it would try to locate the letter ‘R’ in the string {I’D’, ‘E’, ‘S’, ‘C’, ‘R’, ‘l’, ‘B’, ‘E’}. Provide a best, worst, and average-case complexity analysis for the algorithm, in terms of the number of comparisons required when applied to a list of length n. 8/11 Bubble sort o through the list {al , … , an-I , an }. If an adjacent pair of element are out of order, swap them. At the end of the 1st pass, the largest element should be at the end. Now repeat the process for the list {al , ay , … , an-I }. And then for the list . , an-2 }. Continue until you have sorted the list {al , ay }. Question: What are the best- and worst-case time complexities – in terms of the number of comparisons – for this algorithm? Solution: 9/11 Example: Bubble sort Recall Insertion Sort from Lecture 2.

Show that the total number of comparisons squired by it for a list of length n is O(no ) 10/11 Exercise (Based on Corner at al, Exert 2. 2-2) The selection sort algorithm works as follows: Find the smallest element in the list , an }, and swap it with the element al . Find the smallest element in the list, {ay , ay , … , an } and swap it with the element ay . Stop when you have sorted item an-I . 1 23 Write a pseudopodia description of this algorithm. What loop invariant does this algorithm maintain? Give best- and worst-case estimates for the number of comparisons required. 11/11