Sorting algorithms/Strand sort

Revision as of 14:20, 5 May 2011 by rosettacode>Dkf (→‎Tcl: Added implementation)

Implement the Strand sort. This is a way of sorting numbers by extracting shorter sequences of already sorted numbers from an unsorted list.

Task
Sorting algorithms/Strand sort
You are encouraged to solve this task according to the task description, using any language you may know.
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)

J

Generally, this task should be accomplished in J using /:~. Here we take an approach that's more comparable with the other examples on this page.

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>

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] 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>