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

Strange behaviour with ConnectionFactory using CDI with Tomcat 7
Android UnityPlayerActivity Action Bar
Native Error: [25037] - There is not enough disk space left for the Database (Windows Compact)
Retrieve data from Json through php in android?
Calculate time between dates in list
Java= arbitrary placement of if statement gives different output? [duplicate]
Do Orphaned object in java lead to performance Issues [duplicate]
How do I mock an object of a class with parameterized constructor?
How to concatenate strings in Java?
How can I convert java.util.Calendar to org.quartz.impl.calendar.DailyCalendar?
Lose focus when user clicks outside Node's bound in JavaFX
Parallel Stream gives null items, How to do in Java 8
Extending the ObjectFactory in EclipseLink MOXy
is my implementation of builder violates mutability
Inserting a node into the n-th position in a LinkedList for Hackerrank Java
How to Split Strings into Words WITHOUT the split method or using Arrays?

Categories

HOME
testng
openmp
pycharm
objectgears
filtering
amortized-analysis
malloc
sql-server-2016
ebean
fingerprint
echarts
datastax-java-driver
virtualization
kibana-4
kentor-authservices
mapserver
pugjs
introduction
zapier
paging
abi
ehcache
mps
zurb-foundation-6
microsoft-r
crystal-reports-2010
one-to-many
cas
info.plist
maxmind
typo3-6.2.x
srcset
instant-messaging
google-cloud-endpoints-v2
functor
contact-form
rotational-matrices
http-referer
amazon-kinesis-kpl
mapbox-gl
android-ble
qwerty
uft-api
gzipstream
keydown
acoustics
eclipse-gef
qwt
network-flow
revapi
paxos
clean-architecture
gabor-filter
git-diff
carrot
forever
worker-thread
dtexec
mcafee
color-profile
query-performance
specrun
word-vba-mac
nested-sets
android-fonts
multiple-regression
livequery
design-by-contract
hsv
kendonumerictextbox
vhd
cartesian-product
ora-00900
security-testing
coveralls
emailrelay
sniffer
graph-api-explorer
modalpopup
oxwall
angularjs-ng-click
fpml
expected-exception
seaside
kgdb
rtmfp
concurrent-collections
balanced-payments
intentservice
geos
ftps
seed
android-screen-support
orchardcms-1.7
ember-app-kit
google-email-migration
armcc
regsvr32
postgresql-performance
boost-filesystem
zpt
gcj
infobox
oncheckedchanged
imac
mongomapper
gamequery
ext3
user-friendly
anti-piracy

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App