-
Notifications
You must be signed in to change notification settings - Fork 0
/
plots.R
74 lines (58 loc) · 2.21 KB
/
plots.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
source("utils.R")
source("sorts.R")
generate_data <- function(sorting_algorithm, x) {
#' Генерация данных времени исполнения
#'
#' @param sort_algorithm: Function - алгоритм сортировки
#' @param x: Numeric - список размеров случайных списков, по которому будем делать замеры
#' @return time: Numeric - список времени выполнения функции
time <- c()
for (n in x) {
sa_timer <- timer(sorting_algorithm)
time <- c(time, sa_timer(get_random_list(n)))
}
return(time)
}
build_time_plot <- function(xmin = 250, xmax = 3000, step = 250) {
x <- seq(xmin, xmax, by = step)
print("генерируем данные для bubblesort")
y_bubblesort <- generate_data(bubblesort, x)
print("генерируем данные для insertsort")
y_insertsort <- generate_data(insertsort, x)
print("генерируем данные для selectionsort")
y_selectionsort <- generate_data(selectionsort, x)
print("генерируем данные для mergesort")
y_mergesort <- generate_data(mergesort, x)
print("генерируем данные для встроенной сортировки")
y_built_in <- generate_data(sort, x)
plot(
x, y_bubblesort,
type = "l",
col = "red",
xlab = "Количество элементов в списке",
ylab = "Время сортировки",
)
lines(x, y_insertsort, type = "l", col = "blue")
lines(x, y_selectionsort, type = "l", col = "green")
lines(x, y_mergesort, type = "l", col = "orange")
lines(x, y_built_in, type='l', col='black')
nsquare <- (x)^2 / (5 * 10^7)
nlogn <- x * log2(x) / (7 * 10^5)
lines(x, nsquare, col = 'grey', lty = 2, type = "l")
lines(x, nlogn, col = 'grey', lty = 2)
title(main = "Время выполнения сортировок")
legend(
"topleft",
legend = c(
"Bubblesort",
"Insert sort",
"Selection sort",
"Merge sort",
"built-in sort",
"y = O(n^2)",
"y = O(nlog(n))"
),
col = c("red", "blue", "green", "orange", "black", "grey", "grey"),
lty = 1
)
}