Sorting algorithms/Strand sort: Difference between revisions

m
→‎{{header|AppleScript}}: Made into an in-place sort and more efficient.
(Added AppleScript.)
m (→‎{{header|AppleScript}}: Made into an in-place sort and more efficient.)
Line 50:
=={{header|AppleScript}}==
Strand sort seems to be essentially a merge sort with a particular way of setting up the initial blocks.
<syntaxhighlight lang="applescript">-- ReturnSort aitems deepl copythru r of theList with items l thru rin sortedplace, ascending.
on strandSort(theList, l, r)
-- ResolveDeal with negative and/or transposed range index parameters.
set listLength to (count theList)
if (l < 0) then set l to listLength + l + 1
if (r < 0) then set r to listLength + r + 1
if (l > r) then set {l, r} to {r, l}
if ((l < 1) or (r > listLength)) then ¬error "strandSort(): range index parameter(s) outside list range."
error "strandSort(): range index parameter(s) outside list range."
script o
property srcdest : missingtheList value-- Original list.
property destsrc : missingmy dest's items l thru r -- The items in the sort valuerange.
property ranges : {}
end script
-- Individually list-wrap the items in o's src to avoid having to
-- Arrange a copy of the list into "strands" of exisiting ascending order
-- andhard-code gettheir theactual strands'class in the rangesline withinmarked this** arrangementbelow.
copyrepeat theListwith i from 1 to o's(r - l + src1)
set o's destsrc's item i to {o's src's itemsitem i}
end repeat
-- Extract "strands" of existing order from the sort range items
-- and write the resulting runs over the range in the original list.
set i to l
repeat until (i > r)
set j to i
set jValjv to o's src's itembeginning's beginning -- The value in src's first jsublist.
repeatset witho's kdest's fromitem (j +to 1)jv to-- Store it in the next original-list rslot
set kVal to o's src's item k1 to missing value -- Replace the sublist with a placeholder.
-- Do the same with any later values that are sequentially greater or equal.
if (kVal < jVal) then
repeat with k from 2 to (count o's src)
set jValkv to o's src's item jk's beginning
if (kValkv < jValjv) then
else
set j to j + 1
set o's srcdest's item kj to o's src's item jkv
set o's src's item jjv to kValkv
set jValo's src's item k to kValmissing value
end if
end repeat
set o's ranges's end to {i, j} -- Note this strand's range in the list.
set o's src to o's src's lists -- Lose src's zapped sublists. **
set i to j + 1
end repeat
set rangeCountstrandCount to (count o's ranges)
if (rangeCountstrandCount = 1) then return o's src -- AlreadyThe input list was already in order.
-- MergeWork theout strandshow backmany andpasses forththe betweeniterative thismerge listwill take and anotherfrom this duplicate.whether
-- the auxiliary list has to be the source or the destination during the first pass.
set o's dest to o's src's items
-- The destination in the final pass has to be the original list.
repeat until (rangeCount = 1)
set {o's src, o's dest}passCount to {o's dest, o's src}0
repeat while (2 ^ setpassCount k< to lstrandCount)
repeatset with rr from 2passCount to rangeCountpassCount by+ 21
end repeat
if (passCount mod 2 = 0) then
set o's src to o's dest
set o's dest to o's dest's items
else
set o's src to o's dest's items
end if
-- Merge the strands.
repeat passCount times
set k to l -- Destination index.
repeat with rr from 2 to strandCount by 2 -- Per pair of ranges.
set {{i, ix}, {j, jx}} to o's ranges's items (rr - 1) thru rr
set o's ranges's item (rr - 1) to {i, jx}
set o's ranges's item rr to missing value
set iValiv to o's src's item i
set jValjv to o's src's item j
repeat until (k > jx)
if (iValiv > jValjv) then
set o's dest's item k to jValjv
setif (j to< j +jx) 1then
if ( set j >to j jx)+ then1
set iValjv to o's src's item ij
else
repeat with i from i to ix
set k to k + 1
set o's dest's item k to o's src's item i
end repeat
else
set jVal to o's src's item j
end if
else
set o's dest's item k to iValiv
setif (i to< i +ix) 1then
if ( set i >to i ix)+ then1
set iv to o's src's item i
else
repeat with k from j to jx
set o's dest's item k to o's src's item k
end repeat
else
set iVal to o's src's item i
end if
end if
Line 127 ⟶ 146:
end repeat
end repeat
if (rr < rangeCountstrandCount) then -- Odd range at the end of this pass?
set {i, ix} to o's ranges's end
repeat with k from i to ix
Line 135 ⟶ 154:
set o's ranges to o's ranges's lists
set rangeCountstrandCount to (rangeCountstrandCount + 1) div 2
set {o's src, o's dest} to {o's dest, o's src}
end repeat
return o's-- destnothing.
end strandSort
 
local lst
strandSort(set lst to {5, 1, 4, 37, 2, 0, 9, 6, -44, 3, 8, 7}, 1, -1)</syntaxhighlight>
strandSort(lst, 1, -1)
return lst</syntaxhighlight>
 
{{output}}
557

edits