hw1-solutions(2) - ECE368 Homework #1 1. (20 points)...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
ECE368 Homework #1 1. (20 points) Consider the following procedure acting on an array A [ 1 .. n ] . INSERTION SORT( A [ 1 .. n ] ) Cost Times 1. for j 2 to n C 1 n 2. key A [ j ] C 2 n - 1 3. i j - 1 C 3 n - 1 4. while i > 0 and A [ i ] > key C 4 n j = 2 t j 5. A [ i + 1 ] A [ i ] C 5 n j = 2 ( t j - 1 ) 6. i i - 1 C 6 n j = 2 ( t j - 1 ) 7. A [ i + 1 ] key C 7 n - 1 (a) Let t j denote the number of times the while loop test in line 4 is executed for that value of j . Fill in for each line of instruction, the number of times the instruction is executed. (b) Derive the expression for the running time of INSERTION SORT in terms of n , C i , and t j . Let T ( n ) = running time of INSERTION SORT . T ( n ) = C 1 n + C 2 ( n - 1 )+ C 3 ( n - 1 )+ C 4 n j = 2 t j + C 5 n j = 2 ( t j - 1 )+ C 6 n j = 2 ( t j - 1 )+ C 7 ( n - 1 ) (c) What is t j for the best-case scenario, i.e., when the running time of the algorithm is the smallest. Use that to derive the expression for the best-case running time of INSERTION SORT in terms of n and C i . What is the best-case time complexity of INSERTION SORT using the big-O notation? The array is already sorted. All t j are 1. Therefore, T ( n ) = C 1 n + C 2 ( n - 1 )+ C 3 ( n - 1 )+ C 4 ( n - 1 )+ C 7 ( n - 1 ) = ( C 1 + C 2 + C 3 + C 4 + C 7 ) n - ( C 2 + C 3 + C 4 + C 7 ) . T ( n ) = O ( n ) . (In fact, T ( n ) = Θ ( n ) .) (d) What is t j for the worst-case scenario, i.e., when the running time of the algorithm is the largest. Use that to derive the expression for the worst-case running time of
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/28/2012 for the course ECE 368 taught by Professor Staff during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 4

hw1-solutions(2) - ECE368 Homework #1 1. (20 points)...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online