Sorting algorithms/Permutation sort: Difference between revisions
Content added Content deleted
ReeceGoding (talk | contribs) m (→{{header|R}}: Syntax highlighting.) |
|||
Line 2,015: | Line 2,015: | ||
{{libheader|e1071}} |
{{libheader|e1071}} |
||
Warning: This function keeps all the possible permutations in memory at once, which becomes silly when x has 10 or more elements. |
Warning: This function keeps all the possible permutations in memory at once, which becomes silly when x has 10 or more elements. |
||
<lang |
<lang rsplus>permutationsort <- function(x) |
||
{ |
{ |
||
if(!require(e1071) stop("the package e1071 is required") |
if(!require(e1071) stop("the package e1071 is required") |
||
Line 2,034: | Line 2,034: | ||
{{libheader|RcppAlgos}} |
{{libheader|RcppAlgos}} |
||
RcppAlgos lets us do this at the speed of C++ and with some very short code. The while loop with no body strikes me as poor taste, but I know of no better way. |
RcppAlgos lets us do this at the speed of C++ and with some very short code. The while loop with no body strikes me as poor taste, but I know of no better way. |
||
<lang |
<lang rsplus>library(RcppAlgos) |
||
permuSort<-function(list) |
permuSort<-function(list) |
||
{ |
{ |
||
Line 2,053: | Line 2,053: | ||
An alternative solution would be to replace the while loop with the following: |
An alternative solution would be to replace the while loop with the following: |
||
<lang |
<lang rsplus>repeat |
||
{ |
{ |
||
if(!is.unsorted(iter$nextIter())) break |
if(!is.unsorted(iter$nextIter())) break |