Skip to content

Instantly share code, notes, and snippets.

@jweslley
Created April 12, 2011 16:58
Show Gist options
  • Save jweslley/915913 to your computer and use it in GitHub Desktop.
Save jweslley/915913 to your computer and use it in GitHub Desktop.
Running time of common time complexities
big_O.plot <- function(count,log="") {
n <- seq(count)
plot(n, n*n, log=log, type='l', col='red',
ylab="Running time", xlab='n (# of elements)')
lines(n, n*log(n), col='green')
lines(n, n, col='blue')
lines(n, log(n), col='black')
title('Running time of common time complexities')
legend('topleft', lty=1, cex=0.8, inset=.025,
title='Big O notation',
c(
expression(paste("O(", n^2, ")")),
expression(paste("O(", n*log(n), ")")),
expression(paste("O(", n, ")")),
expression(paste("O(", log(n), ")"))
),
col=c('red','green','blue','black')
)
}
png('time_complexity_10.png')
big_O.plot(10)
png('time_complexity_1000.png')
big_O.plot(1000)
png('time_complexity_10000.png')
big_O.plot(10000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment