Recursive Algorithm Analysis and Binary Search Pseudo-codes

pseudo code 1 n.w
1 / 8
Embed
Share

Explore pseudo-codes for array max, prefix averages, recursive factorial analysis, and binary search recursion with detailed explanations and running time analysis. Learn about algorithmic concepts with visual representations.

  • Algorithms
  • Pseudo-codes
  • Recursive Analysis
  • Binary Search
  • Running Time

Uploaded on | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Pseudo-code 1 Algorithm1arrayMax(A, n) Input array A of n integers Output maximum element of A currentMax A[0] fori 1 ton 1 do ifA[i] currentMaxthen currentMax A[i] returncurrentMax Running time of algorithm is O(n)

  2. Pseudo-code 2 Algorithm2prefixAverages(A, n) Input array A of n integers Output array X of n doubles Let X be an array of n doubles fori 1 ton 1 do a 0 forj 0 toi 1 do a a + A[j] X[i] a / (i+1) returnX Running time of algorithm is O(n2)

  3. Pseudo-code 3 Algorithm3 m 1 result 0 fori 1 tondo m m * 2 forj 1 tomdo result result + i*m*j returnresult Running time of algorithm is O(2n)

  4. Recursive algorithm analysis: factorial Factorial(n) if(n==1) else return 1 return Factorial(n-1)*n 1st step: come up with a recurrence equation T(n) = running time of Factorial(n) => T(n) = T(n-1) + 1 2nd step: identify a base case That is, a termination condition (n=1) T(1) = 1 step (i.e., a constant number of steps being executed)

  5. Recursive algorithm analysis: factorial (cont d) 3rd step: expand T(n) ; 1 ) 1 ( ) ( = n T n T = n T + = + ( ) 1 + ( ) 2 1 T n T n T n T n ) 1 + = + ( ) ( ( ) 2 1 ( ) 2 2 T n T n = ) 3 ) 1 + + = ) 3 + ( ) ( ( 2 ( 3 T n ...... 4th step: see the pattern = + ( ) ( ) + T n T n k k = = + = ( ) ) 1 ( n = 1 1 1 T n T n n n = ( ) ( ) T n O n

  6. Binary search recursion: pseudo-code Boolean BS(A, key, start, end) mid = (start+end)/2 if(A[mid] == key) return true else if(end <= start) else return false if (A[mid] > key) return BS(A, key, start, mid-1) else return BS(A, key, mid+1, end)

  7. Binary search recursion: running time analysis 1st step: find recurrence equation T(n): running time of BS for input A of size n T(n) = T(n/2) + 1 2nd step: look at termination condition When the search pool is reduced to one T(1) = 1

  8. Binary search recursion: running time analysis (cont d) 3rd step: expand T(n) + = T n T n T = + ( ) ( ) 2 / ; 1 ( ) 2 / + ( / 2 ) 2 / 1 n T n = ) 1 + = + 2 2 ( ) ( ( / 2 ) 1 ( / 2 ) 2 T n T n T n = ) 1 + + = + 3 3 ( ) ( ( / 2 ) 2 ( / 2 ) 3 T n T n T n ...... 4th step: pattern matching = + k ( ) ( / 2 ) + T n T n k = ( ) ) 1 ( T + log ( = ) O T n n 2 = ( ) 1 log ( ) (log ( )) T n n n 2 2

Related


More Related Content