cse 373 optional section runtime analysis
Download
Skip this Video
Download Presentation
CSE 373 Optional Section Runtime Analysis

Loading in 2 Seconds...

play fullscreen
1 / 18

CSE 373 Optional Section Runtime Analysis - PowerPoint PPT Presentation


  • 116 Views
  • Uploaded on

CSE 373 Optional Section Runtime Analysis. 2013-10-08 A. Conrad Nied. Interview Question / Runtime Analysis. How do we find the integer square root of a number?. Integer Square Root Solution 1. Increment through the list of numbers from 1 to x . Call this value i . What cases do we have?.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' CSE 373 Optional Section Runtime Analysis' - bishop


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
interview question runtime analysis
Interview Question / Runtime Analysis
  • How do we find the integer square root of a number?
integer square root solution 1
Integer Square Root Solution 1
  • Increment through the list of numbers from 1 to x. Call this value i .
  • What cases do we have?
integer square root solution 11
Integer Square Root Solution 1
  • Increment through the list of numbers from 1 to x. Call this value i .
  • What cases do we have?
    • If i× i < x
    • If i× i == x
    • If i× i > x
integer square root solution 12
Integer Square Root Solution 1
  • Increment through the list of numbers from 1 to x. Call this value i .
  • What cases do we have?
    • If i× i < x increment i
    • If i× i == x return i
    • If i× i > x return i - 1
integer square root solution 13
Integer Square Root Solution 1
    • If i× i < x increment i
    • If i× i == x return i
    • If i× i > x return i – 1
  • Implementation:

inti = 1

while(i * i < x)

i++

return i - 1

integer square root solution 14
Integer Square Root Solution 1
  • Implementation:

inti = 1

while(i * i < x)

i++

return i – 1

  • Runtime
    • O( ? )
integer square root solution 15
Integer Square Root Solution 1
  • Implementation:

inti = 1

while(i * i < x)

i++

return i – 1

  • Runtime
    • O(n0.5)
integer square root solution 16
Integer Square Root Solution 1
  • Implementation:

inti = 1

while(i * i < x)

i++

return i – 1

  • Runtime
    • O(n0.5)
  • Implement it in Java together & add timing
integer square root solution 2
Integer Square Root Solution 2
  • Binary search
  • Imagine all of the numbers are in an array (indexed by itself) and search for the integer root
  • What number should we start with first?
    • 0, 1, x / 2, x
integer square root solution 21
Integer Square Root Solution 2
  • Starting with i, jump = x; search(x, i, jump)
  • Proposition A: i× i <= x
  • Proposition B: (i + 1) × (i + 1) > x
  • What do we do in each case?
    • A & B
    • A only
    • B only
    • Neither
integer square root solution 22
Integer Square Root Solution 2
  • Starting with i, jump = x; search(x, i, jump)
  • Proposition A: i× i <= x
  • Proposition B: (i + 1) × (i + 1) > x
  • What do we do in each case?
    • A & B return i
    • A only
    • B only
    • Neither
integer square root solution 23
Integer Square Root Solution 2
  • Starting with i, jump = x; search(x, i, jump)
  • Proposition A: i× i <= x
  • Proposition B: (i + 1) × (i + 1) > x
  • What do we do in each case?
    • A & B return i
    • A only jump /= 2; search(x, i + jump, jump)
    • B only jump /= 2; search(x, i - jump, jump)
    • Neither
integer square root solution 24
Integer Square Root Solution 2
  • Starting with i, jump = x; search(x, i, jump)
  • Proposition A: i× i <= x
  • Proposition B: (i + 1) × (i + 1) > x
  • What do we do in each case?
    • A & B return i
    • A only jump /= 2; search(x, i + jump, jump)
    • B only jump /= 2; search(x, i - jump, jump)
    • Neither throw WhatJustHappenedException()
integer square root solution 25
Integer Square Root Solution 2
  • Implement

intSqrt(x):

return search(x, x / 2, x / 2)

Search(x, i, jump):

if(i * i <= x)

if((i+1)(i+1) > x)

return i

else

jump /= 2

return search(x, i + jump, jump)

else

jump /= 2

return search(x, i – jump, jump)

integer square root solution 26
Integer Square Root Solution 2
  • Runtime
    • T(1) = 1
    • T(N) = 1 + T(N / 2)
    • O( ? )
integer square root solution 27
Integer Square Root Solution 2
  • Runtime
    • T(1) = 1
    • T(N) = 1 + T(N / 2)
    • O(log(n))
stuck on proof by induction
Stuckon Proof by Induction?
  • Khan Academy!
    • https://www.khanacademy.org/math/trigonometry/seq_induction/proof_by_induction/v/proof-by-induction
ad