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 will be the big-O of following code?
Time complexity analysis using big-o
Big O - Determining whether the function is Big O
Why is the runtime of this code O(n^5)?
I need help writing pseudocode for findAll(k) method in binary search table
Order of growth of following function
BIG O complexity n or n^2log(n)
Proving a single-term function is big Omega
Big Oh complexity of polynomial times log N
Finding the Complexity of Nested Loops (Big O)
Does Big O notation also represent space complexity?
Is it formally correct to say that 2*n = O(2*n)?
What is the Complexity (BigO) of this Algorithm?
When does one typically prefer the little-o instead of the big-O?
big-O notation with fractions and negative exponents
simple g(n) such that f(n) = Θ(g(n))

Categories

HOME
shell
notepad++
go
google-app-engine
fuelux
wakanda
pagination
youtube-data-api-v3
obfuscation
solaris
appcelerator
hpc
x264
jquery-ui
data-visualization
computer-science
g++
google-schemas
google-chrome-app
mel
why3
ycsb
naivebayes
selectize.js
symbol
atlassian-stash
chargify
red5
git-extensions
digital-signature
mapstruct
breadth-first-search
articulate-storyline
ios10.2
monit
jboss6.x
surveymonkey
flooding
fragment-backstack
magento-1.8
autodesk-designautomation
memory-address
papaparse
biztalk-rule-engine
keras-layer
reactjs.net
reverse
arduino-ide
browser-extension
sim-toolkit
xv6
doc
android-softkeyboard
logback-groovy
stylecop
plan-9
puredata
linter
user-defined-fields
scalding
custom-font
cup
type-theory
sandcastle
choice
taocp
node-request
xdoclet
np-spring
highland.js
decimalformat
google-books
poppler
liquid-layout
teamcity-9.1
ms-access-2000
phpldapadmin
gamekit
crowd
big-ip
easyrtc
create.js
named-parameters
lnk
scriptaculous
hibernate-entitymanager
mysql-error-1044
rolify
twisted.web
argouml
wimax
booksleeve
veracity
dajaxice
fitbounds
infobright
urlrewriter
ikimagebrowserview
denied
update-statement
cdonts
mtu
vs-android
non-clustered-index
burndowncharts
murmurhash
skype4java
mathematical-notation
fail-fast-fail-early
localizable.strings
int64
technical-debt

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