Jump to content

Permutations: Difference between revisions

m
Line 3,706:
=={{header|R}}==
<lang r>next.perm <- function(p) {
n <- length(p)
i <- n - 1
r = TRUET
for (i in seq(n - 1):, 1)) {
if (p[i] < p[i + 1]) {
r = FALSEF
break
}
}
}
j <- i + 1
k <- n
while (j < k) {
x <- p[j]
p[j] <- p[k]
p[k] <- x
j <- j + 1
k <- k - 1
}
if(r) return(NULL)
j <- n
while (p[j] > p[i]) j <- j - 1
j <- j + 1
x <- p[i]
p[i] <- p[j]
p[j] <- x
return(p)
}
 
print.perms <- function(n) {
p <- 1:n
while (!is.null(p)) {
cat(p, "\n")
p <- next.perm(p)
}
}
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.