Sorting algorithms/Strand sort: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 14: | Line 14: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<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]))</ |
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}} |
||
< |
<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</ |
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</ |
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> |
||
< |
<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; |
||
}</ |
}</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</ |
after sort: -4 -3 -2 -2 -1 0 0 2 2 3 4 5 5 5 5</syntaxhighlight> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <list> |
||
template <typename T> |
template <typename T> |
||
Line 184: | Line 184: | ||
} |
} |
||
return result; |
return result; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<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. |
||
< |
<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)</ |
endfunction(strand_sort)</syntaxhighlight> |
||
< |
<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</ |
message(STATUS "${result}") # -- 11;11;22;22;33;33;44;44;55;55</syntaxhighlight> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<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 #'<)))</ |
(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)</ |
(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 === |
||
< |
<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, " "); |
||
}</ |
}</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 === |
||
< |
<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; |
||
}</ |
}</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}} |
||
< |
<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]</ |
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}}== |
||
< |
<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)</ |
? strand_sort(s)</syntaxhighlight> |
||
Output: |
Output: |
||
Line 467: | Line 467: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 559: | Line 559: | ||
} |
} |
||
return |
return |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 568: | Line 568: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<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)</ |
| 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]]: |
||
< |
<syntaxhighlight lang="j">strandSort=: (#~ merge $:^:(0<#)@(#~ -.)) (= >./\)</syntaxhighlight> |
||
Example use: |
Example use: |
||
< |
<syntaxhighlight lang="j"> strandSort 3 1 5 4 2 |
||
1 2 3 4 5</ |
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>. |
||
< |
<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││ |
||
└─────────┴─────┴┘</ |
└─────────┴─────┴┘</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{works with|Java|1.6+}} |
{{works with|Java|1.6+}} |
||
< |
<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)))); |
||
} |
} |
||
}</ |
}</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.< |
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}} |
||
< |
<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])) |
||
</ |
</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}} |
||
< |
<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)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 791: | Line 791: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<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}]</ |
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}}== |
||
< |
<syntaxhighlight lang="maxscript">fn strandSort arr = |
||
( |
( |
||
arr = deepcopy arr |
arr = deepcopy arr |
||
Line 828: | Line 828: | ||
return results |
return results |
||
)</ |
)</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}}== |
||
< |
<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}}== |
||
< |
<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</ |
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}} |
||
< |
<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)</ |
List.merge cmp strand (strand_sort cmp rest)</syntaxhighlight> |
||
usage |
usage |
||
<pre> |
<pre> |
||
Line 990: | Line 990: | ||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
< |
<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 |
||
};</ |
};</syntaxhighlight> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
< |
<syntaxhighlight lang="pascal">program StrandSortDemo; |
||
type |
type |
||
Line 1,100: | Line 1,100: | ||
end; |
end; |
||
writeln; |
writeln; |
||
end.</ |
end.</syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<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";</ |
say "After @a";</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<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> |
||
<!--</ |
<!--</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+}} |
||
< |
<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; |
||
}</ |
}</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}}== |
||
< |
<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 ) )</ |
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}}== |
||
< |
<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;</ |
end strand;</syntaxhighlight> |
||
Generated data: |
Generated data: |
||
<pre> |
<pre> |
||
Line 1,365: | Line 1,365: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<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</ |
EndIf</syntaxhighlight> |
||
Sample output: |
Sample output: |
||
<pre>List 1: |
<pre>List 1: |
||
Line 1,461: | Line 1,461: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<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])</ |
print strand_sort([1, 6, 3, 2, 1, 7, 5, 3])</syntaxhighlight> |
||
Output:<lang>[1, 1, 2, 3, 3, 5, 6, 7]</ |
Output:<syntaxhighlight lang="text">[1, 1, 2, 3, 3, 5, 6, 7]</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|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 |
<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}";</ |
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. |
||
< |
<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)</ |
return space(p a.1 a.2)</syntaxhighlight> |
||
'''output''' when using the input of: <tt> 25 -9 30 1000 2000 3000 </tt> |
'''output''' when using the input of: <tt> 25 -9 30 1000 2000 3000 </tt> |
||
<pre> |
<pre> |
||
Line 1,615: | Line 1,615: | ||
=={{header|Ring}}== |
=={{header|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}}== |
||
< |
<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</ |
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}} |
||
< |
<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)}";</ |
say "After: #{strand_sort(a)}";</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,743: | Line 1,743: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<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}]</ |
puts [strandSort {3 1 5 4 2}]</syntaxhighlight> |
||
=={{header|Ursala}}== |
=={{header|Ursala}}== |
||
< |
<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+-</ |
~&r->l ^|rlPlCrrPX/~& @hNCNXtX ~&r->lbx "r"?rllPXh/~&llPrhPlrPCXrtPX ~&rhPllPClrPXrtPX+-</syntaxhighlight> |
||
demonstration code:< |
demonstration code:<syntaxhighlight lang="ursala">#cast %nL |
||
x = (strand_sort nat-nleq) <3,1,5,4,2></ |
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}} |
||
< |
<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)")</ |
System.print("Sorted : %(a)")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,846: | Line 1,846: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<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 |
||
}</ |
}</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 |
||
< |
<syntaxhighlight lang="zkl">strandSort(L(3,1,5,4,2)).println(); |
||
strandSort("azbfe".split("")).println();</ |
strandSort("azbfe".split("")).println();</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |