-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript2.R
More file actions
53 lines (47 loc) · 1.36 KB
/
script2.R
File metadata and controls
53 lines (47 loc) · 1.36 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
52
53
# Animation of sorting N integers using comb sort in R
# https://rosettacode.org/wiki/Sorting_algorithms/Comb_sort#R
# Author: Maurits Evers (maurits.evers@anu.edu.au)
# Date: 16 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 with replacement from the interval [1, N]
N <- 30
set.seed(2018)
x <- sample(N, replace = T)
# Comb sort
lst <- list()
step <- 0
gap <- length(x)
swaps <- 1
while(gap > 1 & swaps == 1) {
gap <- floor(gap / 1.3)
if (gap < 1) gap = 1
swaps <- 0
i <- 1
while (i + gap <= length(x)) {
if( x[i] > x[i + gap]) {
step <- step + 1
x[c(i, i + gap)] <- x[c(i + gap, i)]
swaps <- 1
lst[[step]] <- data.frame(
pos = 1:length(x),
x = x,
status = replace(rep("keep", length(x)), c(i, i + gap), "swap"))
}
i <- i + 1
}
}
# 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_comb.gif")