# 1. Instalamos y cargamos la librerĆa para medir tiempos
if(!require(microbenchmark)) install.packages("microbenchmark")
library(microbenchmark)
# 2. Tomo la muestra de 20.000 nĆŗmeros
set.seed(123)
x <- sample(1:100000, 20000)
burbuja <- function(x) {
n <- length(x)
for (j in 1:(n - 1)) {
for (i in 1:(n - j)) {
if (x[i] > x[i + 1]) {
temp <- x[i]
x[i] <- x[i + 1]
x[i + 1] <- temp
}
}
}
return(x)
}
res_comparativa <- microbenchmark(
"MƩtodo Burbuja" = burbuja(x),
"Comando SORT" = sort(x),
times = 1
)
# Mostramos la tabla de tiempos obtenida
print(res_comparativa)
## Unit: microseconds
## expr min lq mean median uq
## MƩtodo Burbuja 2.06482e+07 2.06482e+07 2.06482e+07 2.06482e+07 2.06482e+07
## Comando SORT 8.45241e+02 8.45241e+02 8.45241e+02 8.45241e+02 8.45241e+02
## max neval
## 2.06482e+07 1
## 8.45241e+02 1
boxplot(res_comparativa, unit = "s",
main = "Diferencia de tiempo en segundos",
col = c("lightblue", "lightgreen"))
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.