big-o


Counting Operations to Produce Polynomials


I am struggling to accurately count the operations for this method in order to produce a polynomial f(x):
public static int numOccurrences(int n){
int count = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(i == j) {
continue;
}
if(strings[i] == strings[j]){
count++;
}
}
}
return count;
}
So far this is my understanding:
int count = 0; //1 time
int i = 0; //1 time
while(i < n) //n times
int j = 0; //n times
while(j<n) //n*n times
if(i == j) //n*n times
continue; //n times
if(strings[i] == strings[j]) //n*n+2 times
count++; //n*n times
i++ //n*n times
j++ //n*n times
return count; //1 time
One we are inside of a nested loop with the if statements, I am wondering how I count the operations if what I have above is incorrect?
Also, for the line if(strings[i] == strings[j]) I am counting 1 for the test, 1 for grabbing strings[i] and 1 for strings[j]. Is this correct?
My final answer was: f(x) = 5n^4+10n^3+3n+3
and I sincerely apologize if this is insanely incorrect!
I wanted to share my answer just in case someone stumbles upon this post.
An easier way of producing a polynomial that I discovered from a tutor today is to count each operation, BUT instead of writing out that everything inside a loop happens n*n times, we would simply count the single steps and nest the those individual steps (like i++) inside of n.
For example:
int count = 0; //1
int i = 0; //+ 1
while(i < n) //+ n(
i++; //+ 1
int j = 0; //+ 1
while(j < n) //+ n(
j++ //+ 1
if(i == j) //+ 1
continue; //+ 1
if(strings[i] == strings[j]) //+ 3
count++; //+ 1
return count; //+ 1
add all of them up, we get = 1+1+n(1+1+n(1+1+1+3+1))+1
Simplify = 6n^2+2n+3

Related Links

What does O(O(f(n))) mean?
3D Matrix traversal Big-O
Big-O Sigma Notation
Big O Of Mod 20
What is the asymptotic worst-case running time of this function?
Big-O of `2n-2n^3`
Big O complexity of two nested loops
Order of Growth for f(n) and g(n)
Asymptotic Complexity for an Algorithm
Big-O Nested While Loop
Big O complexity of this loop
Does inside “if” statement change running time of algorithm?
Algorithm Peer Review
Big O notation algebra
Big-Oh notation theory, Exam perperation: Given This task, how can i calculate the big-Oh
What are the Big-Oh complexities of these tasks?

Categories

HOME
uml
notepad++
google-app-engine
kentico
hyperlink
3d
flex
alsa
dojo
regular-language
settings
arangodb
openpyxl
ibeacon-android
endpoint
speech-synthesis
plink
capistrano3
atlassian-stash
job-scheduling
fileserver
cfml
simpleitk
springfox
flask-sqlalchemy
dynamically-generated
onload
fat
stackexchange
mat
testbed
hibernate-validator
pop3
numpy-broadcasting
salesforce-communities
wurfl
amazon-ses
bc
verifiable-c
cin
comparison-operators
portforwarding
android-ibeacon
linqpad
glm-math
gitlist
robomongo
spring-integration-sftp
rserve
insight
remedy
pair-programming
scalding
edit
er-diagram
seq
trigger.io
installshield-2010
pre-build-event
embedding
aurora
libav
crouton-os
jbase
file-move
ons-api
android-launcher
airbrake
alloy-ui
omnifaces
shopizer
administration
post-commit-hook
jama
jqmodal
kissfft
personalization
rcaller
matrix-inverse
spring-remoting
mutual-authentication
authlogic
file-not-found
extjs2
menubar
clickbank
xmltodict
ecslidingviewcontroller
gd-graph
nokia-s40
qtconcurrent
pyunit
relative
luabind
django-paypal
squishit
rowlex
sifr
echo3
strtod
cuda.net
wysiwym
pbcopy
great-circle

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