Sorting algorithms/Permutation sort: Difference between revisions

Content added Content deleted
m (→‎{{header|Wren}}: Removed duplicated lines.)
(→‎{{header|R}}: New solution. It's so simple that I'm doubting myself.)
Line 1,963: Line 1,963:


=={{header|R}}==
=={{header|R}}==
===e1071===
{{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.
Line 1,980: Line 1,981:
}
}
permutationsort(c(1, 10, 9, 7, 3, 0))</lang>
permutationsort(c(1, 10, 9, 7, 3, 0))</lang>

===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.
<lang r>library(RcppAlgos)
permuSort<-function(list)
{
iter<-permuteIter(list)
while(is.unsorted(iter$nextIter())){}#iter$nextIter advances iter to the next iteration and returns it.
iter$currIter()
}
test<-sample(10)
print(test)
permuSort(test)</lang>
{{out}}
<pre>#Output
> test<-sample(10)
> print(test)
[1] 8 10 6 2 9 4 7 5 3 1
> permuSort(test)
[1] 1 2 3 4 5 6 7 8 9 10</pre>


=={{header|Racket}}==
=={{header|Racket}}==