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

Best services for writing your paper according to Trustpilot

Premium Partner
From $18.00 per page
4,8 / 5
4,80
Writers Experience
4,80
Delivery
4,90
Support
4,70
Price
Recommended Service
From $13.90 per page
4,6 / 5
4,70
Writers Experience
4,70
Delivery
4,60
Support
4,60
Price
From $20.00 per page
4,5 / 5
4,80
Writers Experience
4,50
Delivery
4,40
Support
4,10
Price
* All Partners were chosen among 50+ writing services by our Customer Satisfaction Team

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

x

Hi!
I'm Niki!

Would you like to get a custom essay? How about receiving a customized one?

Check it out