2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,3 +1,9 @@
|
|||
{{Sorting Algorithm}}
|
||||
{{Wikipedia|Strand sort}}
|
||||
Implement the [[wp:Strand sort|Strand sort]]. This is a way of sorting numbers by extracting shorter sequences of already sorted numbers from an unsorted list.
|
||||
|
||||
<br>
|
||||
;Task:
|
||||
Implement the [[wp:Strand sort|Strand sort]].
|
||||
|
||||
This is a way of sorting numbers by extracting shorter sequences of already sorted numbers from an unsorted list.
|
||||
<br><br>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,14 @@
|
|||
defmodule Sort do
|
||||
def strand_sort(args), do: strand_sort(args, [])
|
||||
|
||||
defp strand_sort([], result), do: result
|
||||
defp strand_sort(a, result) do
|
||||
{_, sublist, b} = Enum.reduce(a, {hd(a),[],[]}, fn val,{v,l1,l2} ->
|
||||
if v <= val, do: {val, [val | l1], l2},
|
||||
else: {v, l1, [val | l2]}
|
||||
end)
|
||||
strand_sort(b, :lists.merge(Enum.reverse(sublist), result))
|
||||
end
|
||||
end
|
||||
|
||||
IO.inspect Sort.strand_sort [7, 17, 6, 20, 20, 12, 1, 1, 9]
|
||||
|
|
@ -1,32 +1,31 @@
|
|||
/*REXX pgm sorts a random list of words using the strand sort algorithm.*/
|
||||
parse arg size minv maxv,old /*get options from command line. */
|
||||
if size=='' then size=20 /*no size? Then use the default.*/
|
||||
if minv=='' then minv=0 /*no minV? " " " " */
|
||||
if maxv=='' then maxv=size /*no maxV? " " " " */
|
||||
do i=1 for size /*generate random # list*/
|
||||
old=old random(0,maxv-minv)+minv
|
||||
end /*i*/
|
||||
old=space(old) /*remove any extraneous blanks. */
|
||||
say center('unsorted list',length(old),"─"); say old; say
|
||||
new=strand_sort(old) /*sort the list of random numbers*/
|
||||
say center('sorted list' ,length(new),"─"); say new
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────STRAND_SORT subroutine──────────────*/
|
||||
strand_sort: procedure; parse arg x; y=
|
||||
do while words(x)\==0; w=words(x)
|
||||
do j=1 for w-1 /*any number | word out of order?*/
|
||||
if word(x,j)>word(x,j+1) then do; w=j; leave; end
|
||||
end /*j*/
|
||||
y=merge(y,subword(x,1,w)); x=subword(x,w+1)
|
||||
end /*while*/
|
||||
return y
|
||||
/*──────────────────────────────────MERGE subroutine────────────────────*/
|
||||
merge: procedure; parse arg a.1,a.2; p=
|
||||
do forever /*keep at it while 2 lists exist.*/
|
||||
do i=1 for 2; w.i=words(a.i); end /*find number of entries in lists*/
|
||||
if w.1*w.2==0 then leave /*if any list is empty, then stop*/
|
||||
if word(a.1,w.1) <= word(a.2,1) then leave /*lists are now sorted?*/
|
||||
if word(a.2,w.2) <= word(a.1,1) then return space(p a.2 a.1)
|
||||
#=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)
|
||||
/*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*/
|
||||
if size=='' | size=="," then size=20 /*Not specified? Then use the default.*/
|
||||
if minv=='' | minv=="," then minv= 0 /*Not specified? Then use the default.*/
|
||||
if maxv=='' | maxv=="," then maxv=size /*Not specified? Then use the default.*/
|
||||
do i=1 for size /*generate a list of random numbers. */
|
||||
old=old random(0,maxv-minv)+minv /*append a random number to a list. */
|
||||
end /*i*/
|
||||
old=space(old) /*elide extraneous blanks from the list*/
|
||||
say center('unsorted list', length(old), "─"); say old
|
||||
new=strand_sort(old) /*sort the list of the random numbers. */
|
||||
say; say center('sorted list' , length(new), "─"); say new
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
strand_sort: procedure; parse arg x; y=
|
||||
do while words(x)\==0; w=words(x)
|
||||
do j=1 for w-1 /*anything out of order?*/
|
||||
if word(x,j)>word(x,j+1) then do; w=j; leave; end
|
||||
end /*j*/
|
||||
y=merge(y,subword(x,1,w)); x=subword(x,w+1)
|
||||
end /*while*/
|
||||
return y
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
merge: procedure; parse arg a.1,a.2; p=
|
||||
do forever; w1=words(a.1); w2=words(a.2) /*do while 2 lists exist*/
|
||||
if w1==0 | if w2==0 then leave /*Any list empty? Stop.*/
|
||||
if word(a.1,w1) <= word(a.2,1) then leave /*lists are now sorted? */
|
||||
if word(a.2,w2) <= word(a.1,1) then return space(p a.2 a.1)
|
||||
#=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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue