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>on-- insertionSort(theList)Sort range l thru r of a list, in place.
on insertionSort(aListtheList, l, r)
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 highestSoFarr to beginninglistLength + ofr o's+ lst1
setif currentValue(l to> itemr) 2then ofset {l, r} to o's{r, lstl}
-- Presort the first two values to setSet up a minor optimisation whereby the most recentlatest instance of the highest value so far isn't put back
-- isinto onlyor putre-fetched back intofrom the list whenuntil a value greater than or equal toeither it's turns upsuperseded or the end of the listsort is reached.
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 1l of o's lst to currentValue
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 3(l + 2) to listLengthr
set currentValue to item i of o's lst
if (currentValue < highestSoFar) then
repeat with j from (i - 2) to 1l by -1
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 listLengthr to highestSoFar
end if
return -- nothing. Either o's lst or theList can be returned if preferred. They and theThe input list arehas allbeen thesorted samein objectplace.
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}}
--><lang applescript>{6, 11, 15, 22, 41, 45, 54, 59, 59, 60, 61, 64, 66, 73, 77, 77, 89, 91, 97, 100}</lang>
 
=={{header|ARM Assembly}}==
557

edits