Sorting algorithms/Insertion sort: Difference between revisions

Content added Content deleted
Line 470: Line 470:
=={{header|AppleScript}}==
=={{header|AppleScript}}==


<lang applescript>-- Sort range l thru r of a list, in place.
<lang applescript>-- In-place insertion sort
on insertionSort(theList, l, r)
on insertionSort(theList, l, r) -- Sort items l thru r of theList.
set listLength to (count theList)
if (listLength < 2) then return
-- Convert negative and/or transposed range indices.
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
script o
property lst : theList
property lst : theList
end script
end script
-- Set up a minor optimisation whereby the latest instance of the highest value so far isn't
set listLength to (count theList)
-- put back into the list until either it's superseded or the end of the sort is reached.
if (listLength > 1) then
set highestSoFar to o's lst's item l
-- Convert negative and/or transposed range indices.
if (l < 0) then set l to listLength + l + 1
set currentValue to o's lst's item (l + 1)
if (r < 0) then set r to listLength + r + 1
if (highestSoFar > currentValue) then
if (l > r) then set {l, r} to {r, l}
set o's lst's item l to currentValue
else
set highestSoFar to currentValue
-- Set up a minor optimisation whereby the latest instance of the highest value so far isn't put back
end if
-- into or re-fetched from the list until either it's superseded or the end of the sort is reached.
-- Work through the rest of the range, rotating values back into the sorted group as necessary.
set highestSoFar to item l of o's lst
set currentValue to item (l + 1) of o's lst
repeat with i from (l + 2) to r
set currentValue to o's lst's item i
if (highestSoFar > currentValue) then
if (highestSoFar > currentValue) then
set item l of o's lst to currentValue
repeat with j from (i - 2) to l by -1
set thisValue to o's lst's item j
if (thisValue > currentValue) then
set o's lst's item (j + 1) to thisValue
else
set j to j + 1
exit repeat
end if
end repeat
set o's lst's item j to currentValue
else
else
set o's lst's item (i - 1) to highestSoFar
set highestSoFar to currentValue
set highestSoFar to currentValue
end if
end if
end repeat
-- Work through the rest of the range, rotating values back into the sorted group as necessary.
set o's lst's item r to highestSoFar
repeat with i from (l + 2) to r
set currentValue to item i of o's lst
if (currentValue < highestSoFar) then
repeat with j from (i - 2) to l by -1
set thisValue to item j of o's lst
if (currentValue < thisValue) then
set item (j + 1) of o's lst to thisValue
else
set j to j + 1
exit repeat
end if
end repeat
set item j of o's lst to currentValue
else
set item (i - 1) of o's lst to highestSoFar
set highestSoFar to currentValue
end if
end repeat
-- At the end, ensure that the highest value goes back into the list.
set o's lst's item r to highestSoFar
end if
return -- nothing. The input list has been sorted in place.
return -- nothing.
end insertionSort
end insertionSort
property sort : insertionSort
property sort : insertionSort


-- Test code:
-- Demo:
local aList
set aList to {60, 73, 11, 66, 6, 77, 41, 97, 59, 45, 64, 15, 91, 100, 22, 89, 77, 59, 54, 61}
set aList to {60, 73, 11, 66, 6, 77, 41, 97, 59, 45, 64, 15, 91, 100, 22, 89, 77, 59, 54, 61}
sort(aList, 1, -1) -- Sort the entire list.
sort(aList, 1, -1) -- Sort items 1 thru -1 of aList.
return aList</lang>
return aList</lang>