-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortPlot Comps.R
More file actions
52 lines (43 loc) · 1.38 KB
/
Copy pathSortPlot Comps.R
File metadata and controls
52 lines (43 loc) · 1.38 KB
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
#plots
library(lattice)
library(microbenchmark)
## plot number of comparisons per log2nfact
avg1 <- function(funcs = list(), r1=20, l = 100, measure=2){
y <- numeric(length(funcs))
names(y) <- funcs
avg = numeric(4)
for (i in 1:r1) {
s <- sample(1:l)
comps = sapply(funcs, function(x) get(x)(s)[measure])
y <- rbind(y, comps)
}
log2nfact = sum(sapply(1:l, function(n) log2(n)))
colMeans(y[-1,]) / log2nfact
}
#[measure] decides wheather to count number of swaps(1) or comparisons(2)
sort_plot1 <- function(seqn, r1=20, funcs = c("SE86", "TO92", "CI01"), measure=2){
itr <- sapply(seqn, function (x) floor(10^x))
y <- numeric(length(funcs))
names(y) <- funcs
for (i in itr) {
y <- rbind(y,
avg1(funcs, r1, l=i, measure)
)
print(i)
}
D = as.data.frame(cbind(y[-1, ], itr))
frml = paste(
paste(
names(D)[-dim(D)[2]],
collapse="+"),
"~itr")
write.csv(D, sprintf("data/count(%d) -- %s -- %f, %f.csv", measure, paste(funcs, collapse = " + "), seqn[1], seqn[length(seqn)]))
xyplot(as.formula(frml), data = D,
type=c('p', 'l'),
auto.key = TRUE,
scales = list(x=list(log = 10))
)
}
seqn = seq(from=2, to=6, length=15)
sort_plot1(seqn, funcs = c("QuickSortH", "QuickSortL", "CI01", "TO92", "SE86"))
sort_plot1(seqn, funcs = c("QuickSortH", "QuickSortL", "CI01", "TO92", "SE86"), measure = 1)