java


Big O notation and not understading from class lecture


I have this problem where in class my professor said that the below statement is O(log(n)) where I thought it was O(n). Could someone please clarify how it is O(log(n))?
Printing a number of magnitude n in binary. Assume that printing each bit requires constant time.
You should work out some examples. Write some numbers in binary. For example, how many bits are there in 63, 255, and 511? Notice that the number of bits does not grow nearly as quickly as the number itself.
It's O(log(n)) because you have to divide by 2 every time you are going to print a 0 or 1.
For example, to print 256 in binary, you'd have to divide by 2 starting from 256 and print the result of % 2 every time.
256 % 2 -> 0
64% 2 -> 0
32 % 2 -> 0
16 % 2 -> 0
8 % 2 -> 0
4 % 2 -> 0
2 % 2 -> 0
1 % 2 -> 1
So, for a number of magnitude 256 you would have to iterate 8 times which is equals to log 256.
O(log(n)) is all about cutting data by half.
When each step of an algorithm rules out a fraction of the remaining input -- e.g. you always cut the space in half, or by a third, or even to 99/100 of the previous step -- that algorithm runs in O(log(n)) time.
hope this helps

Related Links

How to break inside if condition in java
Dependency in RCP plugin project java for visibility of view
turning off pluralization when using maven-jaxb2-plugin?
mysql transposing columns into rows dynmaically in jdbc project
JLabel + JTextArea keeps jumping around when stuff happens
BankAccount java program not showing output
Problems to align a Javafx button
picassa new API in android
Generate Java bindings for TinyB
Java Replace sub-string (pattern) by another sub-string (value) issue
doClick() method of JButton
Java loops: Write a program that returns number of occurrences of a string in another string
How to programatically access the Eclipse variable pool?
json to java hashmap using complex key
Should I always write extra configuration in Deployment Descriptor everytime I create a servlet in eclipse?
Sending audio over socket from server to client

Categories

HOME
gerrit
dot
webstorm
mean-stack
kalman-filter
razor
spring-jdbc
malloc
flyway4
django-imagekit
portia
cross-validation
gitpitch
windows-10-universal
handsontable
google-cloud-spanner
lombok
django-admin
transformation
visjs
mps
css-animations
chromium-embedded
mmap
social-media
assistant
msys2
nouislider
google-rich-snippets
key-value-observing
column-family
code-contracts
kendo-ui-grid
google-api-nodejs-client
host
ios5
git-merge
multilingual
neuroscience
libvpx
vapor
az-application-insights
reportbuilder
document.write
tasker
pdf-reactor
ibpy
node-sass
thin
react-scripts
jquery-validate
boost-multi-index
mu
drupal-6
darcs
libusb-win32
mako
cudafy.net
gnome-shell-extensions
mikroc
websitepanel
sdf
r-tree
wireshark-dissector
ionic
method-parameters
generic-programming
sonarqube5.1.2
proj4js
streambase
castle
app42
viadeo
commoncrypto
access-rights
spring-io
tws
excel-2003
chronoforms
gil
funscript
gwt-ext
zpt
gcj
genshi
cinema-4d
yui-datatable
actionview
oncheckedchanged
remember-me
blitz++
createwindow
zend-translate
meego
firefox4
premature-optimization
aquaticprime
zend-decorators
data-acquisition

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App