An implementation of the Steinhaus-Johnson-Trotter permutation algorithm.

permutations(set)

Arguments

set

a set

Value

a matrix whose rows are the permutations of set

Examples

permutations(1:3)
#> [,1] [,2] [,3] #> [1,] 1 2 3 #> [2,] 1 3 2 #> [3,] 2 1 3 #> [4,] 2 3 1 #> [5,] 3 1 2 #> [6,] 3 2 1
permutations(c('first','second','third'))
#> [,1] [,2] [,3] #> [1,] "first" "second" "third" #> [2,] "first" "third" "second" #> [3,] "second" "first" "third" #> [4,] "second" "third" "first" #> [5,] "third" "first" "second" #> [6,] "third" "second" "first"
permutations(c(1,1,3))
#> [,1] [,2] [,3] #> [1,] 1 1 3 #> [2,] 1 3 1 #> [3,] 3 1 1
apply(permutations(letters[1:6]), 1, paste, collapse = '')
#> [1] "abcdef" "abcdfe" "abcedf" "abcefd" "abcfde" "abcfed" "abdcef" "abdcfe" #> [9] "abdecf" "abdefc" "abdfce" "abdfec" "abecdf" "abecfd" "abedcf" "abedfc" #> [17] "abefcd" "abefdc" "abfcde" "abfced" "abfdce" "abfdec" "abfecd" "abfedc" #> [25] "acbdef" "acbdfe" "acbedf" "acbefd" "acbfde" "acbfed" "acdbef" "acdbfe" #> [33] "acdebf" "acdefb" "acdfbe" "acdfeb" "acebdf" "acebfd" "acedbf" "acedfb" #> [41] "acefbd" "acefdb" "acfbde" "acfbed" "acfdbe" "acfdeb" "acfebd" "acfedb" #> [49] "adbcef" "adbcfe" "adbecf" "adbefc" "adbfce" "adbfec" "adcbef" "adcbfe" #> [57] "adcebf" "adcefb" "adcfbe" "adcfeb" "adebcf" "adebfc" "adecbf" "adecfb" #> [65] "adefbc" "adefcb" "adfbce" "adfbec" "adfcbe" "adfceb" "adfebc" "adfecb" #> [73] "aebcdf" "aebcfd" "aebdcf" "aebdfc" "aebfcd" "aebfdc" "aecbdf" "aecbfd" #> [81] "aecdbf" "aecdfb" "aecfbd" "aecfdb" "aedbcf" "aedbfc" "aedcbf" "aedcfb" #> [89] "aedfbc" "aedfcb" "aefbcd" "aefbdc" "aefcbd" "aefcdb" "aefdbc" "aefdcb" #> [97] "afbcde" "afbced" "afbdce" "afbdec" "afbecd" "afbedc" "afcbde" "afcbed" #> [105] "afcdbe" "afcdeb" "afcebd" "afcedb" "afdbce" "afdbec" "afdcbe" "afdceb" #> [113] "afdebc" "afdecb" "afebcd" "afebdc" "afecbd" "afecdb" "afedbc" "afedcb" #> [121] "bacdef" "bacdfe" "bacedf" "bacefd" "bacfde" "bacfed" "badcef" "badcfe" #> [129] "badecf" "badefc" "badfce" "badfec" "baecdf" "baecfd" "baedcf" "baedfc" #> [137] "baefcd" "baefdc" "bafcde" "bafced" "bafdce" "bafdec" "bafecd" "bafedc" #> [145] "bcadef" "bcadfe" "bcaedf" "bcaefd" "bcafde" "bcafed" "bcdaef" "bcdafe" #> [153] "bcdeaf" "bcdefa" "bcdfae" "bcdfea" "bceadf" "bceafd" "bcedaf" "bcedfa" #> [161] "bcefad" "bcefda" "bcfade" "bcfaed" "bcfdae" "bcfdea" "bcfead" "bcfeda" #> [169] "bdacef" "bdacfe" "bdaecf" "bdaefc" "bdafce" "bdafec" "bdcaef" "bdcafe" #> [177] "bdceaf" "bdcefa" "bdcfae" "bdcfea" "bdeacf" "bdeafc" "bdecaf" "bdecfa" #> [185] "bdefac" "bdefca" "bdface" "bdfaec" "bdfcae" "bdfcea" "bdfeac" "bdfeca" #> [193] "beacdf" "beacfd" "beadcf" "beadfc" "beafcd" "beafdc" "becadf" "becafd" #> [201] "becdaf" "becdfa" "becfad" "becfda" "bedacf" "bedafc" "bedcaf" "bedcfa" #> [209] "bedfac" "bedfca" "befacd" "befadc" "befcad" "befcda" "befdac" "befdca" #> [217] "bfacde" "bfaced" "bfadce" "bfadec" "bfaecd" "bfaedc" "bfcade" "bfcaed" #> [225] "bfcdae" "bfcdea" "bfcead" "bfceda" "bfdace" "bfdaec" "bfdcae" "bfdcea" #> [233] "bfdeac" "bfdeca" "bfeacd" "bfeadc" "bfecad" "bfecda" "bfedac" "bfedca" #> [241] "cabdef" "cabdfe" "cabedf" "cabefd" "cabfde" "cabfed" "cadbef" "cadbfe" #> [249] "cadebf" "cadefb" "cadfbe" "cadfeb" "caebdf" "caebfd" "caedbf" "caedfb" #> [257] "caefbd" "caefdb" "cafbde" "cafbed" "cafdbe" "cafdeb" "cafebd" "cafedb" #> [265] "cbadef" "cbadfe" "cbaedf" "cbaefd" "cbafde" "cbafed" "cbdaef" "cbdafe" #> [273] "cbdeaf" "cbdefa" "cbdfae" "cbdfea" "cbeadf" "cbeafd" "cbedaf" "cbedfa" #> [281] "cbefad" "cbefda" "cbfade" "cbfaed" "cbfdae" "cbfdea" "cbfead" "cbfeda" #> [289] "cdabef" "cdabfe" "cdaebf" "cdaefb" "cdafbe" "cdafeb" "cdbaef" "cdbafe" #> [297] "cdbeaf" "cdbefa" "cdbfae" "cdbfea" "cdeabf" "cdeafb" "cdebaf" "cdebfa" #> [305] "cdefab" "cdefba" "cdfabe" "cdfaeb" "cdfbae" "cdfbea" "cdfeab" "cdfeba" #> [313] "ceabdf" "ceabfd" "ceadbf" "ceadfb" "ceafbd" "ceafdb" "cebadf" "cebafd" #> [321] "cebdaf" "cebdfa" "cebfad" "cebfda" "cedabf" "cedafb" "cedbaf" "cedbfa" #> [329] "cedfab" "cedfba" "cefabd" "cefadb" "cefbad" "cefbda" "cefdab" "cefdba" #> [337] "cfabde" "cfabed" "cfadbe" "cfadeb" "cfaebd" "cfaedb" "cfbade" "cfbaed" #> [345] "cfbdae" "cfbdea" "cfbead" "cfbeda" "cfdabe" "cfdaeb" "cfdbae" "cfdbea" #> [353] "cfdeab" "cfdeba" "cfeabd" "cfeadb" "cfebad" "cfebda" "cfedab" "cfedba" #> [361] "dabcef" "dabcfe" "dabecf" "dabefc" "dabfce" "dabfec" "dacbef" "dacbfe" #> [369] "dacebf" "dacefb" "dacfbe" "dacfeb" "daebcf" "daebfc" "daecbf" "daecfb" #> [377] "daefbc" "daefcb" "dafbce" "dafbec" "dafcbe" "dafceb" "dafebc" "dafecb" #> [385] "dbacef" "dbacfe" "dbaecf" "dbaefc" "dbafce" "dbafec" "dbcaef" "dbcafe" #> [393] "dbceaf" "dbcefa" "dbcfae" "dbcfea" "dbeacf" "dbeafc" "dbecaf" "dbecfa" #> [401] "dbefac" "dbefca" "dbface" "dbfaec" "dbfcae" "dbfcea" "dbfeac" "dbfeca" #> [409] "dcabef" "dcabfe" "dcaebf" "dcaefb" "dcafbe" "dcafeb" "dcbaef" "dcbafe" #> [417] "dcbeaf" "dcbefa" "dcbfae" "dcbfea" "dceabf" "dceafb" "dcebaf" "dcebfa" #> [425] "dcefab" "dcefba" "dcfabe" "dcfaeb" "dcfbae" "dcfbea" "dcfeab" "dcfeba" #> [433] "deabcf" "deabfc" "deacbf" "deacfb" "deafbc" "deafcb" "debacf" "debafc" #> [441] "debcaf" "debcfa" "debfac" "debfca" "decabf" "decafb" "decbaf" "decbfa" #> [449] "decfab" "decfba" "defabc" "defacb" "defbac" "defbca" "defcab" "defcba" #> [457] "dfabce" "dfabec" "dfacbe" "dfaceb" "dfaebc" "dfaecb" "dfbace" "dfbaec" #> [465] "dfbcae" "dfbcea" "dfbeac" "dfbeca" "dfcabe" "dfcaeb" "dfcbae" "dfcbea" #> [473] "dfceab" "dfceba" "dfeabc" "dfeacb" "dfebac" "dfebca" "dfecab" "dfecba" #> [481] "eabcdf" "eabcfd" "eabdcf" "eabdfc" "eabfcd" "eabfdc" "eacbdf" "eacbfd" #> [489] "eacdbf" "eacdfb" "eacfbd" "eacfdb" "eadbcf" "eadbfc" "eadcbf" "eadcfb" #> [497] "eadfbc" "eadfcb" "eafbcd" "eafbdc" "eafcbd" "eafcdb" "eafdbc" "eafdcb" #> [505] "ebacdf" "ebacfd" "ebadcf" "ebadfc" "ebafcd" "ebafdc" "ebcadf" "ebcafd" #> [513] "ebcdaf" "ebcdfa" "ebcfad" "ebcfda" "ebdacf" "ebdafc" "ebdcaf" "ebdcfa" #> [521] "ebdfac" "ebdfca" "ebfacd" "ebfadc" "ebfcad" "ebfcda" "ebfdac" "ebfdca" #> [529] "ecabdf" "ecabfd" "ecadbf" "ecadfb" "ecafbd" "ecafdb" "ecbadf" "ecbafd" #> [537] "ecbdaf" "ecbdfa" "ecbfad" "ecbfda" "ecdabf" "ecdafb" "ecdbaf" "ecdbfa" #> [545] "ecdfab" "ecdfba" "ecfabd" "ecfadb" "ecfbad" "ecfbda" "ecfdab" "ecfdba" #> [553] "edabcf" "edabfc" "edacbf" "edacfb" "edafbc" "edafcb" "edbacf" "edbafc" #> [561] "edbcaf" "edbcfa" "edbfac" "edbfca" "edcabf" "edcafb" "edcbaf" "edcbfa" #> [569] "edcfab" "edcfba" "edfabc" "edfacb" "edfbac" "edfbca" "edfcab" "edfcba" #> [577] "efabcd" "efabdc" "efacbd" "efacdb" "efadbc" "efadcb" "efbacd" "efbadc" #> [585] "efbcad" "efbcda" "efbdac" "efbdca" "efcabd" "efcadb" "efcbad" "efcbda" #> [593] "efcdab" "efcdba" "efdabc" "efdacb" "efdbac" "efdbca" "efdcab" "efdcba" #> [601] "fabcde" "fabced" "fabdce" "fabdec" "fabecd" "fabedc" "facbde" "facbed" #> [609] "facdbe" "facdeb" "facebd" "facedb" "fadbce" "fadbec" "fadcbe" "fadceb" #> [617] "fadebc" "fadecb" "faebcd" "faebdc" "faecbd" "faecdb" "faedbc" "faedcb" #> [625] "fbacde" "fbaced" "fbadce" "fbadec" "fbaecd" "fbaedc" "fbcade" "fbcaed" #> [633] "fbcdae" "fbcdea" "fbcead" "fbceda" "fbdace" "fbdaec" "fbdcae" "fbdcea" #> [641] "fbdeac" "fbdeca" "fbeacd" "fbeadc" "fbecad" "fbecda" "fbedac" "fbedca" #> [649] "fcabde" "fcabed" "fcadbe" "fcadeb" "fcaebd" "fcaedb" "fcbade" "fcbaed" #> [657] "fcbdae" "fcbdea" "fcbead" "fcbeda" "fcdabe" "fcdaeb" "fcdbae" "fcdbea" #> [665] "fcdeab" "fcdeba" "fceabd" "fceadb" "fcebad" "fcebda" "fcedab" "fcedba" #> [673] "fdabce" "fdabec" "fdacbe" "fdaceb" "fdaebc" "fdaecb" "fdbace" "fdbaec" #> [681] "fdbcae" "fdbcea" "fdbeac" "fdbeca" "fdcabe" "fdcaeb" "fdcbae" "fdcbea" #> [689] "fdceab" "fdceba" "fdeabc" "fdeacb" "fdebac" "fdebca" "fdecab" "fdecba" #> [697] "feabcd" "feabdc" "feacbd" "feacdb" "feadbc" "feadcb" "febacd" "febadc" #> [705] "febcad" "febcda" "febdac" "febdca" "fecabd" "fecadb" "fecbad" "fecbda" #> [713] "fecdab" "fecdba" "fedabc" "fedacb" "fedbac" "fedbca" "fedcab" "fedcba"