Sorting algorithms/Strand sort: Difference between revisions
m
syntax highlighting fixup automation
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 14:
{{trans|Python}}
<
[Int] out
L !a.empty & !b.empty
Line 41:
R out
print(strand_sort(&[1, 6, 3, 2, 1, 7, 5, 3]))</
{{out}}
Line 50:
=={{header|AutoHotkey}}==
{{works with|AutoHotkey_L}}
<
(
-2 0 -2 5 5 3 -1 -3 5 5 0 2 -4 4 2
Line 76:
string := list, list = "", index := 0
}
esc::ExitApp</
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</
=={{header|C}}==
Strand sort using singly linked list. C99, compiled with <code>gcc -std=c99</code>
<
typedef struct node_t *node, node_t;
Line 159:
return 0;
}</
after sort: -4 -3 -2 -2 -1 0 0 2 2 3 4 5 5 5 5</
=={{header|C++}}==
<
template <typename T>
Line 184:
}
return result;
}</
=={{header|Clojure}}==
<
(defn merge-join
Line 222:
(strand-sort [1, 6, 3, 2, 1, 7, 5, 3])
;;=> (1 1 2 3 3 5 6 7)
</syntaxhighlight>
=={{header|CMake}}==
Only for lists of integers.
<
function(strand_sort var)
# Strand sort moves elements from _ARGN_ to _answer_.
Line 287:
set("${var}" ${answer} PARENT_SCOPE)
endfunction(strand_sort)</
<
message(STATUS "${result}") # -- 11;11;22;22;33;33;44;44;55;55</
=={{header|Common Lisp}}==
<
(if l
(let* ((l (reverse l))
Line 303:
(let ((r (loop repeat 15 collect (random 10))))
(print r)
(print (strand-sort r #'<)))</
(0 0 1 3 3 4 5 5 6 6 6 7 7 8 8)</
=={{header|D}}==
=== Using doubly linked lists ===
<
DList!T strandSort(T)(DList!T list) {
Line 352:
foreach (e; strandSort(lst))
write(e, " ");
}</
{{out}}
<pre>-4 -3 -2 -2 -1 0 0 2 2 3 4 5 5 5 5 </pre>
=== Faster version using slices ===
<
T[] strandSort(T)(const(T)[] list) pure nothrow {
Line 391:
const arr = [-2, 0, -2, 5, 5, 3, -1, -3, 5, 5, 0, 2, -4, 4, 2];
arr.strandSort.writeln;
}</
{{out}}
<pre>[-4, -3, -2, -2, -1, 0, 0, 2, 2, 3, 4, 5, 5, 5, 5]</pre>
Line 397:
=={{header|Elixir}}==
{{trans|Ruby}}
<
def strand_sort(args), do: strand_sort(args, [])
Line 410:
end
IO.inspect Sort.strand_sort [7, 17, 6, 20, 20, 12, 1, 1, 9]</
{{out}}
Line 418:
=={{header|Euphoria}}==
<
sequence result
result = {}
Line 460:
? s
puts(1,"After: ")
? strand_sort(s)</
Output:
Line 467:
=={{header|Go}}==
<
import "fmt"
Line 559:
}
return
}</
Output:
<pre>
Line 568:
=={{header|Haskell}}==
<
merge :: (Ord a) => [a] -> [a] -> [a]
merge [] ys = ys
Line 583:
extractStrand x (x1 : xs)
| x <= x1 = let (strand, rest) = extractStrand x1 xs in (x : strand, rest)
| otherwise = let (strand, rest) = extractStrand x xs in (strand, x1 : rest)</
=={{header|J}}==
Line 589:
Using <code>merge</code> defined at [[Sorting algorithms/Merge sort#J]]:
<
Example use:
<
1 2 3 4 5</
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:
Also note that the individual strands can be seen by using <code>;</code> instead of <code>merge</code>.
<
┌───┬───┬─┬┐
│3 5│1 4│2││
Line 607:
┌─────────┬─────┬┐
│3 3 4 5 6│1 2 3││
└─────────┴─────┴┘</
=={{header|Java}}==
{{works with|Java|1.6+}}
<
import java.util.LinkedList;
Line 656:
System.out.println(strandSort(new LinkedList<Integer>(Arrays.asList(3,3,1,2,4,3,5,6))));
}
}</
Output:
<pre>[1, 2, 3, 4, 5]
Line 663:
=={{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.<
# in turn; # if both arrays are sorted, the result will be sorted:
def merge(x):
Line 701:
| ($s[0] | merge( $s[1] | strand_sort))
end ;
</syntaxhighlight>
Example:
[1,3,5,2,4,6] | strand_sort
Line 707:
=={{header|Julia}}==
{{trans|Python}}
<
out = Vector{Int}()
while !isempty(a) && !isempty(b)
Line 736:
println(strandsort([1, 6, 3, 2, 1, 7, 5, 3]))
</
[1, 1, 2, 3, 3, 5, 6, 7]
</pre>
Line 742:
=={{header|Kotlin}}==
{{trans|D}}
<
fun <T : Comparable<T>> strandSort(l: List<T>): List<T> {
Line 783:
val l = listOf(-2, 0, -2, 5, 5, 3, -1, -3, 5, 5, 0, 2, -4, 4, 2)
println(strandSort(l))
}</
{{out}}
Line 791:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
While[Length@A > 0,
sublist = {A[[1]]}; A = A[[2;;All]];
Line 799:
results = #[[Ordering@#]]&@Join[sublist, results];];
results ]
StrandSort[{2, 3, 7, 5, 1, 4, 7}]</
{{out}}
<pre>{1, 2, 3, 4, 5, 7, 7}</pre>
=={{header|MAXScript}}==
<
(
arr = deepcopy arr
Line 828:
return results
)</
Output:
<syntaxhighlight lang="maxscript">
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)
strandSort a
#(2, 2, 11, 12, 14, 14, 18, 19, 24, 26, 27, 28, 28, 31, 31, 32, 33, 37, 38, 39)
</syntaxhighlight>
=={{header|NetRexx}}==
<
options replace format comments java crossref savelog symbols binary
Line 911:
return result
</syntaxhighlight>
;Output
<pre>
Line 934:
=={{header|Nim}}==
<
result = @[]
while a.len > 0 and b.len > 0:
Line 965:
var a = @[1, 6, 3, 2, 1, 7, 5, 3]
echo a.strandSort</
Output:
<pre>@[1, 1, 2, 3, 3, 5, 6, 7]</pre>
Line 971:
=={{header|OCaml}}==
{{trans|Haskell}}
<
[] -> []
| x::xs ->
Line 982:
in
let strand, rest = extract_strand x xs in
List.merge cmp strand (strand_sort cmp rest)</
usage
<pre>
Line 990:
=={{header|PARI/GP}}==
<
my(sorted=[],unsorted=v,remaining,working);
while(#unsorted,
Line 1,019:
);
ret
};</
=={{header|Pascal}}==
<
type
Line 1,100:
end;
writeln;
end.</
=={{header|Perl}}==
<
use warnings;
use feature 'say';
Line 1,138:
say "Before @a";
@a = strand_sort(@a);
say "After @a";</
=={{header|Phix}}==
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
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>
<!--</
{{out}}
<pre>
Line 1,189:
{{trans|D}}
{{works with|PHP 5.3.0+}}
<
foreach (array(1,20,64,72,48,75,96,55,42,74) as $v)
$lst->push($v);
Line 1,226:
foreach ($right as $v) $res->push($v);
return $res;
}</
<pre>1 20 42 48 55 64 72 74 75 96</pre>
=={{header|PicoLisp}}==
<
(let Res NIL # Result list
(while Lst
Line 1,250:
(pop 'Res)
(fifo 'Sub) ) ) ) ) ) ) )
Res ) )</
Test:
<pre>: (strandSort (3 1 5 4 2))
Line 1,259:
=={{header|PL/I}}==
<
declare A(100) fixed, used(100) bit (1), sorted fixed controlled;
declare (temp, work) fixed controlled;
Line 1,349:
end move;
end strand;</
Generated data:
<pre>
Line 1,365:
=={{header|PureBasic}}==
<
Protected NewList subList()
Protected NewList results()
Line 1,446:
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf</
Sample output:
<pre>List 1:
Line 1,461:
=={{header|Python}}==
<
out = []
while len(a) and len(b):
Line 1,487:
return out
print strand_sort([1, 6, 3, 2, 1, 7, 5, 3])</
Output:<syntaxhighlight lang="text">[1, 1, 2, 3, 3, 5, 6, 7]</
=={{header|Racket}}==
<
#lang racket
(require mzlib/list)
Line 1,509:
(strand-sort (build-list 10 (λ(_) (random 15))))
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
{{Works with|Rakudo|2018.04.01}}
<syntaxhighlight lang="raku"
my @x = | @x-in;
my @y = | @y-in;
Line 1,549:
say "Before {@a}";
@a = strand_sort(@a);
say "After {@a}";</
{{out}}
<pre>Before 1 20 64 72 48 75 96 55 42 74
Line 1,561:
It can handle integers, floating point numbers, exponentiated numbers, and/or character strings.
<
parse arg size minv maxv old /*obtain optional arguments from the CL*/
if size=='' | size=="," then size=20 /*Not specified? Then use the default.*/
Line 1,591:
#=1+(word(a.1,1) >= word(a.2,1)); p=p word(a.#,1); a.#=subword(a.#,2)
end /*forever*/
return space(p a.1 a.2)</
'''output''' when using the input of: <tt> 25 -9 30 1000 2000 3000 </tt>
<pre>
Line 1,615:
=={{header|Ring}}==
<
# Project : Sorting algorithms/Strand sort
Line 1,660:
svect = left(svect, len(svect) - 1)
see svect + nl
</syntaxhighlight>
Output:
<pre>
Line 1,670:
=={{header|Ruby}}==
<
def strandsort
a = dup
Line 1,692:
end
p [1, 6, 3, 2, 1, 7, 5, 3].strandsort</
{{out}}
Line 1,699:
=={{header|Sidef}}==
{{trans|Perl}}
<
var out = [];
while (x && y) {
Line 1,734:
var a = 10.of { 100.irand };
say "Before: #{a}";
say "After: #{strand_sort(a)}";</
{{out}}
Line 1,743:
=={{header|Tcl}}==
<
upvar 1 $listVar v
set i [set j 0]
Line 1,783:
}
puts [strandSort {3 1 5 4 2}]</
=={{header|Ursala}}==
<
@NiX -+
:-0 ~&B^?a\~&Y@a "r"?abh/~&alh2faltPrXPRC ~&arh2falrtPXPRC,
~&r->l ^|rlPlCrrPX/~& @hNCNXtX ~&r->lbx "r"?rllPXh/~&llPrhPlrPCXrtPX ~&rhPllPClrPXrtPX+-</
demonstration code:<
x = (strand_sort nat-nleq) <3,1,5,4,2></
=={{header|Wren}}==
{{trans|Kotlin}}
<
var res = []
while (!left.isEmpty && !right.isEmpty) {
Line 1,837:
System.print("Unsorted: %(a)")
a = strandSort.call(a)
System.print("Sorted : %(a)")</
{{out}}
Line 1,846:
=={{header|zkl}}==
<
sublist:=List.createLong(A.len()); results:=List.createLong(A.len());
while(A){
Line 1,856:
}
results
}</
The createLong list method creates a new list with pre-allocated space
<
strandSort("azbfe".split("")).println();</
{{out}}
<pre>
|