Sorting algorithms/Strand sort: Difference between revisions

added C++
(added C++)
Line 3:
{{Wikipedia|Strand sort}}
Implement the [[wp:Strand sort|Strand sort]]. This is a way of sorting numbers by extracting shorter sequences of already sorted numbers from an unsorted list.
 
=={{header|C++}}==
<lang cpp>#include <list>
 
template <typename T>
std::list<T> strandSort(std::list<T> lst) {
if (lst.size() <= 1)
return lst;
std::list<T> result;
std::list<T> sorted;
while (!lst.empty()) {
sorted.push_back(lst.front());
lst.pop_front();
for (typename std::list<T>::iterator it = lst.begin(); it != lst.end(); ) {
if (sorted.back() <= *it) {
sorted.push_back(*it);
it = lst.erase(it);
} else
it++;
}
result.merge(sorted);
}
return result;
}</lang>
 
=={{header|J}}==
Line 34 ⟶ 58:
 
public class Strand{
// note: the input list is destroyed
public static <E extends Comparable<? super E>>
LinkedList<E> strandSort(LinkedList<E> list){
Anonymous user