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

running time of algorithm does not match the reality
Big O notation - why is O(n^2/4) = O(n^2)
Why is theta notation never used?
Finding the big o of a function [closed]
how would you compare time complexities for two algorithms
Can I say that a Θ(n^3/2)-time algorithm is asymptotically slower than an Θ(n log n)-time algorithm?
how to calculate time complexity in big O notation of this algorithm
Where do the functions 2n^2 , 100n log n, and (log n) ^3 fit in the big-O hierarchy?
O(N) execution times
Using Big O notation for counting nodes in a tree
Big-O Hierarchy and comparisons
Recurrence relation of the following function
What is the O notation of this loop?
Mathematical Definition of Asymptotic Bound
Is O(n Log n) in polynomial time?
Why is O(n^2) the same as Θ(n^2)?

Categories

HOME
go
delphi-7
developer-tools
ng-idle
battery
composite-primary-key
graphics
vscode-extensions
gembox-spreadsheet
zip
tvos
spring-session
azure-container-service
modal-dialog
qpython
fosrestbundle
vimdiff
endeca
pega
text-mining
frequency-distribution
plink
do-while
atlassian-stash
mapstruct
jmp
pchart
exploit
object-storage
comparator
strongname
google-api-client
game-theory
definitelytyped
itunes
maze
slidetoggle
ms-access-2003
datastax-enterprise-graph
sqlpackage
record
sharding
magento-1.9.2.4
alphabetical
tex
android-augmented-reality
metawidget
urlsession
resolution
dcmtk
android-ibeacon
preloader
apollostack
sonarlint-eclipse
geotiff
tf
trojan
opengl-4
gpg-signature
branch-prediction
restore
cubism.js
angular2-testing
flex4.6
drawstring
observablecollection
udpclient
cfchart
nullable
halcon
thread-sleep
design-principles
maybe
lazybones
ons-api
cgi-bin
iced-coffeescript
viola-jones
typemock
haskell-warp
airbrake
osc
fits
angular-gettext
addressing-mode
post-commit-hook
rmysql
insight.database
high-resolution
rfc5322
sphinxql
http-patch
mbunit
route-provider
poker
facebook-timeline
fragmenttransaction
vfs
zope.interface
iboutlet
thttpd
window-decoration
flex-mobile
exponentiation
w3c-geolocation
objective-j
maven-1
office-2007
folding
pos-for-.net
cfwindow
cldc

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