●高速ソートのサブルーチン作成に成功しました
レコードの中に元になるリストと、ソートされた要素を収める場所を作り、そのレコードを参照形式にしてハンドラに引き渡すことによって、成功しました。
5000項目で16秒、1000項目で1秒を切りました。前回の実験結果よりもやや速くなっています。たぶん、行の最後に※印を付けている部分の処理が若干早くなったのだと思います。
“リスト参照にかかる時間-3”での実験結果から考えると1000項目で1秒を切るというのは、驚くべきパフォーマンスです。ただし、項目数が増えるとパフォーマンスがかなり落ちてきます。この原因は、やはり※印を付けている部分の処理の問題だろうと思います。
--ランダムな数値からなるリストを作成
set N to {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
set numList to {}
set theList to a reference to numList
repeat 1000 times
set end of theList to (some item of N) * 100 + (some item of N) * 10 + (some item of N)
end repeat
set T to current date --時間計測開始
repeat 10 times --正確な時間測定のため10回繰り返す
set sortRec to {sortList:{}, oldList:{}} --あらかじめもとのリストと、ソート後のリストを収めるレコードを作成
set sortRecRef to a reference to sortRec --そのレコードを参照形式に変換
set oldList of sortRec to contents of theList --元となるリストを oldList of sortRec にセット
sort_list(sortRecRef)
end repeat
(current date) - T --時間計測終了
--sortRec
--以下がソートのハンドラ(サブルーチン)
on sort_list(sortRecRef)
set {end of sortList of sortRecRef, i} to {item 1 of oldList of sortRecRef, 2}
repeat (count oldList of sortRecRef) - 1 times
set {aItem, A, Z} to {item i of oldList of sortRecRef, 1, i}
repeat until A = Z
if item ((A + Z) div 2) of sortList of sortRecRef > aItem then
set Z to (A + Z) div 2
else
set A to (A + Z) div 2 + 1
end if
end repeat
if A = 1 then
set beginning of sortList of sortRecRef to aItem
else if A = i then
set end of sortList of sortRecRef to aItem
else
set sortList of sortRecRef to items 1 thru (A - 1) of sortList of sortRecRef & {aItem} & (items A thru -1 of sortList of sortRecRef) --※
end if
set i to i + 1
end repeat
end sort_list
結果 8