-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.R
More file actions
46 lines (40 loc) · 1.15 KB
/
Copy pathscript.R
File metadata and controls
46 lines (40 loc) · 1.15 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
# Animation of sorting N integers using bubble sort in R
# Author: Maurits Evers (maurits.evers@anu.edu.au)
# Date: 15 September 2019
# Licence: GPL-3
# Dependencies: tidyverse, gganimate
# Tested on R_3.6.1, tidyverse_1.2.1, gganimate_1.0.3
# Randomly sample N values from the interval [1, N]
N <- 20
set.seed(2018)
x <- sample(N)
# Bubble sort
lst <- list()
step <- 0
for (i in 1:(length(x) - 1)) {
for (j in 1:(length(x) - i)) {
if (x[j] > x[j+1]) {
step <- step + 1
tmp <- x[j]
x[j] <- x[j + 1]
x[j + 1] <- tmp
lst[[step]] <- data.frame(
pos = 1:length(x),
x = x,
status = replace(rep("keep", length(x)), c(j, j + 1), "swap"))
}
}
}
# Animate
library(gganimate)
library(tidyverse)
a <- bind_rows(lst, .id = "step") %>%
mutate(step = factor(step, levels = 1:length(lst))) %>%
ggplot(aes(pos, x, fill = status)) +
geom_col() +
scale_x_continuous(breaks = 1:length(x)) +
labs(title = "Step: {current_frame}") +
transition_manual(step) +
ease_aes("linear")
animate(a, end_pause = 10)
anim_save("animation.gif")