Sorting algorithms/Strand sort: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 14: Line 14:
{{trans|Python}}
{{trans|Python}}


<lang 11l>F merge_list(&a, &b)
<syntaxhighlight lang="11l">F merge_list(&a, &b)
[Int] out
[Int] out
L !a.empty & !b.empty
L !a.empty & !b.empty
Line 41: Line 41:
R out
R out


print(strand_sort(&[1, 6, 3, 2, 1, 7, 5, 3]))</lang>
print(strand_sort(&[1, 6, 3, 2, 1, 7, 5, 3]))</syntaxhighlight>


{{out}}
{{out}}
Line 50: Line 50:
=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
{{works with|AutoHotkey_L}}
{{works with|AutoHotkey_L}}
<lang AutoHotkey>string =
<syntaxhighlight lang="autohotkey">string =
(
(
-2 0 -2 5 5 3 -1 -3 5 5 0 2 -4 4 2
-2 0 -2 5 5 3 -1 -3 5 5 0 2 -4 4 2
Line 76: Line 76:
string := list, list = "", index := 0
string := list, list = "", index := 0
}
}
esc::ExitApp</lang>outout<lang>
esc::ExitApp</syntaxhighlight>outout<syntaxhighlight lang="text">
unsorted:-2 0 -2 5 5 3 -1 -3 5 5 0 2 -4 4 2
unsorted:-2 0 -2 5 5 3 -1 -3 5 5 0 2 -4 4 2
Sorted:-4 -3 -2 -2 -1 0 0 2 2 3 4 5 5 5 5</lang>
Sorted:-4 -3 -2 -2 -1 0 0 2 2 3 4 5 5 5 5</syntaxhighlight>


=={{header|C}}==
=={{header|C}}==
Strand sort using singly linked list. C99, compiled with <code>gcc -std=c99</code>
Strand sort using singly linked list. C99, compiled with <code>gcc -std=c99</code>
<lang C>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>


typedef struct node_t *node, node_t;
typedef struct node_t *node, node_t;
Line 159: Line 159:


return 0;
return 0;
}</lang>outout<lang>before sort: -2 0 -2 5 5 3 -1 -3 5 5 0 2 -4 4 2
}</syntaxhighlight>outout<syntaxhighlight lang="text">before sort: -2 0 -2 5 5 3 -1 -3 5 5 0 2 -4 4 2
after sort: -4 -3 -2 -2 -1 0 0 2 2 3 4 5 5 5 5</lang>
after sort: -4 -3 -2 -2 -1 0 0 2 2 3 4 5 5 5 5</syntaxhighlight>


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>#include <list>
<syntaxhighlight lang="cpp">#include <list>


template <typename T>
template <typename T>
Line 184: Line 184:
}
}
return result;
return result;
}</lang>
}</syntaxhighlight>


=={{header|Clojure}}==
=={{header|Clojure}}==
<lang Clojure>(ns rosettacode.strand-sort)
<syntaxhighlight lang="clojure">(ns rosettacode.strand-sort)


(defn merge-join
(defn merge-join
Line 222: Line 222:
(strand-sort [1, 6, 3, 2, 1, 7, 5, 3])
(strand-sort [1, 6, 3, 2, 1, 7, 5, 3])
;;=> (1 1 2 3 3 5 6 7)
;;=> (1 1 2 3 3 5 6 7)
</syntaxhighlight>
</lang>


=={{header|CMake}}==
=={{header|CMake}}==
Only for lists of integers.
Only for lists of integers.
<lang cmake># strand_sort(<output variable> [<value>...]) sorts a list of integers.
<syntaxhighlight lang="cmake"># strand_sort(<output variable> [<value>...]) sorts a list of integers.
function(strand_sort var)
function(strand_sort var)
# Strand sort moves elements from _ARGN_ to _answer_.
# Strand sort moves elements from _ARGN_ to _answer_.
Line 287: Line 287:


set("${var}" ${answer} PARENT_SCOPE)
set("${var}" ${answer} PARENT_SCOPE)
endfunction(strand_sort)</lang>
endfunction(strand_sort)</syntaxhighlight>


<lang cmake>strand_sort(result 11 55 55 44 11 33 33 44 22 22)
<syntaxhighlight lang="cmake">strand_sort(result 11 55 55 44 11 33 33 44 22 22)
message(STATUS "${result}") # -- 11;11;22;22;33;33;44;44;55;55</lang>
message(STATUS "${result}") # -- 11;11;22;22;33;33;44;44;55;55</syntaxhighlight>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
<lang lisp>(defun strand-sort (l cmp)
<syntaxhighlight lang="lisp">(defun strand-sort (l cmp)
(if l
(if l
(let* ((l (reverse l))
(let* ((l (reverse l))
Line 303: Line 303:
(let ((r (loop repeat 15 collect (random 10))))
(let ((r (loop repeat 15 collect (random 10))))
(print r)
(print r)
(print (strand-sort r #'<)))</lang>output<lang>(5 8 6 0 6 8 4 7 0 7 1 5 3 3 6)
(print (strand-sort r #'<)))</syntaxhighlight>output<syntaxhighlight lang="text">(5 8 6 0 6 8 4 7 0 7 1 5 3 3 6)
(0 0 1 3 3 4 5 5 6 6 6 7 7 8 8)</lang>
(0 0 1 3 3 4 5 5 6 6 6 7 7 8 8)</syntaxhighlight>


=={{header|D}}==
=={{header|D}}==


=== Using doubly linked lists ===
=== Using doubly linked lists ===
<lang d>import std.stdio, std.container;
<syntaxhighlight lang="d">import std.stdio, std.container;


DList!T strandSort(T)(DList!T list) {
DList!T strandSort(T)(DList!T list) {
Line 352: Line 352:
foreach (e; strandSort(lst))
foreach (e; strandSort(lst))
write(e, " ");
write(e, " ");
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>-4 -3 -2 -2 -1 0 0 2 2 3 4 5 5 5 5 </pre>
<pre>-4 -3 -2 -2 -1 0 0 2 2 3 4 5 5 5 5 </pre>


=== Faster version using slices ===
=== Faster version using slices ===
<lang d>import std.stdio, std.array;
<syntaxhighlight lang="d">import std.stdio, std.array;


T[] strandSort(T)(const(T)[] list) pure nothrow {
T[] strandSort(T)(const(T)[] list) pure nothrow {
Line 391: Line 391:
const arr = [-2, 0, -2, 5, 5, 3, -1, -3, 5, 5, 0, 2, -4, 4, 2];
const arr = [-2, 0, -2, 5, 5, 3, -1, -3, 5, 5, 0, 2, -4, 4, 2];
arr.strandSort.writeln;
arr.strandSort.writeln;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>[-4, -3, -2, -2, -1, 0, 0, 2, 2, 3, 4, 5, 5, 5, 5]</pre>
<pre>[-4, -3, -2, -2, -1, 0, 0, 2, 2, 3, 4, 5, 5, 5, 5]</pre>
Line 397: Line 397:
=={{header|Elixir}}==
=={{header|Elixir}}==
{{trans|Ruby}}
{{trans|Ruby}}
<lang elixir>defmodule Sort do
<syntaxhighlight lang="elixir">defmodule Sort do
def strand_sort(args), do: strand_sort(args, [])
def strand_sort(args), do: strand_sort(args, [])
Line 410: Line 410:
end
end


IO.inspect Sort.strand_sort [7, 17, 6, 20, 20, 12, 1, 1, 9]</lang>
IO.inspect Sort.strand_sort [7, 17, 6, 20, 20, 12, 1, 1, 9]</syntaxhighlight>


{{out}}
{{out}}
Line 418: Line 418:


=={{header|Euphoria}}==
=={{header|Euphoria}}==
<lang euphoria>function merge(sequence left, sequence right)
<syntaxhighlight lang="euphoria">function merge(sequence left, sequence right)
sequence result
sequence result
result = {}
result = {}
Line 460: Line 460:
? s
? s
puts(1,"After: ")
puts(1,"After: ")
? strand_sort(s)</lang>
? strand_sort(s)</syntaxhighlight>


Output:
Output:
Line 467: Line 467:


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 559: Line 559:
}
}
return
return
}</lang>
}</syntaxhighlight>
Output:
Output:
<pre>
<pre>
Line 568: Line 568:
=={{header|Haskell}}==
=={{header|Haskell}}==


<lang haskell>-- Same merge as in Merge Sort
<syntaxhighlight lang="haskell">-- Same merge as in Merge Sort
merge :: (Ord a) => [a] -> [a] -> [a]
merge :: (Ord a) => [a] -> [a] -> [a]
merge [] ys = ys
merge [] ys = ys
Line 583: Line 583:
extractStrand x (x1 : xs)
extractStrand x (x1 : xs)
| x <= x1 = let (strand, rest) = extractStrand x1 xs in (x : strand, rest)
| x <= x1 = let (strand, rest) = extractStrand x1 xs in (x : strand, rest)
| otherwise = let (strand, rest) = extractStrand x xs in (strand, x1 : rest)</lang>
| otherwise = let (strand, rest) = extractStrand x xs in (strand, x1 : rest)</syntaxhighlight>


=={{header|J}}==
=={{header|J}}==
Line 589: Line 589:
Using <code>merge</code> defined at [[Sorting algorithms/Merge sort#J]]:
Using <code>merge</code> defined at [[Sorting algorithms/Merge sort#J]]:


<lang j>strandSort=: (#~ merge $:^:(0<#)@(#~ -.)) (= >./\)</lang>
<syntaxhighlight lang="j">strandSort=: (#~ merge $:^:(0<#)@(#~ -.)) (= >./\)</syntaxhighlight>


Example use:
Example use:


<lang j> strandSort 3 1 5 4 2
<syntaxhighlight lang="j"> strandSort 3 1 5 4 2
1 2 3 4 5</lang>
1 2 3 4 5</syntaxhighlight>


Note: the order in which this J implementation 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.
Note: the order in which this J implementation 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.
Line 600: Line 600:
Also note that the individual strands can be seen by using <code>;</code> instead of <code>merge</code>.
Also note that the individual strands can be seen by using <code>;</code> instead of <code>merge</code>.


<lang j> ((#~ ; $:^:(0<#)@(#~ -.)) (= >./\)) 3 1 5 4 2
<syntaxhighlight lang="j"> ((#~ ; $:^:(0<#)@(#~ -.)) (= >./\)) 3 1 5 4 2
┌───┬───┬─┬┐
┌───┬───┬─┬┐
│3 5│1 4│2││
│3 5│1 4│2││
Line 607: Line 607:
┌─────────┬─────┬┐
┌─────────┬─────┬┐
│3 3 4 5 6│1 2 3││
│3 3 4 5 6│1 2 3││
└─────────┴─────┴┘</lang>
└─────────┴─────┴┘</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==
{{works with|Java|1.6+}}
{{works with|Java|1.6+}}
<lang java5>import java.util.Arrays;
<syntaxhighlight lang="java5">import java.util.Arrays;
import java.util.LinkedList;
import java.util.LinkedList;


Line 656: Line 656:
System.out.println(strandSort(new LinkedList<Integer>(Arrays.asList(3,3,1,2,4,3,5,6))));
System.out.println(strandSort(new LinkedList<Integer>(Arrays.asList(3,3,1,2,4,3,5,6))));
}
}
}</lang>
}</syntaxhighlight>
Output:
Output:
<pre>[1, 2, 3, 4, 5]
<pre>[1, 2, 3, 4, 5]
Line 663: Line 663:


=={{header|jq}}==
=={{header|jq}}==
Most of the implementation is the "merge" function for merging two arrays. Notice that the helper function, strand, is defined here as an inner function.<lang jq># merge input array with array x by comparing the heads of the arrays
Most of the implementation is the "merge" function for merging two arrays. Notice that the helper function, strand, is defined here as an inner function.<syntaxhighlight lang="jq"># merge input array with array x by comparing the heads of the arrays
# in turn; # if both arrays are sorted, the result will be sorted:
# in turn; # if both arrays are sorted, the result will be sorted:
def merge(x):
def merge(x):
Line 701: Line 701:
| ($s[0] | merge( $s[1] | strand_sort))
| ($s[0] | merge( $s[1] | strand_sort))
end ;
end ;
</syntaxhighlight>
</lang>
Example:
Example:
[1,3,5,2,4,6] | strand_sort
[1,3,5,2,4,6] | strand_sort
Line 707: Line 707:
=={{header|Julia}}==
=={{header|Julia}}==
{{trans|Python}}
{{trans|Python}}
<lang julia>function mergelist(a, b)
<syntaxhighlight lang="julia">function mergelist(a, b)
out = Vector{Int}()
out = Vector{Int}()
while !isempty(a) && !isempty(b)
while !isempty(a) && !isempty(b)
Line 736: Line 736:
println(strandsort([1, 6, 3, 2, 1, 7, 5, 3]))
println(strandsort([1, 6, 3, 2, 1, 7, 5, 3]))
</lang>{{output}}<pre>
</syntaxhighlight>{{output}}<pre>
[1, 1, 2, 3, 3, 5, 6, 7]
[1, 1, 2, 3, 3, 5, 6, 7]
</pre>
</pre>
Line 742: Line 742:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|D}}
{{trans|D}}
<lang scala>// version 1.1.2
<syntaxhighlight lang="scala">// version 1.1.2


fun <T : Comparable<T>> strandSort(l: List<T>): List<T> {
fun <T : Comparable<T>> strandSort(l: List<T>): List<T> {
Line 783: Line 783:
val l = listOf(-2, 0, -2, 5, 5, 3, -1, -3, 5, 5, 0, 2, -4, 4, 2)
val l = listOf(-2, 0, -2, 5, 5, 3, -1, -3, 5, 5, 0, 2, -4, 4, 2)
println(strandSort(l))
println(strandSort(l))
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 791: Line 791:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>StrandSort[ input_ ] := Module[ {results = {}, A = input},
<syntaxhighlight lang="mathematica">StrandSort[ input_ ] := Module[ {results = {}, A = input},
While[Length@A > 0,
While[Length@A > 0,
sublist = {A[[1]]}; A = A[[2;;All]];
sublist = {A[[1]]}; A = A[[2;;All]];
Line 799: Line 799:
results = #[[Ordering@#]]&@Join[sublist, results];];
results = #[[Ordering@#]]&@Join[sublist, results];];
results ]
results ]
StrandSort[{2, 3, 7, 5, 1, 4, 7}]</lang>
StrandSort[{2, 3, 7, 5, 1, 4, 7}]</syntaxhighlight>
{{out}}
{{out}}
<pre>{1, 2, 3, 4, 5, 7, 7}</pre>
<pre>{1, 2, 3, 4, 5, 7, 7}</pre>


=={{header|MAXScript}}==
=={{header|MAXScript}}==
<lang MAXScript>fn strandSort arr =
<syntaxhighlight lang="maxscript">fn strandSort arr =
(
(
arr = deepcopy arr
arr = deepcopy arr
Line 828: Line 828:
return results
return results


)</lang>
)</syntaxhighlight>
Output:
Output:
<syntaxhighlight lang="maxscript">
<lang MAXScript>
a = for i in 1 to 20 collect random 1 40
a = for i in 1 to 20 collect random 1 40
#(19, 26, 14, 31, 11, 33, 2, 14, 32, 28, 12, 38, 2, 37, 27, 18, 31, 24, 39, 28)
#(19, 26, 14, 31, 11, 33, 2, 14, 32, 28, 12, 38, 2, 37, 27, 18, 31, 24, 39, 28)
strandSort a
strandSort a
#(2, 2, 11, 12, 14, 14, 18, 19, 24, 26, 27, 28, 28, 31, 31, 32, 33, 37, 38, 39)
#(2, 2, 11, 12, 14, 14, 18, 19, 24, 26, 27, 28, 28, 31, 31, 32, 33, 37, 38, 39)
</syntaxhighlight>
</lang>


=={{header|NetRexx}}==
=={{header|NetRexx}}==
<lang NetRexx>/* NetRexx */
<syntaxhighlight lang="netrexx">/* NetRexx */
options replace format comments java crossref savelog symbols binary
options replace format comments java crossref savelog symbols binary


Line 911: Line 911:


return result
return result
</syntaxhighlight>
</lang>
;Output
;Output
<pre>
<pre>
Line 934: Line 934:


=={{header|Nim}}==
=={{header|Nim}}==
<lang nim>proc mergeList[T](a, b: var seq[T]): seq[T] =
<syntaxhighlight lang="nim">proc mergeList[T](a, b: var seq[T]): seq[T] =
result = @[]
result = @[]
while a.len > 0 and b.len > 0:
while a.len > 0 and b.len > 0:
Line 965: Line 965:


var a = @[1, 6, 3, 2, 1, 7, 5, 3]
var a = @[1, 6, 3, 2, 1, 7, 5, 3]
echo a.strandSort</lang>
echo a.strandSort</syntaxhighlight>
Output:
Output:
<pre>@[1, 1, 2, 3, 3, 5, 6, 7]</pre>
<pre>@[1, 1, 2, 3, 3, 5, 6, 7]</pre>
Line 971: Line 971:
=={{header|OCaml}}==
=={{header|OCaml}}==
{{trans|Haskell}}
{{trans|Haskell}}
<lang ocaml>let rec strand_sort (cmp : 'a -> 'a -> int) : 'a list -> 'a list = function
<syntaxhighlight lang="ocaml">let rec strand_sort (cmp : 'a -> 'a -> int) : 'a list -> 'a list = function
[] -> []
[] -> []
| x::xs ->
| x::xs ->
Line 982: Line 982:
in
in
let strand, rest = extract_strand x xs in
let strand, rest = extract_strand x xs in
List.merge cmp strand (strand_sort cmp rest)</lang>
List.merge cmp strand (strand_sort cmp rest)</syntaxhighlight>
usage
usage
<pre>
<pre>
Line 990: Line 990:


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
<lang parigp>strandSort(v)={
<syntaxhighlight lang="parigp">strandSort(v)={
my(sorted=[],unsorted=v,remaining,working);
my(sorted=[],unsorted=v,remaining,working);
while(#unsorted,
while(#unsorted,
Line 1,019: Line 1,019:
);
);
ret
ret
};</lang>
};</syntaxhighlight>


=={{header|Pascal}}==
=={{header|Pascal}}==
<lang Pascal>program StrandSortDemo;
<syntaxhighlight lang="pascal">program StrandSortDemo;
type
type
Line 1,100: Line 1,100:
end;
end;
writeln;
writeln;
end.</lang>
end.</syntaxhighlight>


=={{header|Perl}}==
=={{header|Perl}}==
<lang Perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use feature 'say';
use feature 'say';
Line 1,138: Line 1,138:
say "Before @a";
say "Before @a";
@a = strand_sort(@a);
@a = strand_sort(@a);
say "After @a";</lang>
say "After @a";</syntaxhighlight>


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 1,180: Line 1,180:
<span style="color: #0000FF;">?</span><span style="color: #000000;">strand_sort</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shuffle</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)))</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">strand_sort</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shuffle</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)))</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,189: Line 1,189:
{{trans|D}}
{{trans|D}}
{{works with|PHP 5.3.0+}}
{{works with|PHP 5.3.0+}}
<lang php>$lst = new SplDoublyLinkedList();
<syntaxhighlight lang="php">$lst = new SplDoublyLinkedList();
foreach (array(1,20,64,72,48,75,96,55,42,74) as $v)
foreach (array(1,20,64,72,48,75,96,55,42,74) as $v)
$lst->push($v);
$lst->push($v);
Line 1,226: Line 1,226:
foreach ($right as $v) $res->push($v);
foreach ($right as $v) $res->push($v);
return $res;
return $res;
}</lang>
}</syntaxhighlight>
<pre>1 20 42 48 55 64 72 74 75 96</pre>
<pre>1 20 42 48 55 64 72 74 75 96</pre>


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(de strandSort (Lst)
<syntaxhighlight lang="picolisp">(de strandSort (Lst)
(let Res NIL # Result list
(let Res NIL # Result list
(while Lst
(while Lst
Line 1,250: Line 1,250:
(pop 'Res)
(pop 'Res)
(fifo 'Sub) ) ) ) ) ) ) )
(fifo 'Sub) ) ) ) ) ) ) )
Res ) )</lang>
Res ) )</syntaxhighlight>
Test:
Test:
<pre>: (strandSort (3 1 5 4 2))
<pre>: (strandSort (3 1 5 4 2))
Line 1,259: Line 1,259:


=={{header|PL/I}}==
=={{header|PL/I}}==
<lang PL/I>strand: procedure options (main); /* 27 Oct. 2012 */
<syntaxhighlight lang="pl/i">strand: procedure options (main); /* 27 Oct. 2012 */
declare A(100) fixed, used(100) bit (1), sorted fixed controlled;
declare A(100) fixed, used(100) bit (1), sorted fixed controlled;
declare (temp, work) fixed controlled;
declare (temp, work) fixed controlled;
Line 1,349: Line 1,349:
end move;
end move;


end strand;</lang>
end strand;</syntaxhighlight>
Generated data:
Generated data:
<pre>
<pre>
Line 1,365: Line 1,365:


=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>Procedure strandSort(List a())
<syntaxhighlight lang="purebasic">Procedure strandSort(List a())
Protected NewList subList()
Protected NewList subList()
Protected NewList results()
Protected NewList results()
Line 1,446: Line 1,446:
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
CloseConsole()
EndIf</lang>
EndIf</syntaxhighlight>
Sample output:
Sample output:
<pre>List 1:
<pre>List 1:
Line 1,461: Line 1,461:


=={{header|Python}}==
=={{header|Python}}==
<lang Python>def merge_list(a, b):
<syntaxhighlight lang="python">def merge_list(a, b):
out = []
out = []
while len(a) and len(b):
while len(a) and len(b):
Line 1,487: Line 1,487:
return out
return out


print strand_sort([1, 6, 3, 2, 1, 7, 5, 3])</lang>
print strand_sort([1, 6, 3, 2, 1, 7, 5, 3])</syntaxhighlight>
Output:<lang>[1, 1, 2, 3, 3, 5, 6, 7]</lang>
Output:<syntaxhighlight lang="text">[1, 1, 2, 3, 3, 5, 6, 7]</syntaxhighlight>


=={{header|Racket}}==
=={{header|Racket}}==
<lang racket>
<syntaxhighlight lang="racket">
#lang racket
#lang racket
(require mzlib/list)
(require mzlib/list)
Line 1,509: Line 1,509:


(strand-sort (build-list 10 (λ(_) (random 15))))
(strand-sort (build-list 10 (λ(_) (random 15))))
</syntaxhighlight>
</lang>


=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
{{Works with|Rakudo|2018.04.01}}
{{Works with|Rakudo|2018.04.01}}
<lang perl6>sub infix:<M> (@x-in, @y-in) {
<syntaxhighlight lang="raku" line>sub infix:<M> (@x-in, @y-in) {
my @x = | @x-in;
my @x = | @x-in;
my @y = | @y-in;
my @y = | @y-in;
Line 1,549: Line 1,549:
say "Before {@a}";
say "Before {@a}";
@a = strand_sort(@a);
@a = strand_sort(@a);
say "After {@a}";</lang>
say "After {@a}";</syntaxhighlight>
{{out}}
{{out}}
<pre>Before 1 20 64 72 48 75 96 55 42 74
<pre>Before 1 20 64 72 48 75 96 55 42 74
Line 1,561: Line 1,561:


It can handle integers, floating point numbers, exponentiated numbers, and/or character strings.
It can handle integers, floating point numbers, exponentiated numbers, and/or character strings.
<lang rexx>/*REXX program sorts a random list of words (or numbers) using the strand sort algorithm*/
<syntaxhighlight lang="rexx">/*REXX program sorts a random list of words (or numbers) using the strand sort algorithm*/
parse arg size minv maxv old /*obtain optional arguments from the CL*/
parse arg size minv maxv old /*obtain optional arguments from the CL*/
if size=='' | size=="," then size=20 /*Not specified? Then use the default.*/
if size=='' | size=="," then size=20 /*Not specified? Then use the default.*/
Line 1,591: Line 1,591:
#=1+(word(a.1,1) >= word(a.2,1)); p=p word(a.#,1); a.#=subword(a.#,2)
#=1+(word(a.1,1) >= word(a.2,1)); p=p word(a.#,1); a.#=subword(a.#,2)
end /*forever*/
end /*forever*/
return space(p a.1 a.2)</lang>
return space(p a.1 a.2)</syntaxhighlight>
'''output''' &nbsp; when using the input of: &nbsp; <tt> 25 -9 30 1000 2000 3000 </tt>
'''output''' &nbsp; when using the input of: &nbsp; <tt> 25 -9 30 1000 2000 3000 </tt>
<pre>
<pre>
Line 1,615: Line 1,615:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
# Project : Sorting algorithms/Strand sort
# Project : Sorting algorithms/Strand sort


Line 1,660: Line 1,660:
svect = left(svect, len(svect) - 1)
svect = left(svect, len(svect) - 1)
see svect + nl
see svect + nl
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 1,670: Line 1,670:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>class Array
<syntaxhighlight lang="ruby">class Array
def strandsort
def strandsort
a = dup
a = dup
Line 1,692: Line 1,692:
end
end


p [1, 6, 3, 2, 1, 7, 5, 3].strandsort</lang>
p [1, 6, 3, 2, 1, 7, 5, 3].strandsort</syntaxhighlight>


{{out}}
{{out}}
Line 1,699: Line 1,699:
=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Perl}}
{{trans|Perl}}
<lang ruby>func merge(x, y) {
<syntaxhighlight lang="ruby">func merge(x, y) {
var out = [];
var out = [];
while (x && y) {
while (x && y) {
Line 1,734: Line 1,734:
var a = 10.of { 100.irand };
var a = 10.of { 100.irand };
say "Before: #{a}";
say "Before: #{a}";
say "After: #{strand_sort(a)}";</lang>
say "After: #{strand_sort(a)}";</syntaxhighlight>


{{out}}
{{out}}
Line 1,743: Line 1,743:


=={{header|Tcl}}==
=={{header|Tcl}}==
<lang tcl>proc merge {listVar toMerge} {
<syntaxhighlight lang="tcl">proc merge {listVar toMerge} {
upvar 1 $listVar v
upvar 1 $listVar v
set i [set j 0]
set i [set j 0]
Line 1,783: Line 1,783:
}
}


puts [strandSort {3 1 5 4 2}]</lang>
puts [strandSort {3 1 5 4 2}]</syntaxhighlight>


=={{header|Ursala}}==
=={{header|Ursala}}==


<lang Ursala>strand_sort "r" = # parameterized by a relational predicate "r"
<syntaxhighlight lang="ursala">strand_sort "r" = # parameterized by a relational predicate "r"


@NiX -+
@NiX -+
:-0 ~&B^?a\~&Y@a "r"?abh/~&alh2faltPrXPRC ~&arh2falrtPXPRC,
:-0 ~&B^?a\~&Y@a "r"?abh/~&alh2faltPrXPRC ~&arh2falrtPXPRC,
~&r->l ^|rlPlCrrPX/~& @hNCNXtX ~&r->lbx "r"?rllPXh/~&llPrhPlrPCXrtPX ~&rhPllPClrPXrtPX+-</lang>
~&r->l ^|rlPlCrrPX/~& @hNCNXtX ~&r->lbx "r"?rllPXh/~&llPrhPlrPCXrtPX ~&rhPllPClrPXrtPX+-</syntaxhighlight>
demonstration code:<lang Ursala>#cast %nL
demonstration code:<syntaxhighlight lang="ursala">#cast %nL


x = (strand_sort nat-nleq) <3,1,5,4,2></lang>output:<pre><1,2,3,4,5></pre>
x = (strand_sort nat-nleq) <3,1,5,4,2></syntaxhighlight>output:<pre><1,2,3,4,5></pre>


=={{header|Wren}}==
=={{header|Wren}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang ecmascript>var merge = Fn.new { |left, right|
<syntaxhighlight lang="ecmascript">var merge = Fn.new { |left, right|
var res = []
var res = []
while (!left.isEmpty && !right.isEmpty) {
while (!left.isEmpty && !right.isEmpty) {
Line 1,837: Line 1,837:
System.print("Unsorted: %(a)")
System.print("Unsorted: %(a)")
a = strandSort.call(a)
a = strandSort.call(a)
System.print("Sorted : %(a)")</lang>
System.print("Sorted : %(a)")</syntaxhighlight>


{{out}}
{{out}}
Line 1,846: Line 1,846:


=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>fcn strandSort(A){ //--> new list, A is cleared, should add A=A.copy()
<syntaxhighlight lang="zkl">fcn strandSort(A){ //--> new list, A is cleared, should add A=A.copy()
sublist:=List.createLong(A.len()); results:=List.createLong(A.len());
sublist:=List.createLong(A.len()); results:=List.createLong(A.len());
while(A){
while(A){
Line 1,856: Line 1,856:
}
}
results
results
}</lang>
}</syntaxhighlight>
The createLong list method creates a new list with pre-allocated space
The createLong list method creates a new list with pre-allocated space
<lang zkl>strandSort(L(3,1,5,4,2)).println();
<syntaxhighlight lang="zkl">strandSort(L(3,1,5,4,2)).println();
strandSort("azbfe".split("")).println();</lang>
strandSort("azbfe".split("")).println();</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>