new files
This commit is contained in:
parent
3af7344581
commit
86c034bb8b
1364 changed files with 21352 additions and 0 deletions
12
Task/Knuth-shuffle/R/knuth-shuffle-1.r
Normal file
12
Task/Knuth-shuffle/R/knuth-shuffle-1.r
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
fisheryatesshuffle <- function(n)
|
||||
{
|
||||
pool <- seq_len(n)
|
||||
a <- c()
|
||||
while(length(pool) > 0)
|
||||
{
|
||||
k <- sample.int(length(pool), 1)
|
||||
a <- c(a, pool[k])
|
||||
pool <- pool[-k]
|
||||
}
|
||||
a
|
||||
}
|
||||
21
Task/Knuth-shuffle/R/knuth-shuffle-2.r
Normal file
21
Task/Knuth-shuffle/R/knuth-shuffle-2.r
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
fisheryatesknuthshuffle <- function(n)
|
||||
{
|
||||
a <- seq_len(n)
|
||||
while(n >=2)
|
||||
{
|
||||
k <- sample.int(n, 1)
|
||||
if(k != n)
|
||||
{
|
||||
temp <- a[k]
|
||||
a[k] <- a[n]
|
||||
a[n] <- temp
|
||||
}
|
||||
n <- n - 1
|
||||
}
|
||||
a
|
||||
}
|
||||
|
||||
#Example usage:
|
||||
fisheryatesshuffle(6) # e.g. 1 3 6 2 4 5
|
||||
x <- c("foo", "bar", "baz", "quux")
|
||||
x[fisheryatesknuthshuffle(4)] # e.g. "bar" "baz" "quux" "foo"
|
||||
Loading…
Add table
Add a link
Reference in a new issue