{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Analysis of algorithm - Introduction CS 344 1 Analysis of...

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

View Full Document Right Arrow Icon
Introduction CS 344 1 Analysis of Algorithms I NSERTION S ORT ( A ) for j 2 to n do key A [ j ] /* Insert A [ j ] into the already sorted A [1 ..j - 1] */ /* comparing sequentially from the end */ i j - 1 while i > 0 and A [ i ] > key do A [ i + 1] A [ i ] i i - 1 A [ i + 1] key Subhasish Mazumdar CS 344 Home Page Fall 2008
Background image of page 1

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

View Full Document Right Arrow Icon
Introduction CS 344 2 NOTATION indentation / whitespace while, for , comments var: local array length[A] complex/array objects: ptrs parameter passing: by value Subhasish Mazumdar CS 344 Home Page Fall 2008
Background image of page 2
Introduction CS 344 3 The Model 1-processor random-access machine Data types: integer and floating point word size fixed but big enough to hold a given input. Subhasish Mazumdar CS 344 Home Page Fall 2008
Background image of page 3

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

View Full Document Right Arrow Icon
Introduction CS 344 4 instruction set: assignment arithmetic ( + - * / ) compromise: 2 k takes constant time for small k . control (procedure call, return, if-then, if-then- else, while, for, repeat loops) Each line of pseudo-code takes a constant amount of time. Subhasish Mazumdar CS 344 Home Page Fall 2008
Background image of page 4
Introduction CS 344 5 Example : Input sequence < 89 , 12 , 3 , 21 , 15 > : 89 12 89 3 12 89 3 12 21 89 3 12 15 21 89 In-Place sort. 1 extra location. (A sorting algo- rithm is in-place when it needs only O (1) extra storage) Number of shifts (inner loop) = # of inversions (pairs (i,j) such that A [ i ] > A [ j ] ). Subhasish Mazumdar CS 344 Home Page Fall 2008
Background image of page 5

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

View Full Document Right Arrow Icon
Introduction CS 344 6 Cost #Times for j 2 to n c 1 n do key A [ j ] c 2 n - 1 /* Insert A [ j ] into the already... */ 0 n - 1 /* examining it sequentially..., */ 0 n - 1 i j - 1 c 4 n - 1 while i > 0 and A [ i ] > key c 5 X do A [ i + 1] A [ i ] c 6 Y i i - 1 c 7 Y A [ i + 1] key c 8 n - 1 Subhasish Mazumdar CS 344 Home Page Fall 2008
Background image of page 6
Introduction CS 344 7 where X = t 2 + t 3 + ... + t n = n j =2 t j Y = ( t 2 - 1) + ( t 3 - 1) + ... + ( t n - 1) = n j =2 ( t j - 1) Best Case occurs when t j = 1 for all j . T ( n ) = c 1 n + c 2 ( n - 1) + c 4 ( n - 1)+ c 5 ( n - 1) + c 8 ( n - 1) = ( c 1 + c 2 + c 4 + c 5 + c 8 ) n - ( c 2 + c 4 + c 5 + c 8 ) = Θ( n ) Subhasish Mazumdar CS 344 Home Page Fall 2008
Background image of page 7

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

View Full Document Right Arrow Icon
Introduction CS 344 8 Worst Case occurs when t j = j for all j . T ( n ) = c 1 n + c 2 ( n - 1) + c 4 ( n - 1) + c 5 ( S ( n ) - 1) + c 6 ( S ( n ) - n ) + c 7 ( S ( n ) - n ) + c 8 ( n - 1) = Fn 2 + Gn - H where F, G, H are ... = Θ( n 2 ) Average case : some probabilistic assumptions are necessary. Subhasish Mazumdar CS 344 Home Page Fall 2008
Background image of page 8
Introduction CS 344 9 Assume that half the time the shift is necessary. In that case, X = t 2 + t 3 + ... + t n = 2 + 0 + 4 + 0 + 6 + ... + n = 2 . (1 + 2 + 3 + ... + n 2 ) = ( n 2 )( n 2 + 1) Y = ... Clearly T ( n ) = Θ( n 2 ) Subhasish Mazumdar CS 344 Home Page Fall 2008
Background image of page 9

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

View Full Document Right Arrow Icon
Introduction CS 344 10 Alternatively, assume that half the array is shifted every time, i.e., that t j = j 2 Here too, T ( n ) = Θ( n 2 ) Thus, average case worst case.
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}