ルーム情報 : "droom" (291件)
最近更新されたメモ一覧 :
docs
被リンクメモ一覧 : (1件)
[Subroutines]
[Home] [List] [Recent] [Orphan] [Help] [Edit] [Remove]

AppleScript : Subroutine - Sort

バブルソート

--結果はlist
--単なるバブルソートです。

on SortList(aList)
    set ItemList to (count items of aList)
    repeat
        set sTrue to 0
        repeat with i from 1 to (ItemList - 1)
            if (item i of aList > item (i + 1) of aList) then
                set Buf to item i of aList
                set item i of aList to item (i + 1) of aList
                set item (i + 1) of aList to Buf
            else
                set sTrue to sTrue + 1
            end if
        end repeat
        if sTrue = (ItemList - 1) then exit repeat
    end repeat
    return aList
end SortList

バブルソート (サブリスト用)

--結果はlist(サブリスト方式、いくつ目のリストアイテムを比較するか入力)

on sort_list_sub(s_list, item_No)
    set item_num to (count items of s_list)
    repeat
        set s_true to 0
        repeat with i from 1 to (item_num - 1)
            if (item item_No of item i of s_list > item item_No of item (i + 1) of s_list) then
                set buf_item to item i of s_list
                set item i of s_list to item (i + 1) of s_list
                set item (i + 1) of s_list to buf_item
            else
                set s_true to s_true + 1
            end if
        end repeat
        if s_true = (item_num - 1) then exit repeat
    end repeat
    return s_list
end sort_list_sub

スワップ

--リストのnLeft番目の項目とnRight番目の項目を入れ換えます。

on Swap(theList, nLeft, nRight)
    set Buf to item nLeft of theList
    set item nLeft of theList to item nRight of theList
    set item nRight of theList to Buf
    return theList
end Swap

ランダム並べ替え (数字)

--ランダム並べ替え
--returnはリスト
on narabekae(s_res)
    set r_list to {}
    set def_list to {}
    repeat with i from 1 to s_res
        set def_list to def_list & {i}
    end repeat
    repeat with ii from s_res to 1 by -1
        set def_list2 to {}
        set r_num to (random number (ii - 1)) + 1
        set r_list to r_list & {item r_num of def_list}
        repeat with iii from 1 to ii
            if iii is not r_num then set def_list2 to def_list2 & {item iii of def_list}
        end repeat
        set def_list to def_list2
    end repeat
    return r_list
end narabekae

ランダム並べ替え (リスト)

on random_sort(gain_list)
    set i_num to (count items of gain_list)
    set new_list to {}
    repeat with i from i_num to 1 by -1
        set ran_num to (random number (i - 1)) + 1
        set new_list to new_list & item ran_num of gain_list
        set buf_list to {}
        repeat with ii from 1 to i
            if ii is not ran_num then set buf_list to buf_list & item ii of gain_list
        end repeat
        set gain_list to buf_list
    end repeat
    return new_list
end random_sort
[Home] [List] [Recent] [Orphan] [Help] [Edit] [Remove]
-->