Jump to content

Sorting algorithms/Strand sort: Difference between revisions

GP
(added Ursala)
(GP)
Line 79:
[1, 2, 3, 3, 4, 5]
[1, 2, 3, 3, 3, 4, 5, 6]</pre>
 
=={{header|PARI/GP}}==
<lang parigp>strandSort(v)={
my(sorted=[],unsorted=v,remaining,working);
while(#unsorted,
remaining=working=List();
listput(working, unsorted[1]);
for(i=2,#unsorted,
if(unsorted[i]<working[#working],
listput(remaining, unsorted[i])
,
listput(working, unsorted[i])
)
);
unsorted=Vec(remaining);
sorted=merge(sorted, Vec(working))
);
sorted
};
merge(u,v)={
my(ret=vector(#u+#v),i=1,j=1);
for(k=1,#ret,
if(i<=#u & (j>#v | u[i]<v[j]),
ret[k]=u[i];
i++
,
ret[k]=v[j];
j++
)
);
ret
};</lang>
 
=={{header|PicoLisp}}==
Line 108 ⟶ 140:
: (strandSort (3 abc 1 (d e f) 5 T 4 NIL 2))
-> (NIL 1 2 3 4 5 abc (d e f) T)</pre>
 
=={{header|PureBasic}}==
<lang PureBasic>Procedure strandSort(List a())
Cookies help us deliver our services. By using our services, you agree to our use of cookies.