Sorting algorithms/Strand sort: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎Tcl: Added implementation)
m (→‎{{header|Tcl}}: added some notes on design choices)
Line 68: Line 68:
while {[llength $A]} {
while {[llength $A]} {
set sublist [lrange $A 0 0]
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 {}
set newA {}
foreach a [lrange $A 1 end] {
foreach a [lrange $A 1 end] {

Revision as of 15:03, 5 May 2011

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)

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

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