Analysis of algorithm

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

Info iconThis preview shows pages 1–10. 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 DocumentRight 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 DocumentRight Arrow Icon
Introduction CS 344 4 instruction set: assignment arithmetic ( + - * / bcde ) 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 DocumentRight 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 X j =2 t j Y = ( t 2 - 1) + ( t 3 - 1) + ... + ( t n - 1) = n X 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 DocumentRight 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 = ...
Background image of page 9

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

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

This note was uploaded on 08/12/2009 for the course CSC 344 taught by Professor Muzumdar during the Fall '08 term at New Mexico Junior College.

Page1 / 41

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

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

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