instructions 10 7 lg10 7 instructions 20000 seconds more than 55 hours 1160

# Instructions 10 7 lg10 7 instructions 20000 seconds

• 17

This preview shows page 6 - 17 out of 17 pages.

instructions 10 7 lg10 7 instructions 20,000 seconds (more than 5.5 hours) 1160 seconds (less than 20 minutes) 6
Finding the order of an algorithm • Insertion Sort 7
Finding the order of an algorithm • Insertion Sort 8
Complexity of insertion sort • What about the worst case? • Best case 9
Complexity of insertion sort • Worst case 10
Best vs. worst-case running time • Which one should we consider? - The worst-case running time of an algorithm gives an upper bound on the running time for any input - The worst-case occurs fairly often 11
Order of growth T 1 ( n ) = 20 n + 5 T 2 ( n ) = 4 n + 50 T 3 ( n ) = n 2 + n T 4 ( n ) = 2 n 2 + 50 T 1 ( n ) -> 20 n T 2 ( n ) -> 4 n T 3 ( n ) -> n 2 T 4 ( n ) -> 2 n 2 T 1 ( n ) -> n T 2 ( n ) -> n T 3 ( n ) -> n 2 T 4 ( n ) -> n 2 T 1 ( n ) is in T 3 ( n ) is in Θ ( n ) Θ ( n 2 ) 12
Order of growth 13
for all - We are only interested in what happens when n becomes very large - grows at a rate the same as n n 0 c 1 g ( n ) f ( n ) c 2 g ( n ) f ( n ) g ( n ) 14 - notation Θ
Analyzing computational complexity • For a sequence of statements, consider the complexity of the most complex thing in the sequence • For a loop, consider the complexity of the body of the loop multiplied by the number of times the loop executes • For a conditional statement, consider the complexity of whichever of the two alternatives has the higher complexity 15
How exactly? • Identify the fundamental step that is executed the most often • Write down a function that relates the number of times that step executes to the size of the input • Simplify that function as much as possible by discarding smaller terms and constant coefficients • What remains is the complexity of the algorithm/code 16
Exercises • Rewrite the insertion-sort algorithm to sort into non-increasing instead of non-decreasing order • What is the step count of the insertion-sort algorithm on the array A = {30, 41, 55, 23, 41, 52} (refer to slide 8) 19

#### You've reached the end of your free preview.

Want to read all 17 pages?

• Fall '13
• DorotheaBlostein
• Analysis of algorithms, Computational complexity theory, best solution, Ting Hu

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern