Sorting algorithms/Shell sort: Difference between revisions

m
Line 445:
=={{header|AppleScript}}==
 
<lang applescript>(*-- In-place Shell sort.
-- Algorithm: Donald Shell, 1959.
on ShellSort(theList, l, r) -- Sort items l thru r of theList.
*)
set listLength to (count theList)
 
if (listLength < 2) then return
-- Sort range l thru r of a list, in place.
-- Convert negative and/or transposed range indices.
on ShellSort(theList, l, r)
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}
-- The list as a script property to allow faster references to its items.
script o
property lst : theList
end script
set listLengthstepSize to (countr theList- l + 1) div 2
ifrepeat while (listLengthstepSize > 10) then
repeat with ji from (il -+ stepSize) to l by -stepSizer
-- Convert negative and/or transposed range indices.
if (l < 0) then set lcurrentValue to listLength +o's llst's +item 1i
if (r < 0) thenrepeat setwith rj tofrom listLength(i +- stepSize) to rl +by 1-stepSize
if (l > r) then set {l, r} set thisValue to {r,o's lst's item l}j
if (currentValuethisValue <> thisValuecurrentValue) then
set o's setlst's item (j + stepSize) of o's lst to thisValue
-- Do the sort.
set stepSize to (r - l + 1) div 2else
set j to j + stepSize
repeat while (stepSize > 0)
repeat with i from (l + stepSize) to rexit repeat
setend currentValue to item i of o's lstif
repeat with j from (i - stepSize) to l by -stepSize
set thisValue to item j of o's lst
if (currentValue < thisValue) then
set item (j + stepSize) of o's lst to thisValue
else
set j to j + stepSize
exit repeat
end if
end repeat
if (j < i) then set item j of o's lst to currentValue
end repeat
set stepSize toif (stepSizej /< 2.2i) asthen set o's lst's item j to integercurrentValue
end repeat
repeatset whilestepSize to (stepSize >/ 02.2) as integer
end if
end repeat
return -- nothing. The input list has been sorted in place.
end ShellSort
property sort : ShellSort
 
-- Test codeDemo:
local aList
set aList to {6056, 7344, 1172, 664, 693, 7726, 4161, 9772, 5952, 459, 6487, 1526, 9173, 10075, 2294, 8991, 7730, 5918, 5463, 6116}
sort(aList, 1, -1) -- Sort theitems entire1 listthru -1 of aList.
return aList</lang>
 
{{output}}
<lang applescript>{64, 119, 1516, 2218, 4126, 4526, 5430, 5944, 5952, 6056, 61, 6463, 6672, 7372, 7773, 7775, 8987, 91, 9793, 10094}</lang>
 
=={{header|ARM Assembly}}==
557

edits