Sorting algorithms/Insertion sort: Difference between revisions
m
→{{header|AppleScript}}: Minor modification to take range parameters. Output shown in separate box.
(add task to aarch64 assembly raspberry pi) |
m (→{{header|AppleScript}}: Minor modification to take range parameters. Output shown in separate box.) |
||
Line 448:
=={{header|AppleScript}}==
<lang applescript>
script o
property lst : theList
Line 455 ⟶ 456:
set listLength to (count theList)
if (listLength > 1) then
-- Convert negative and/or transposed range indices.
-- Presort the first two values to set up a minor optimisation whereby the most recent instance of the highest value so far▼
if (l < 0) then set l to listLength + l + 1
-- is only put back into the list when a value greater than or equal to it turns up or the end of the list is reached. ▼
if (r < 0) then set
▲ --
▲ --
set highestSoFar to item l of o's lst
set currentValue to item (l + 1) of o's lst
if (highestSoFar > currentValue) then
set item
else
set highestSoFar to currentValue
end if
-- Work through the rest of the range, rotating values back into the sorted group as necessary.
repeat with i from
set currentValue to item i of o's lst
if (currentValue < highestSoFar) then
repeat with j from (i - 2) to
set thisValue to item j of o's lst
if (currentValue < thisValue) then
Line 484 ⟶ 490:
end repeat
-- At the end, ensure that the highest value goes back into the list.
set o's lst's item
end if
return -- nothing.
end insertionSort
property sort : insertionSort
-- Test code:
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.
▲insertionSort(aList)
return aList</lang>
--> {6, 11, 15, 22, 41, 45, 54, 59, 59, 60, 61, 64, 66, 73, 77, 77, 89, 91, 97, 100}</lang>▼
{{output}}
▲
=={{header|ARM Assembly}}==
|