Sorting algorithms/Strand sort
You are encouraged to solve this task according to the task description, using any language you may know.
Sorting Algorithm
This is a sorting algorithm. It may be applied to a set of data in order to sort it.
For comparing various sorts, see compare sorts.
For other sorting algorithms, see sorting algorithms, or:
Heap sort | Merge sort | Patience sort | Quick sort
O(n log2n) sorts
Shell Sort
O(n2) sorts
Bubble sort |
Cocktail sort |
Cocktail sort with shifting bounds |
Comb sort |
Cycle sort |
Gnome sort |
Insertion sort |
Selection sort |
Strand sort
other sorts
Bead sort |
Bogo sort |
Common sorted list |
Composite structures sort |
Custom comparator sort |
Counting sort |
Disjoint sublist sort |
External sort |
Jort sort |
Lexicographical sort |
Natural sorting |
Order by pair comparisons |
Order disjoint list items |
Order two numerical lists |
Object identifier (OID) sort |
Pancake sort |
Quickselect |
Permutation sort |
Radix sort |
Ranking methods |
Remove duplicate elements |
Sleep sort |
Stooge sort |
[Sort letters of a string] |
Three variable sort |
Topological sort |
Tree sort
This page uses content from Wikipedia. The original article was at Strand sort. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) |
Implement the Strand sort. This is a way of sorting numbers by extracting shorter sequences of already sorted numbers from an unsorted list.
J
Using merge
defined at Sorting algorithms/Merge sort#J:
<lang j>strandSort=: (#~ merge $:^:(0<#)@(#~ -.)) (= >./\)</lang>
Example use:
<lang j> strandSort 3 1 5 4 2 1 2 3 4 5</lang>
Note: the individual strands can be seen by using ; instead of merge:
<lang j> ((#~ ; $:^:(0<#)@(#~ -.)) (= >./\)) 3 1 5 4 2 ┌───┬───┬─┬┐ │3 5│1 4│2││ └───┴───┴─┴┘</lang>
Also note that the order in which J processes the strands differs from the pseudocode currently at the wikipedia page on strand sort and matches the haskell implementation currently at the wikipedia page.
Java
<lang java5>import java.util.Arrays; import java.util.LinkedList;
public class Strand{ public static <E extends Comparable<? super E>> LinkedList<E> strandSort(LinkedList<E> list){ if(list.size() <= 1) return list;
LinkedList<E> result = new LinkedList<E>(); while(list.size() > 0){ LinkedList<E> sorted = new LinkedList<E>(); sorted.add(list.removeFirst()); //same as remove() or remove(0) for(int i = 0; i < list.size();i++){ E elem = list.get(i); if(sorted.peekLast().compareTo(elem) <= 0){ sorted.addLast(elem); //same as add(elem) or add(0, elem) list.remove(i--); } } result = merge(sorted, result); } return result; }
private static <E extends Comparable<? super E>> LinkedList<E> merge(LinkedList<E> left, LinkedList<E> right){ LinkedList<E> result = new LinkedList<E>(); while(!left.isEmpty() && !right.isEmpty()){ //change the direction of this comparison to change the direction of the sort if(left.peek().compareTo(right.peek()) <= 0) result.add(left.remove()); else result.add(right.remove()); } result.addAll(left); result.addAll(right); return result; }
public static void main(String[] args){ System.out.println(strandSort(new LinkedList<Integer>(Arrays.asList(3,1,2,4,5)))); System.out.println(strandSort(new LinkedList<Integer>(Arrays.asList(3,3,1,2,4,5)))); System.out.println(strandSort(new LinkedList<Integer>(Arrays.asList(3,3,1,2,4,3,5,6)))); } }</lang> Output:
[1, 2, 3, 4, 5] [1, 2, 3, 3, 4, 5] [1, 2, 3, 3, 3, 4, 5, 6]
PicoLisp
<lang PicoLisp>(de strandSort (Lst)
(let Res NIL # Result list (while Lst (let Sub (circ (car Lst)) # Build sublist as fifo (setq Lst (filter '((X) (or (> (car Sub) X) (nil (fifo 'Sub X)) ) ) (cdr Lst) ) Res (make (while (or Res Sub) # Merge (link (if2 Res Sub (if (>= (car Res) (cadr Sub)) (fifo 'Sub) (pop 'Res) ) (pop 'Res) (fifo 'Sub) ) ) ) ) ) ) ) Res ) )</lang>
Test:
: (strandSort (3 1 5 4 2)) -> (1 2 3 4 5) : (strandSort (3 abc 1 (d e f) 5 T 4 NIL 2)) -> (NIL 1 2 3 4 5 abc (d e f) T)
Tcl
<lang tcl>proc merge {listVar toMerge} {
upvar 1 $listVar v set i [set j 0] set out {} while {$i<[llength $v] && $j<[llength $toMerge]} {
if {[set a [lindex $v $i]] < [set b [lindex $toMerge $j]]} { lappend out $a incr i } else { lappend out $b incr j }
} # Done the merge, but will be one source with something left # This will handle all that by doing a merge of the remnants onto the end set v [concat $out [lrange $v $i end] [lrange $toMerge $j end]] return
}
proc strandSort A {
set results {} while {[llength $A]} {
set sublist [lrange $A 0 0] # We build a list of items that weren't filtered rather than removing "in place" # because this fits better with the way Tcl values work (the underlying data # structure is an array, not a linked list). set newA {} foreach a [lrange $A 1 end] { if {$a > [lindex $sublist end]} { lappend sublist $a } else { lappend newA $a } } set A $newA merge results $sublist
} return $results
}
puts [strandSort {3 1 5 4 2}]</lang>