• 検索結果がありません。

整列アルゴリズムの比較評価と二分探索挿入ソートの提案

N/A
N/A
Protected

Academic year: 2021

シェア "整列アルゴリズムの比較評価と二分探索挿入ソートの提案"

Copied!
16
0
0

読み込み中.... (全文を見る)

全文

(1)

著者

新森 修一, 荒谷 遙香

雑誌名

鹿児島大学理学部紀要

53

ページ

1-15

発行年

2020-12-31

URL

http://hdl.handle.net/10232/00031550

(2)

ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢẚ㍑ホ౯࡜஧ศ᥈⣴ᤄධࢯ࣮ࢺࡢᥦ᱌

Comparative Evaluation of Some Sorting Algorithms and Proposal of

Binary Search Insertion Sort

৿ਁ रҲ ʀߧ୫ ᴶߵ̏ʥ

Shuichi SHINMORI1)ː, Haruka ARATANI1)

 㮵ඣᓥ኱Ꮫ኱Ꮫ㝔⌮ᕤᏛ◊✲⛉ᩘ⌮᝟ሗ⛉Ꮫᑓᨷ

1)Graduate School of Science Engineering, Kagoshima University, Kagoshima 890-0065 * ੻೜ஸं e-mail address : [email protected]

Abstract: We studied on typical sorting algorithms, compared their efficiencies, and proposed and evaluated a new

sorting algorithm. In this paper, for some typical sorting algorithms, the principle and properties of each algorithm are described, and a function-type program in C language is given. Numerical experiments were used to compare and evaluate the execution times of the three O(n2) sorting algorithms (i.e., Bubble sort, Insertion sort and Selection sort)

and the three O(nlogn) sorting algorithms ( i.e., Quicksort, Heaposrt and Mergesort). Furthermore, we proposed a new algorithm called Binary search insertion sort, compared it with the conventional Insertion sort, and confirmed that it can sort at a high speed of about 68%.

Keywords: Sorting algorithm, Insertion sort, Quicksort, Data structure, Numerical experiments.

 ࡣࡌࡵ࡟

ᩚิ(sorting) ࡜ࡣ㸪඲㡰ᗎ㞟ྜࡢせ⣲࡛࠶ࡿ」ᩘࡢࢹ࣮ࢱࢆ㸪඲㡰ᗎࠕӌࠖ࡟ᚑࡗ࡚ᑠࡉ࠸ࢹ࣮ࢱ ࠿ࡽ኱ࡁ࠸ࢹ࣮ࢱ࡬࡜୪࡭᭰࠼ࡿࡇ࡜㸦᪼㡰࡜࠸࠺㸧㸪࠶ࡿ࠸ࡣ㏫࡟㸪኱ࡁ࠸ࢹ࣮ࢱ࠿ࡽᑠࡉ࠸ࢹ࣮ࢱ ࡟୪࡭᭰࠼ࡿࡇ࡜㸦㝆㡰࡜࠸࠺㸧ࢆ࠸࠸㸪ࢯ࣮ࢺ㸪ࢯ࣮ࢸ࢕ࣥࢢ࡜ࡶ࠸࠺ࠋࢡ࢖ࢵࢡࢯ࣮ࢺ㸪ࣂࣈࣝࢯ ࣮ࢺ㸪ᤄධࢯ࣮ࢺ࡞࡝㸪ᩚิࡢࡓࡵࡢᩘከࡃࡢ࢔ࣝࢦࣜࢬ࣒ࡀ◊✲ࡉࢀ࡚࠸ࡿࡀ㸪ࡇࢀࡽࢆ⥲⛠ࡋ࡚ᩚ ิ࢔ࣝࢦࣜࢬ࣒࡜࠸࠺㸦[14], [15]㸧ࠋᡃࠎࡢ㌟㏆࡞࡜ࡇࢁ࡟ࡣ㸪ᵝࠎ࡞ከࡃࡢࢹ࣮ࢱࡀᏑᅾࡍࡿࠋࡇࢀ ࡽࡢࢹ࣮ࢱࡢ୰࡟ࡣ㸪඲㡰ᗎӌ࡛㡰ᗎ௜ࡅࡽࢀࡿࡶࡢࡀᩘከࡃ࠶ࡿࠋ౛࠼ࡤ㸪ᩚᩘࡸᐇᩘࡢᩘ್ࢹ࣮ࢱ ࡛࠶ࢀࡤ኱ᑠ㡰㸪᪥ᮏㄒࡸⱥㄒࡢ༢ㄒ࡞࡝ࡢᩥᏐࢹ࣮ࢱ࡛࠶ࢀࡤ㸪஬༑㡢㡰ࡸ࢔ࣝࣇ࢓࣋ࢵࢺ㡰࡞࡝ ࡢ㎡᭩ᘧ㡰ᗎ࡛኱ᑠ㛵ಀࡀ୚࠼ࡽࢀࡿࠋࢹ࣮ࢱࢆຠ⋡Ⰻࡃᩚิࡉࡏࡿ᪉ἲࡣࢹ࣮ࢱࢆ᥈⣴ࡍࡿ᪉ἲ࡞ ࡝࡜୪ࢇ࡛ᇶᮏⓗ࡞ၥ㢟࡛࠶ࡾ㸪௨๓࠿ࡽከࡃࡢ◊✲ࡀ࡞ࡉࢀ࡚࠸ࡿ㸦౛࠼ࡤ㸪[1], [2], [3], [6], [12]㸧ࠋ ᮏㄽᩥ࡛ࡣ㸪ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢ୰࡛ࡶ௦⾲ⓗ࡞ 6 ✀㢮ࡢ࢔ࣝࢦࣜࢬ࣒࡟ὀ┠ࡋ㸪ྛ࢔ࣝࢦࣜࢬ࣒ ࡢᴫせࡢㄝ᫂࡜ࡑࡢC ゝㄒ࡟ࡼࡿ㛵ᩘᙧ㸦ࣉࣟࢢ࣒ࣛ㸧ࡸ࢔ࣝࢦࣜࢬ࣒ࡢ⌮ㄽⓗ࡞ᛶ㉁ࢆ♧ࡋ㸪ᐇ㝿 ࡟኱つᶍ࡞ᩘ್ࢹ࣮ࢱ࡟ᑐࡋ࡚㸪ࡇࢀࡽࡢࣉࣟࢢ࣒ࣛࢆᐇ⾜ࡉࡏࡿᩘ್ᐇ㦂࡟ࡼࡾྛᩚิ࢔ࣝࢦࣜࢬ ࣒ࡢຠ⋡ᛶࡢホ౯ࢆ⾜࠺ࠋᚋ༙࡛ࡣ㸪ᤄධࢯ࣮ࢺ࡜ࡇࢀࢆᨵⰋࡋࡓ஧ศ᥈⣴ᤄධࢯ࣮ࢺࢆᥦ᱌ࡋ㸪C ゝ ㄒ࡛ࡢࣉࣟࢢ࣒ࣛ໬ࡢ᪉ἲ㸪ᐇ㝿ࡢࢹ࣮ࢱࢆ⏝࠸ࡓᩘ್ᐇ㦂࡟ࡼࡿຠ⋡ᛶࡢホ౯ࢆ⾜࠺ࠋ ➨ 2 ⠇࡛ࡣ㸪࢔ࣝࢦࣜࢬ࣒ࡢホ౯᪉ἲ࡛࠶ࡿ᫬㛫ィ⟬㔞࡜ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢᏳᐃᛶ࡟ࡘ࠸࡚㏙࡭ ࡿࠋ➨3㸪4 ⠇࡛ࡣ㸪௦⾲ⓗ࡞ 6 ✀㢮ࡢᩚิ࢔ࣝࢦࣜࢬ࣒࡟ὀ┠ࡋ㸪ࡇࢀࡽࢆ᫬㛫ィ⟬㔞࡟ࡼࡾ 2 ࡘࡢ ࢢ࣮ࣝࣉ࡟ศ㢮ࡍࡿࠋࡑࡋ࡚㸪ྛᩚิ࢔ࣝࢦࣜࢬ࣒ࡢᴫせࡢㄝ᫂㸪C ゝㄒࢆ⏝࠸࡚ࡢࣉࣟࢢ࣒ࣛ໬㸪ᩘ ್ᐇ㦂࡟ࡼࡿຠ⋡ᛶࡢẚ㍑ホ౯ࢆ⾜࠺ࠋ➨5㸪6 ⠇࡛ࡣ㸪஧ศ᥈⣴ᤄධࢯ࣮ࢺࢆᥦ᱌ࡋ㸪ᩘ್ᐇ㦂࡟ࡼ ࡾ㸪ᚑ᮶ࡢᤄධࢯ࣮ࢺ࡜ࡢຠ⋡ᛶࡢẚ㍑ホ౯ࢆ⾜࠺ࠋ➨㸵⠇ࡣࡲ࡜ࡵ࡜௒ᚋࡢㄢ㢟࡛࠶ࡿࠋ

㸰 ᫬㛫ィ⟬㔞࡜ࢯ࣮ࢺࡢᏳᐃᛶ

㸰㸯㸬࢔ࣝࢦࣜࢬ࣒ࡢ᫬㛫ィ⟬㔞

࢔ࣝࢦࣜࢬ࣒ࡢィ⟬㔞ࡣಶࠎࡢၥ㢟౛࡟ࡼࡗ࡚ኚ໬ࡍࡿࡢ࡛㸪඲యⓗ࡞ホ౯ࢆ⾜࠺࡟ࡣၥ㢟౛ࡢつ

ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢẚ㍑ホ౯࡜஧ศ᥈⣴ᤄධࢯ࣮ࢺࡢᥦ᱌

Comparative Evaluation of Some Sorting Algorithms and Proposal of

Binary Search Insertion Sort

৿ਁ रҲ ʀߧ୫ ᴶߵ̏ʥ

Shuichi SHINMORI1)ː, Haruka ARATANI1)

 㮵ඣᓥ኱Ꮫ኱Ꮫ㝔⌮ᕤᏛ◊✲⛉ᩘ⌮᝟ሗ⛉Ꮫᑓᨷ

1)Graduate School of Science Engineering, Kagoshima University, Kagoshima 890-0065 * ੻೜ஸं e-mail address : [email protected]

Abstract: We studied on typical sorting algorithms, compared their efficiencies, and proposed and evaluated a new

sorting algorithm. In this paper, for some typical sorting algorithms, the principle and properties of each algorithm are described, and a function-type program in C language is given. Numerical experiments were used to compare and evaluate the execution times of the three O(n2) sorting algorithms (i.e., Bubble sort, Insertion sort and Selection sort)

and the three O(nlogn) sorting algorithms ( i.e., Quicksort, Heaposrt and Mergesort). Furthermore, we proposed a new algorithm called Binary search insertion sort, compared it with the conventional Insertion sort, and confirmed that it can sort at a high speed of about 68%.

Keywords: Sorting algorithm, Insertion sort, Quicksort, Data structure, Numerical experiments.

 ࡣࡌࡵ࡟

ᩚิ(sorting) ࡜ࡣ㸪඲㡰ᗎ㞟ྜࡢせ⣲࡛࠶ࡿ」ᩘࡢࢹ࣮ࢱࢆ㸪඲㡰ᗎࠕӌࠖ࡟ᚑࡗ࡚ᑠࡉ࠸ࢹ࣮ࢱ ࠿ࡽ኱ࡁ࠸ࢹ࣮ࢱ࡬࡜୪࡭᭰࠼ࡿࡇ࡜㸦᪼㡰࡜࠸࠺㸧㸪࠶ࡿ࠸ࡣ㏫࡟㸪኱ࡁ࠸ࢹ࣮ࢱ࠿ࡽᑠࡉ࠸ࢹ࣮ࢱ ࡟୪࡭᭰࠼ࡿࡇ࡜㸦㝆㡰࡜࠸࠺㸧ࢆ࠸࠸㸪ࢯ࣮ࢺ㸪ࢯ࣮ࢸ࢕ࣥࢢ࡜ࡶ࠸࠺ࠋࢡ࢖ࢵࢡࢯ࣮ࢺ㸪ࣂࣈࣝࢯ ࣮ࢺ㸪ᤄධࢯ࣮ࢺ࡞࡝㸪ᩚิࡢࡓࡵࡢᩘከࡃࡢ࢔ࣝࢦࣜࢬ࣒ࡀ◊✲ࡉࢀ࡚࠸ࡿࡀ㸪ࡇࢀࡽࢆ⥲⛠ࡋ࡚ᩚ ิ࢔ࣝࢦࣜࢬ࣒࡜࠸࠺㸦[14], [15]㸧ࠋᡃࠎࡢ㌟㏆࡞࡜ࡇࢁ࡟ࡣ㸪ᵝࠎ࡞ከࡃࡢࢹ࣮ࢱࡀᏑᅾࡍࡿࠋࡇࢀ ࡽࡢࢹ࣮ࢱࡢ୰࡟ࡣ㸪඲㡰ᗎӌ࡛㡰ᗎ௜ࡅࡽࢀࡿࡶࡢࡀᩘከࡃ࠶ࡿࠋ౛࠼ࡤ㸪ᩚᩘࡸᐇᩘࡢᩘ್ࢹ࣮ࢱ ࡛࠶ࢀࡤ኱ᑠ㡰㸪᪥ᮏㄒࡸⱥㄒࡢ༢ㄒ࡞࡝ࡢᩥᏐࢹ࣮ࢱ࡛࠶ࢀࡤ㸪஬༑㡢㡰ࡸ࢔ࣝࣇ࢓࣋ࢵࢺ㡰࡞࡝ ࡢ㎡᭩ᘧ㡰ᗎ࡛኱ᑠ㛵ಀࡀ୚࠼ࡽࢀࡿࠋࢹ࣮ࢱࢆຠ⋡Ⰻࡃᩚิࡉࡏࡿ᪉ἲࡣࢹ࣮ࢱࢆ᥈⣴ࡍࡿ᪉ἲ࡞ ࡝࡜୪ࢇ࡛ᇶᮏⓗ࡞ၥ㢟࡛࠶ࡾ㸪௨๓࠿ࡽከࡃࡢ◊✲ࡀ࡞ࡉࢀ࡚࠸ࡿ㸦౛࠼ࡤ㸪[1], [2], [3], [6], [12]㸧ࠋ ᮏㄽᩥ࡛ࡣ㸪ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢ୰࡛ࡶ௦⾲ⓗ࡞ 6 ✀㢮ࡢ࢔ࣝࢦࣜࢬ࣒࡟ὀ┠ࡋ㸪ྛ࢔ࣝࢦࣜࢬ࣒ ࡢᴫせࡢㄝ᫂࡜ࡑࡢC ゝㄒ࡟ࡼࡿ㛵ᩘᙧ㸦ࣉࣟࢢ࣒ࣛ㸧ࡸ࢔ࣝࢦࣜࢬ࣒ࡢ⌮ㄽⓗ࡞ᛶ㉁ࢆ♧ࡋ㸪ᐇ㝿 ࡟኱つᶍ࡞ᩘ್ࢹ࣮ࢱ࡟ᑐࡋ࡚㸪ࡇࢀࡽࡢࣉࣟࢢ࣒ࣛࢆᐇ⾜ࡉࡏࡿᩘ್ᐇ㦂࡟ࡼࡾྛᩚิ࢔ࣝࢦࣜࢬ ࣒ࡢຠ⋡ᛶࡢホ౯ࢆ⾜࠺ࠋᚋ༙࡛ࡣ㸪ᤄධࢯ࣮ࢺ࡜ࡇࢀࢆᨵⰋࡋࡓ஧ศ᥈⣴ᤄධࢯ࣮ࢺࢆᥦ᱌ࡋ㸪C ゝ ㄒ࡛ࡢࣉࣟࢢ࣒ࣛ໬ࡢ᪉ἲ㸪ᐇ㝿ࡢࢹ࣮ࢱࢆ⏝࠸ࡓᩘ್ᐇ㦂࡟ࡼࡿຠ⋡ᛶࡢホ౯ࢆ⾜࠺ࠋ ➨ 2 ⠇࡛ࡣ㸪࢔ࣝࢦࣜࢬ࣒ࡢホ౯᪉ἲ࡛࠶ࡿ᫬㛫ィ⟬㔞࡜ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢᏳᐃᛶ࡟ࡘ࠸࡚㏙࡭ ࡿࠋ➨3㸪4 ⠇࡛ࡣ㸪௦⾲ⓗ࡞ 6 ✀㢮ࡢᩚิ࢔ࣝࢦࣜࢬ࣒࡟ὀ┠ࡋ㸪ࡇࢀࡽࢆ᫬㛫ィ⟬㔞࡟ࡼࡾ 2 ࡘࡢ ࢢ࣮ࣝࣉ࡟ศ㢮ࡍࡿࠋࡑࡋ࡚㸪ྛᩚิ࢔ࣝࢦࣜࢬ࣒ࡢᴫせࡢㄝ᫂㸪C ゝㄒࢆ⏝࠸࡚ࡢࣉࣟࢢ࣒ࣛ໬㸪ᩘ ್ᐇ㦂࡟ࡼࡿຠ⋡ᛶࡢẚ㍑ホ౯ࢆ⾜࠺ࠋ➨5㸪6 ⠇࡛ࡣ㸪஧ศ᥈⣴ᤄධࢯ࣮ࢺࢆᥦ᱌ࡋ㸪ᩘ್ᐇ㦂࡟ࡼ ࡾ㸪ᚑ᮶ࡢᤄධࢯ࣮ࢺ࡜ࡢຠ⋡ᛶࡢẚ㍑ホ౯ࢆ⾜࠺ࠋ➨㸵⠇ࡣࡲ࡜ࡵ࡜௒ᚋࡢㄢ㢟࡛࠶ࡿࠋ

㸰 ᫬㛫ィ⟬㔞࡜ࢯ࣮ࢺࡢᏳᐃᛶ

㸰㸯㸬࢔ࣝࢦࣜࢬ࣒ࡢ᫬㛫ィ⟬㔞

࢔ࣝࢦࣜࢬ࣒ࡢィ⟬㔞ࡣಶࠎࡢၥ㢟౛࡟ࡼࡗ࡚ኚ໬ࡍࡿࡢ࡛㸪඲యⓗ࡞ホ౯ࢆ⾜࠺࡟ࡣၥ㢟౛ࡢつ

(3)

ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢẚ㍑ホ౯࡜஧ศ᥈⣴ᤄධࢯ࣮ࢺࡢᥦ᱌

Comparative Evaluation of Some Sorting Algorithms and Proposal of

Binary Search Insertion Sort

৿ਁ रҲ ʀߧ୫ ᴶߵ̏ʥ

Shuichi SHINMORI1)ː, Haruka ARATANI1)

 㮵ඣᓥ኱Ꮫ኱Ꮫ㝔⌮ᕤᏛ◊✲⛉ᩘ⌮᝟ሗ⛉Ꮫᑓᨷ

1)Graduate School of Science Engineering, Kagoshima University, Kagoshima 890-0065 * ੻೜ஸं e-mail address : [email protected]

Abstract: We studied on typical sorting algorithms, compared their efficiencies, and proposed and evaluated a new

sorting algorithm. In this paper, for some typical sorting algorithms, the principle and properties of each algorithm are described, and a function-type program in C language is given. Numerical experiments were used to compare and evaluate the execution times of the three O(n2) sorting algorithms (i.e., Bubble sort, Insertion sort and Selection sort)

and the three O(nlogn) sorting algorithms ( i.e., Quicksort, Heaposrt and Mergesort). Furthermore, we proposed a new algorithm called Binary search insertion sort, compared it with the conventional Insertion sort, and confirmed that it can sort at a high speed of about 68%.

Keywords: Sorting algorithm, Insertion sort, Quicksort, Data structure, Numerical experiments.

 ࡣࡌࡵ࡟

ᩚิ(sorting) ࡜ࡣ㸪඲㡰ᗎ㞟ྜࡢせ⣲࡛࠶ࡿ」ᩘࡢࢹ࣮ࢱࢆ㸪඲㡰ᗎࠕӌࠖ࡟ᚑࡗ࡚ᑠࡉ࠸ࢹ࣮ࢱ ࠿ࡽ኱ࡁ࠸ࢹ࣮ࢱ࡬࡜୪࡭᭰࠼ࡿࡇ࡜㸦᪼㡰࡜࠸࠺㸧㸪࠶ࡿ࠸ࡣ㏫࡟㸪኱ࡁ࠸ࢹ࣮ࢱ࠿ࡽᑠࡉ࠸ࢹ࣮ࢱ ࡟୪࡭᭰࠼ࡿࡇ࡜㸦㝆㡰࡜࠸࠺㸧ࢆ࠸࠸㸪ࢯ࣮ࢺ㸪ࢯ࣮ࢸ࢕ࣥࢢ࡜ࡶ࠸࠺ࠋࢡ࢖ࢵࢡࢯ࣮ࢺ㸪ࣂࣈࣝࢯ ࣮ࢺ㸪ᤄධࢯ࣮ࢺ࡞࡝㸪ᩚิࡢࡓࡵࡢᩘከࡃࡢ࢔ࣝࢦࣜࢬ࣒ࡀ◊✲ࡉࢀ࡚࠸ࡿࡀ㸪ࡇࢀࡽࢆ⥲⛠ࡋ࡚ᩚ ิ࢔ࣝࢦࣜࢬ࣒࡜࠸࠺㸦[14], [15]㸧ࠋᡃࠎࡢ㌟㏆࡞࡜ࡇࢁ࡟ࡣ㸪ᵝࠎ࡞ከࡃࡢࢹ࣮ࢱࡀᏑᅾࡍࡿࠋࡇࢀ ࡽࡢࢹ࣮ࢱࡢ୰࡟ࡣ㸪඲㡰ᗎӌ࡛㡰ᗎ௜ࡅࡽࢀࡿࡶࡢࡀᩘከࡃ࠶ࡿࠋ౛࠼ࡤ㸪ᩚᩘࡸᐇᩘࡢᩘ್ࢹ࣮ࢱ ࡛࠶ࢀࡤ኱ᑠ㡰㸪᪥ᮏㄒࡸⱥㄒࡢ༢ㄒ࡞࡝ࡢᩥᏐࢹ࣮ࢱ࡛࠶ࢀࡤ㸪஬༑㡢㡰ࡸ࢔ࣝࣇ࢓࣋ࢵࢺ㡰࡞࡝ ࡢ㎡᭩ᘧ㡰ᗎ࡛኱ᑠ㛵ಀࡀ୚࠼ࡽࢀࡿࠋࢹ࣮ࢱࢆຠ⋡Ⰻࡃᩚิࡉࡏࡿ᪉ἲࡣࢹ࣮ࢱࢆ᥈⣴ࡍࡿ᪉ἲ࡞ ࡝࡜୪ࢇ࡛ᇶᮏⓗ࡞ၥ㢟࡛࠶ࡾ㸪௨๓࠿ࡽከࡃࡢ◊✲ࡀ࡞ࡉࢀ࡚࠸ࡿ㸦౛࠼ࡤ㸪[1], [2], [3], [6], [12]㸧ࠋ ᮏㄽᩥ࡛ࡣ㸪ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢ୰࡛ࡶ௦⾲ⓗ࡞ 6 ✀㢮ࡢ࢔ࣝࢦࣜࢬ࣒࡟ὀ┠ࡋ㸪ྛ࢔ࣝࢦࣜࢬ࣒ ࡢᴫせࡢㄝ᫂࡜ࡑࡢC ゝㄒ࡟ࡼࡿ㛵ᩘᙧ㸦ࣉࣟࢢ࣒ࣛ㸧ࡸ࢔ࣝࢦࣜࢬ࣒ࡢ⌮ㄽⓗ࡞ᛶ㉁ࢆ♧ࡋ㸪ᐇ㝿 ࡟኱つᶍ࡞ᩘ್ࢹ࣮ࢱ࡟ᑐࡋ࡚㸪ࡇࢀࡽࡢࣉࣟࢢ࣒ࣛࢆᐇ⾜ࡉࡏࡿᩘ್ᐇ㦂࡟ࡼࡾྛᩚิ࢔ࣝࢦࣜࢬ ࣒ࡢຠ⋡ᛶࡢホ౯ࢆ⾜࠺ࠋᚋ༙࡛ࡣ㸪ᤄධࢯ࣮ࢺ࡜ࡇࢀࢆᨵⰋࡋࡓ஧ศ᥈⣴ᤄධࢯ࣮ࢺࢆᥦ᱌ࡋ㸪C ゝ ㄒ࡛ࡢࣉࣟࢢ࣒ࣛ໬ࡢ᪉ἲ㸪ᐇ㝿ࡢࢹ࣮ࢱࢆ⏝࠸ࡓᩘ್ᐇ㦂࡟ࡼࡿຠ⋡ᛶࡢホ౯ࢆ⾜࠺ࠋ ➨ 2 ⠇࡛ࡣ㸪࢔ࣝࢦࣜࢬ࣒ࡢホ౯᪉ἲ࡛࠶ࡿ᫬㛫ィ⟬㔞࡜ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢᏳᐃᛶ࡟ࡘ࠸࡚㏙࡭ ࡿࠋ➨3㸪4 ⠇࡛ࡣ㸪௦⾲ⓗ࡞ 6 ✀㢮ࡢᩚิ࢔ࣝࢦࣜࢬ࣒࡟ὀ┠ࡋ㸪ࡇࢀࡽࢆ᫬㛫ィ⟬㔞࡟ࡼࡾ 2 ࡘࡢ ࢢ࣮ࣝࣉ࡟ศ㢮ࡍࡿࠋࡑࡋ࡚㸪ྛᩚิ࢔ࣝࢦࣜࢬ࣒ࡢᴫせࡢㄝ᫂㸪C ゝㄒࢆ⏝࠸࡚ࡢࣉࣟࢢ࣒ࣛ໬㸪ᩘ ್ᐇ㦂࡟ࡼࡿຠ⋡ᛶࡢẚ㍑ホ౯ࢆ⾜࠺ࠋ➨5㸪6 ⠇࡛ࡣ㸪஧ศ᥈⣴ᤄධࢯ࣮ࢺࢆᥦ᱌ࡋ㸪ᩘ್ᐇ㦂࡟ࡼ ࡾ㸪ᚑ᮶ࡢᤄධࢯ࣮ࢺ࡜ࡢຠ⋡ᛶࡢẚ㍑ホ౯ࢆ⾜࠺ࠋ➨㸵⠇ࡣࡲ࡜ࡵ࡜௒ᚋࡢㄢ㢟࡛࠶ࡿࠋ

㸰 ᫬㛫ィ⟬㔞࡜ࢯ࣮ࢺࡢᏳᐃᛶ

㸰㸯㸬࢔ࣝࢦࣜࢬ࣒ࡢ᫬㛫ィ⟬㔞

࢔ࣝࢦࣜࢬ࣒ࡢィ⟬㔞ࡣಶࠎࡢၥ㢟౛࡟ࡼࡗ࡚ኚ໬ࡍࡿࡢ࡛㸪඲యⓗ࡞ホ౯ࢆ⾜࠺࡟ࡣၥ㢟౛ࡢつ ᶍ࡟ᛂࡌ࡚ィ⟬㔞ࡀ࡝ࡢࡼ࠺࡟ኚ໬ࡍࡿࡢ࠿㸪ࡑࡢ㛵ᩘᙧࢆồࡵࡿࡇ࡜ࡀ㔜せ࡛࠶ࡿࠋ୍⯡࡟㸪ၥ㢟౛ ࡢつᶍࡣࡑࢀࢆධຊࡍࡿࡓࡵࡢࢹ࣮ࢱࡢ㛗ࡉ

n

㸦ධຊࢧ࢖ࢬ࡜࠸࠺㸧࡛ホ౯ࡍࡿ㸦[9], [13]㸧ࠋ ᫬㛫ィ⟬㔞(time complexity)࡜ࡣ㸪࢔ࣝࢦࣜࢬ࣒ࡢᇶᮏ᧯సࡣ㸪ᅄ๎₇⟬ࡸẚ㍑࡞࡝ࡢᇶᮏࢫࢸࢵࣉ ࡟ศゎ࡛ࡁࡿࡀ㸪࢔ࣝࢦࣜࢬ࣒ࡀ⤊஢ࡍࡿࡲ࡛ࡢᇶᮏࢫࢸࢵࣉࡢᐇ⾜ᅇᩘࡢࡇ࡜࡛࠶ࡿࠋ࢔ࣝࢦࣜࢬ ࣒ࡢ᫬㛫ィ⟬㔞ࡣධຊࢧ࢖ࢬ࡟౫Ꮡࡍࡿሙྜࡀከ࠸ࠋࡲࡓ㸪࠶ࡿධຊࢧ࢖ࢬ

n

࡟࠾࠸࡚㸪㐺ᙜ࡞᮲௳ ୗ࡛᭱ࡶ㏿ࡃ࢔ࣝࢦࣜࢬ࣒ࢆᐇ⾜࡛ࡁࡿሙྜࡢ᫬㛫ィ⟬㔞ࢆ᭱Ⰻ᫬㛫ィ⟬㔞࡜ࡼࡧ㸪࢔ࣝࢦࣜࢬ࣒ࡢ ᐇ⾜࡟᭱ࡶ᫬㛫ࡢ࠿࠿ࡿሙྜࡢ᫬㛫ィ⟬㔞ࢆ᭱ᝏ᫬㛫ィ⟬㔞࡜ࡼࡪࠋ ࢔ࣝࢦࣜࢬ࣒ࡢィ⟬㔞࡟ࡘ࠸࡚ࡣ㸪᫬㛫ィ⟬㔞ࡢ௚࡟࢔ࣝࢦࣜࢬ࣒ࡢࢹ࣮ࢱ౑⏝㔞ࢆ⾲ࡍ㡿ᇦィ⟬ 㔞(space complexity)ࡀ࠶ࡿࠋࡇࡢ㡿ᇦィ⟬㔞ࡶ್ࡀᑠࡉ࠸࡯࡝Ⰻ࠸࢔ࣝࢦࣜࢬ࣒࡜ゝ࠼ࡿࠋࡋ࠿ࡋ㸪ᐇ 㝿ࡢィ⟬࡛ࡣ㸪㡿ᇦィ⟬㔞࡟ẚ࡭᫬㛫ィ⟬㔞ࡢ᪉ࡀ㔜せ࡟࡞ࡿሙྜࡀከ࠸ࡢ࡛㸪༢࡟ຠ⋡ࡢⰋ࠸࢔ࣝ ࢦࣜࢬ࣒࡜࠸࠼ࡤ㸪᫬㛫ィ⟬㔞ࡢᑠࡉ࠸࢔ࣝࢦࣜࢬ࣒ࢆᣦࡍሙྜࡀከ࠸ࠋ ࡲࡓ㸪᫬㛫ィ⟬㔞࡟ࡘ࠸࡚ࡶ㸪ධຊ࡟ᑐࡍࡿ᫬㛫ィ⟬㔞ࡢᮇᚅ್ࢆ⪃࠼ࡿሙྜࡀ࠶ࡿࠋࡇࡢሙྜࡢ᫬ 㛫ィ⟬㔞ࢆᖹᆒ᫬㛫ィ⟬㔞࡜࿧ࡪࠋከࡃࡢሙྜ㸪ᖹᆒ᫬㛫ィ⟬㔞ࡣ᭱ᝏ᫬㛫ィ⟬㔞࡟➼ࡋ࠸ࡇ࡜ࡀ▱ ࡽࢀ࡚࠸ࡿ㸦౛࠼ࡤ㸪[7], [8]㸧ࠋ ࡉ࡚㸪࢔ࣝࢦࣜࢬ࣒ࡢຠ⋡ᛶࢆホ౯ࡍࡿ㝿ࡣ㸪ධຊࢧ࢖ࢬ

n

ࡀ࠶ࡿ⛬ᗘ኱ࡁ࠸ሙྜࢆ᝿ᐃࡋ࡚࠸ࡿ ࡀ㸪ࡇࢀࡽࡢホ౯࡟⏝࠸ࡽࢀࡿ࢔ࣝࢦࣜࢬ࣒ࡢ᫬㛫ィ⟬㔞ࢆ₞㏆ⓗ࡞᫬㛫ィ⟬㔞࡜ࡼࡪࠋࡇࡢ₞㏆ⓗ ࡞᫬㛫ィ⟬㔞ࡣ㸪௨ୗ࡟ᐃ⩏ࡉࢀࡿ࣮࢜ࢲグἲࢆ⏝࠸࡚⾲ࢃࡉࢀࡿ㸦[8], [13]㸧ࠋ ሾᐃ⩏ ͳሿ 㸦࣮࢜ࢲグἲ㸧  ධຊࢧ࢖ࢬ ݊ ࡢ㛵ᩘ࡜ࡋ࡚⾲ࢃࡉࢀࡿ᫬㛫ィ⟬㔞 ܶሺ݊ሻ ࡀ㸪࠶ࡿ㛵ᩘ  ݂ሺ݊ሻ ࡟ᑐࡋ࡚ߍ൫݂ሺ݊ሻ൯ ࡛࠶ࡿ࡜ࡣ㸪㐺ᙜ࡞ ʹ ࡘࡢṇࡢᐃᩘ ݊଴ ࡜ ܿ ࡀᏑᅾࡋ㸪݊଴ ௨ୖࡢࡍ࡭ ࡚ࡢ ݊ ࡟ࡘ࠸࡚ ܶሺ݊ሻ ൑ ܿ ή ݂ሺ݊ሻ ࡀᡂࡾ❧ࡘࡇ࡜ࢆ࠸࠺ࠋ ‹Ǥ‡Ǥ  ܶሺ݊ሻ ൌ ߍ൫݂ሺ݊ሻ൯  ฻  ܿ׌ ൐ Ͳǡ ݊ ଴א Գ ׌ ǡ ݊ ൒ ݊ ଴ ׊ ǡ ܶሺ݊ሻ ൑ ܿ ή ݂ሺ݊ሻ ࡲࡓ㸪ܶሺ݊ሻ ൌ ߍ൫݂ሺ݊ሻ൯ ࡛࠶ࡿሙྜ㸪Dz࢔ࣝࢦࣜࢬ࣒ࡢ᫬㛫ィ⟬㔞ࡣ ߍ൫݂ሺ݊ሻ൯ ࡛࠶ࡿdzࡶࡋࡃࡣDz࢔ࣝ ࢦࣜࢬ࣒ࡣ ߍ൫݂ሺ݊ሻ൯ ᫬㛫࡛ᐇ⾜࡛ࡁࡿdz࡜࠸࠺ࠋ

㸰㸰㸬ᩚิ࢔ࣝࢦࣜࢬ࣒࡜Ᏻᐃᛶ

ᩚิ㸦࠶ࡿ࠸ࡣࢯ࣮ࢺ ሺ•‘”–ሻ㸧࡜ࡣ㸪ḟ࡟ᐃ⩏ࡉࢀࡿࡼ࠺࡟ Dz୚࠼ࡽࢀࡓࢹ࣮ࢱࢆỴࡵࡽࢀࡓ㡰␒࡟ ୪࡭᭰࠼ࡿdz ࡜࠸࠺᧯స࡛࠶ࡾ㸪ࡇࡢ᧯సࡸᡭ㡰ࢆཝᐦ࡟ᐃ⩏ࡋࡓࡶࡢࡀᩚิ㸦ࢯ࣮ࢺ㸧࢔ࣝࢦࣜࢬ࣒ ࡛࠶ࡿࠋ ሾᐃ⩏ ʹሿ  ᩚิ㸦࠶ࡿ࠸ࡣࢯ࣮ࢺ㸧࡜ࡣ㸪ධຊࡉࢀࡓ ݊ ಶࡢࢹ࣮ࢱ ݀଴ǡ ݀ଵǡ ڮ ǡ ݀௡ିଵ ࡀ୚࠼ࡽࢀࡓ࡜ ࡁ࡟㸪ࡑࡢࢹ࣮ࢱࢆ᪼㡰㸪ࡲࡓࡣ㝆㡰࡟୪࡭᭰࠼ࡿ᧯సࡢࡇ࡜࡛࠶ࡿࠋ ୍⯡ⓗ࡟ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢධຊ࡛ࡣྠࡌ್ࢆᣢࡗࡓࢹ࣮ࢱࡀከᩘᏑᅾࡍࡿሙྜࡀ⪃࠼ࡽࢀࡿࠋᩚ ิ࢔ࣝࢦࣜࢬ࣒࡟ࡘ࠸࡚ࡣ㸪ࡇࡢྠࡌ್ࢆᣢࡗࡓࢹ࣮ࢱࢆ࡝࠺࠸࠺㡰ᗎ࡛୪࡭ࡿ࠿࡜࠸࠺Ⅼ࡟ὀពࡍ ࡿᚲせࡀ࠶ࡿࠋḟࡢᐃ⩏࡛♧ࡍࡼ࠺࡟㸪ྠࡌ್ࡢࢹ࣮ࢱࡀࢯ࣮ࢺࡢ๓ᚋ࡛ኚ໬ࡋ࡞࠸ᩚิ࢔ࣝࢦࣜࢬ ࣒ࢆᏳᐃ࡞࢔ࣝࢦࣜࢬ࣒࡜࠸࠺ࠋ ሾᐃ⩏ ͵ሿ  Ᏻᐃ࡞ᩚิ࢔ࣝࢦࣜࢬ࣒࡜ࡣ㸪௨ୗࡢ᮲௳ࢆ‶ࡓࡍ࢔ࣝࢦࣜࢬ࣒ࡢࡇ࡜࡛࠶ࡿࠋ ሺƒሻ ୚࠼ࡽࢀࡓࢹ࣮ࢱࢆ᪼㡰㸪ࡲࡓࡣ㝆㡰࡟୪࡭᭰࠼ࡿࠋ ሺ„ሻ ྠࡌ್ࡢࢹ࣮ࢱࡣ㸪ᙜึࡢධຊࡢ㡰ᗎ࡝࠾ࡾ࡟୪࡭᭰࠼ࡿࠋ ᩚิ࢔ࣝࢦࣜࢬ࣒࡟ࡣຠ⋡ⓗ࡛࠶ࡿࡀᏳᐃ࡛࡞࠸ࡶࡢ㸪ຠ⋡ᛶࡣ㧗ࡃ࡞࠸ࡀᏳᐃᛶࡢ᮲௳ࢆ‶ࡓࡋ ࡚࠸ࡿࡶࡢ࡞࡝ከࡃࡢ✀㢮ࡀ࠶ࡿࠋ࢔ࣝࢦࣜࢬ࣒ࡢຠ⋡ᛶ㸦ᐇ⾜㏿ᗘ㸧ࡔࡅ࡛࡞ࡃ㸪ሙྜ࡟ࡼࡗ࡚ࡣᏳ ᐃᛶࡶ⪃៖ࡋ࡚㸪ࢯ࣮ࢺࡢ┠ⓗ࡟ᛂࡌࡓᩚิ࢔ࣝࢦࣜࢬ࣒ࢆ㑅ᢥࡍࡿᚲせࡀ࠶ࡿࠋ (1)

(4)

ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢẚ㍑ホ౯࡜஧ศ᥈⣴ᤄධࢯ࣮ࢺࡢᥦ᱌

Comparative Evaluation of Some Sorting Algorithms and Proposal of

Binary Search Insertion Sort

৿ਁ रҲ ʀߧ୫ ᴶߵ̏ʥ

Shuichi SHINMORI1)ː, Haruka ARATANI1)

 㮵ඣᓥ኱Ꮫ኱Ꮫ㝔⌮ᕤᏛ◊✲⛉ᩘ⌮᝟ሗ⛉Ꮫᑓᨷ

1)Graduate School of Science Engineering, Kagoshima University, Kagoshima 890-0065 * ੻೜ஸं e-mail address : [email protected]

Abstract: We studied on typical sorting algorithms, compared their efficiencies, and proposed and evaluated a new

sorting algorithm. In this paper, for some typical sorting algorithms, the principle and properties of each algorithm are described, and a function-type program in C language is given. Numerical experiments were used to compare and evaluate the execution times of the three O(n2) sorting algorithms (i.e., Bubble sort, Insertion sort and Selection sort)

and the three O(nlogn) sorting algorithms ( i.e., Quicksort, Heaposrt and Mergesort). Furthermore, we proposed a new algorithm called Binary search insertion sort, compared it with the conventional Insertion sort, and confirmed that it can sort at a high speed of about 68%.

Keywords: Sorting algorithm, Insertion sort, Quicksort, Data structure, Numerical experiments.

 ࡣࡌࡵ࡟

ᩚิ(sorting) ࡜ࡣ㸪඲㡰ᗎ㞟ྜࡢせ⣲࡛࠶ࡿ」ᩘࡢࢹ࣮ࢱࢆ㸪඲㡰ᗎࠕӌࠖ࡟ᚑࡗ࡚ᑠࡉ࠸ࢹ࣮ࢱ ࠿ࡽ኱ࡁ࠸ࢹ࣮ࢱ࡬࡜୪࡭᭰࠼ࡿࡇ࡜㸦᪼㡰࡜࠸࠺㸧㸪࠶ࡿ࠸ࡣ㏫࡟㸪኱ࡁ࠸ࢹ࣮ࢱ࠿ࡽᑠࡉ࠸ࢹ࣮ࢱ ࡟୪࡭᭰࠼ࡿࡇ࡜㸦㝆㡰࡜࠸࠺㸧ࢆ࠸࠸㸪ࢯ࣮ࢺ㸪ࢯ࣮ࢸ࢕ࣥࢢ࡜ࡶ࠸࠺ࠋࢡ࢖ࢵࢡࢯ࣮ࢺ㸪ࣂࣈࣝࢯ ࣮ࢺ㸪ᤄධࢯ࣮ࢺ࡞࡝㸪ᩚิࡢࡓࡵࡢᩘከࡃࡢ࢔ࣝࢦࣜࢬ࣒ࡀ◊✲ࡉࢀ࡚࠸ࡿࡀ㸪ࡇࢀࡽࢆ⥲⛠ࡋ࡚ᩚ ิ࢔ࣝࢦࣜࢬ࣒࡜࠸࠺㸦[14], [15]㸧ࠋᡃࠎࡢ㌟㏆࡞࡜ࡇࢁ࡟ࡣ㸪ᵝࠎ࡞ከࡃࡢࢹ࣮ࢱࡀᏑᅾࡍࡿࠋࡇࢀ ࡽࡢࢹ࣮ࢱࡢ୰࡟ࡣ㸪඲㡰ᗎӌ࡛㡰ᗎ௜ࡅࡽࢀࡿࡶࡢࡀᩘከࡃ࠶ࡿࠋ౛࠼ࡤ㸪ᩚᩘࡸᐇᩘࡢᩘ್ࢹ࣮ࢱ ࡛࠶ࢀࡤ኱ᑠ㡰㸪᪥ᮏㄒࡸⱥㄒࡢ༢ㄒ࡞࡝ࡢᩥᏐࢹ࣮ࢱ࡛࠶ࢀࡤ㸪஬༑㡢㡰ࡸ࢔ࣝࣇ࢓࣋ࢵࢺ㡰࡞࡝ ࡢ㎡᭩ᘧ㡰ᗎ࡛኱ᑠ㛵ಀࡀ୚࠼ࡽࢀࡿࠋࢹ࣮ࢱࢆຠ⋡Ⰻࡃᩚิࡉࡏࡿ᪉ἲࡣࢹ࣮ࢱࢆ᥈⣴ࡍࡿ᪉ἲ࡞ ࡝࡜୪ࢇ࡛ᇶᮏⓗ࡞ၥ㢟࡛࠶ࡾ㸪௨๓࠿ࡽከࡃࡢ◊✲ࡀ࡞ࡉࢀ࡚࠸ࡿ㸦౛࠼ࡤ㸪[1], [2], [3], [6], [12]㸧ࠋ ᮏㄽᩥ࡛ࡣ㸪ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢ୰࡛ࡶ௦⾲ⓗ࡞ 6 ✀㢮ࡢ࢔ࣝࢦࣜࢬ࣒࡟ὀ┠ࡋ㸪ྛ࢔ࣝࢦࣜࢬ࣒ ࡢᴫせࡢㄝ᫂࡜ࡑࡢC ゝㄒ࡟ࡼࡿ㛵ᩘᙧ㸦ࣉࣟࢢ࣒ࣛ㸧ࡸ࢔ࣝࢦࣜࢬ࣒ࡢ⌮ㄽⓗ࡞ᛶ㉁ࢆ♧ࡋ㸪ᐇ㝿 ࡟኱つᶍ࡞ᩘ್ࢹ࣮ࢱ࡟ᑐࡋ࡚㸪ࡇࢀࡽࡢࣉࣟࢢ࣒ࣛࢆᐇ⾜ࡉࡏࡿᩘ್ᐇ㦂࡟ࡼࡾྛᩚิ࢔ࣝࢦࣜࢬ ࣒ࡢຠ⋡ᛶࡢホ౯ࢆ⾜࠺ࠋᚋ༙࡛ࡣ㸪ᤄධࢯ࣮ࢺ࡜ࡇࢀࢆᨵⰋࡋࡓ஧ศ᥈⣴ᤄධࢯ࣮ࢺࢆᥦ᱌ࡋ㸪C ゝ ㄒ࡛ࡢࣉࣟࢢ࣒ࣛ໬ࡢ᪉ἲ㸪ᐇ㝿ࡢࢹ࣮ࢱࢆ⏝࠸ࡓᩘ್ᐇ㦂࡟ࡼࡿຠ⋡ᛶࡢホ౯ࢆ⾜࠺ࠋ ➨ 2 ⠇࡛ࡣ㸪࢔ࣝࢦࣜࢬ࣒ࡢホ౯᪉ἲ࡛࠶ࡿ᫬㛫ィ⟬㔞࡜ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢᏳᐃᛶ࡟ࡘ࠸࡚㏙࡭ ࡿࠋ➨3㸪4 ⠇࡛ࡣ㸪௦⾲ⓗ࡞ 6 ✀㢮ࡢᩚิ࢔ࣝࢦࣜࢬ࣒࡟ὀ┠ࡋ㸪ࡇࢀࡽࢆ᫬㛫ィ⟬㔞࡟ࡼࡾ 2 ࡘࡢ ࢢ࣮ࣝࣉ࡟ศ㢮ࡍࡿࠋࡑࡋ࡚㸪ྛᩚิ࢔ࣝࢦࣜࢬ࣒ࡢᴫせࡢㄝ᫂㸪C ゝㄒࢆ⏝࠸࡚ࡢࣉࣟࢢ࣒ࣛ໬㸪ᩘ ್ᐇ㦂࡟ࡼࡿຠ⋡ᛶࡢẚ㍑ホ౯ࢆ⾜࠺ࠋ➨5㸪6 ⠇࡛ࡣ㸪஧ศ᥈⣴ᤄධࢯ࣮ࢺࢆᥦ᱌ࡋ㸪ᩘ್ᐇ㦂࡟ࡼ ࡾ㸪ᚑ᮶ࡢᤄධࢯ࣮ࢺ࡜ࡢຠ⋡ᛶࡢẚ㍑ホ౯ࢆ⾜࠺ࠋ➨㸵⠇ࡣࡲ࡜ࡵ࡜௒ᚋࡢㄢ㢟࡛࠶ࡿࠋ

㸰 ᫬㛫ィ⟬㔞࡜ࢯ࣮ࢺࡢᏳᐃᛶ

㸰㸯㸬࢔ࣝࢦࣜࢬ࣒ࡢ᫬㛫ィ⟬㔞

࢔ࣝࢦࣜࢬ࣒ࡢィ⟬㔞ࡣಶࠎࡢၥ㢟౛࡟ࡼࡗ࡚ኚ໬ࡍࡿࡢ࡛㸪඲యⓗ࡞ホ౯ࢆ⾜࠺࡟ࡣၥ㢟౛ࡢつ

㸱 ௦⾲ⓗ࡞ᩚิ࢔ࣝࢦࣜࢬ࣒࡜ࡑࡢ㛵ᩘᙧ

ᮏ⠇࡛ࡣ㸪௦⾲ⓗ࡞ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢཎ⌮㸪᫬㛫ィ⟬㔞࡟ࡼࡿホ౯㸪C ゝㄒ࡟ࡼࡿ㛵ᩘࣉࣟࢢ࣒ࣛ ࡢᐇ⿦౛࡟ࡘ࠸࡚㏙࡭ࡿࠋᮏㄽᩥ࡛ᢅ࠺࢔ࣝࢦࣜࢬ࣒ࡸࣉࣟࢢ࣒ࣛ࡟ඹ㏻ࡋ࡚࠸ࡿࡇ࡜ࡣ㸪ᩚิࡢᑐ ㇟࡜ࡋ࡚ࡢࢹ࣮ࢱࡣᩚᩘᆺ㸦㠀㈇ᩚᩘ್㸧࡛࠶ࡾ㸪ᑐ㇟࡜࡞ࡿ඲ࢹ࣮ࢱࡣ㓄ิD ࡟᱁⣡ࡉࢀ࡚࠸ࡿࡶ ࡢ࡜ࡋ࡚࠸ࡿⅬ࡛࠶ࡿࠋࡲࡓ㸪ࢹ࣮ࢱᩘࡣኚᩘ

n

ࢆ⏝࠸࡚࠾ࡾ㸪ࡑࡢࡓࡵᩚิ࢔ࣝࢦࣜࢬ࣒ࡢᑐ㇟ࡣ㸪 D[0], D[1],㺃㺃㺃, D[n-1]࡛࠶ࡿࠋࡇࢀࡽࡢࢹ࣮ࢱ࡟㸪࠶ࡿᩚิ࢔ࣝࢦࣜࢬ࣒ࡀ㐺⏝ࡉࢀࡿ࡜㸪㓄ิ D ࡢࢹ ࣮ࢱࡣ᪼㡰࡟୪࡭᭰࠼ࡽࢀࡿࡇ࡜࡟࡞ࡿࠋ

㸱㸯㸬

O(n

2

)

ࡢᩚิ࢔ࣝࢦࣜࢬ࣒

O(n

2

)

ࡢᩚิ࢔ࣝࢦࣜࢬ࣒࡜ࡋ࡚㸪ࣂࣈࣝࢯ࣮ࢺ㸪ᤄධࢯ࣮ࢺ㸪㑅ᢥࢯ࣮ࢺࡢ㡰࡟㏙࡭ࡿࠋࡑࢀࡒࢀ ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢᴫせ࡜C ゝㄒ࡟ࡼࡿ㛵ᩘᙧᘧࡢࣉࣟࢢ࣒ࣛࢆ♧ࡋ㸪ྛᩚิ࢔ࣝࢦ࣒ࣜࡢヲ⣽࡟ࡘ ࠸࡚ㄝ᫂ࡍࡿࠋ (1) ࣂࣈࣝࢯ࣮ࢺ ࣂࣈࣝࢯ࣮ࢺ (Bubble sort) ࡜ࡣ㸪ᚋ᪉࠿ࡽ๓᪉࡬㞄ྠኈࡢࢹ࣮ࢱࢆẚ㍑࣭஺᥮ࡍࡿࡇ࡜࡛ᑠࡉ࠸ࢹ ࣮ࢱࢆ㡰࡟๓࡟㏦ࡗ࡚࠸ࡃ᪉ἲ࡛࠶ࡾ㸪ࡇࢀࢆ⧞ࡾ㏉ࡍࡇ࡜࡟ࡼࡾ㸪ࢹ࣮ࢱ඲యࡀ᪼㡰㸦࠶ࡿ࠸ࡣ㝆 㡰㸧࡟ࢯ࣮ࢺࡉࢀࡿࠋࡇࡢ࢔ࣝࢦࣜࢬ࣒ࢆC ゝㄒ࡟ࡼࡾࣉࣟࢢ࣒ࣛ໬ࡍࡿ࡜௨ୗࡢࡼ࠺࡟࡞ࡿࠋ ࣉࣟࢢ࣒ࣛ1 ࣂࣈࣝࢯ࣮ࢺ 1 2 3 4 5 6 7 8 9 // --- ࣂࣈࣝࢯ࣮ࢺ --- // void Bubblesort(int D[], int n){

int i, j; for(i = 0 ; i < n-1 ; i++){ for(j = n-1 ; j > i ; j--){ if(D[j-1] > D[j]){ swap(D[j-1], D[j]); } } } } ୖࡢࣂࣈࣝࢯ࣮ࢺࡢࣉࣟࢢ࣒ࣛ࡟ࡘ࠸࡚ㄝ᫂ࡍࡿࠋfor ᩥࡢධࢀᏊᵓ㐀࡟ࡼࡾྑ➃࠿ࡽ㞄ྠኈࡢ್ࢆ ẚ㍑ࡋᕥഃ㸪ࡘࡲࡾ㸪ῧᏐࡢᑠࡉ࠸᪉ࡢ್ࡀ኱ࡁࡅࢀࡤswap 㛵ᩘࢆᐇ⾜ࡍࡿࠋswap(x,y)ࡣ㸪ኚᩘ x ࡜ y ࡢ್ࢆ஺᥮ࡍࡿ㛵ᩘ࡛࠶ࡿࠋࡇࢀࢆ⧞ࡾ㏉ࡍࡇ࡜࡛㸪ࢯ࣮ࢺࡉࢀ࡚࠸࡞࠸⠊ᅖࡢ᭱ᑠ್ࢆᕥ➃࡟⛣ື ࡉࡏࡿࡇ࡜ࡀฟ᮶ࡿࠋࡇࢀࢆఱᅇࡶ⧞ࡾ㏉ࡋ㸪ୖࡢࣉࣟࢢ࣒ࣛࡢ஧㔜ࡢfor ᩥࡀ⤊஢ࡍࡿ࡜㸪㓄ิ D ࡢ ࢹ࣮ࢱࡣ᪼㡰࡟୪࡭᭰࠼ࡽࢀࡓࡇ࡜࡟࡞ࡿࠋ ࣂࣈࣝࢯ࣮ࢺࡢ᫬㛫ィ⟬㔞࡟ࡘ࠸࡚⪃࠼ࡿࠋࣂࣈࣝࢯ࣮ࢺ༢యࡢ᫬㛫ィ⟬㔞ࢆ⪃࠼ࡿࡓࡵ㸪ࣉࣟࢢ ࣒ࣛ1 ࡢ for ᩥࡢືస࡟ὀ┠ࡍࡿࠋࣂࣈࣝࢯ࣮ࢺࡢሙྜ㸪ࢹ࣮ࢱࢆẚ㍑ࡋ㸪᪼㡰࡟ࡍࡿࡓࡵ࡟஺᥮ࡍࡿ ᧯సࡣ ߍሺͳሻ ࡛࠶ࡿࡓࡵ㸪᭱Ⰻ᫬㛫ィ⟬㔞࡜᭱ᝏ᫬㛫ィ⟬㔞ࡣ࡜ࡶ࡟ 2 ࡘࡢ for ᩥ࡟౫Ꮡࡍࡿࠋ4 ⾜┠ ࠿ࡽጞࡲࡿ ݅ ࡟㛵ࡍࡿ for ᩥࡣ ݊ െ ͳ ᅇᐇ⾜ࡉࢀ㸪5 ⾜┠࠿ࡽጞࡲࡿ ݆ ࡟㛵ࡍࡿ for ᩥࡣ ݊ െ ݅ ᅇᐇ ⾜ࡉࢀࡿࡓࡵ㸪᫬㛫ィ⟬㔞ࡣ ܶሺ݊ሻ ൌ ߍሺͳሻ ൈ ෍ ݇ ௡ିଵ ௞ୀଵ ൌ ܱሺͳሻ ൈ݊ሺ݊ െ ͳሻʹ ൌ ܱሺ݊ଶሻ ࡛࠶ࡿࠋྠᵝࡢ⌮⏤࡛㸪ᖹᆒ᫬㛫ィ⟬㔞ࡶ ߍሺ݊ሻ ࡛࠶ࡿࠋ ࡲࡓ㸪ࣉࣟࢢ࣒ࣛ1 ࡢ 6 ⾜┠ࡢ᮲௳ᩥ“ሾ݆ െ ͳሿ ൐ ሾ݆ሿ”࠿ࡽศ࠿ࡿࡼ࠺࡟㸪ࣂࣈࣝࢯ࣮ࢺࡣ㞄ྠኈࡢ ್ࡀྠࡌሙྜ࡟ධࢀ᭰࠼ࡣⓎ⏕ࡋ࡞࠸ࠋࡑࡢࡓࡵ㸪ᐃ⩏3 ࡢ᮲௳(b)ࢆ‶ࡓࡍࠋࡼࡗ࡚㸪ࣂࣈࣝࢯ࣮ࢺ ࡣᏳᐃ࡞ᩚิ࢔ࣝࢦࣜࢬ࣒࡛࠶ࡿ࡜ゝ࠼ࡿࠋ (2) ᤄධࢯ࣮ࢺ ᤄධࢯ࣮ࢺ (Insertion sort) ࡜ࡣ㸪᪤࡟ࢯ࣮ࢺࡉࢀࡓ㒊ศ࡟㸪᪂ࡓ࡞ࢹ࣮ࢱࢆࢹ࣮ࢱࡢ್ࡢ኱ࡁࡉ࡟ (2)

(5)

ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢẚ㍑ホ౯࡜஧ศ᥈⣴ᤄධࢯ࣮ࢺࡢᥦ᱌

Comparative Evaluation of Some Sorting Algorithms and Proposal of

Binary Search Insertion Sort

৿ਁ रҲ ʀߧ୫ ᴶߵ̏ʥ

Shuichi SHINMORI1)ː, Haruka ARATANI1)

 㮵ඣᓥ኱Ꮫ኱Ꮫ㝔⌮ᕤᏛ◊✲⛉ᩘ⌮᝟ሗ⛉Ꮫᑓᨷ

1)Graduate School of Science Engineering, Kagoshima University, Kagoshima 890-0065 * ੻೜ஸं e-mail address : [email protected]

Abstract: We studied on typical sorting algorithms, compared their efficiencies, and proposed and evaluated a new

sorting algorithm. In this paper, for some typical sorting algorithms, the principle and properties of each algorithm are described, and a function-type program in C language is given. Numerical experiments were used to compare and evaluate the execution times of the three O(n2) sorting algorithms (i.e., Bubble sort, Insertion sort and Selection sort)

and the three O(nlogn) sorting algorithms ( i.e., Quicksort, Heaposrt and Mergesort). Furthermore, we proposed a new algorithm called Binary search insertion sort, compared it with the conventional Insertion sort, and confirmed that it can sort at a high speed of about 68%.

Keywords: Sorting algorithm, Insertion sort, Quicksort, Data structure, Numerical experiments.

 ࡣࡌࡵ࡟

ᩚิ(sorting) ࡜ࡣ㸪඲㡰ᗎ㞟ྜࡢせ⣲࡛࠶ࡿ」ᩘࡢࢹ࣮ࢱࢆ㸪඲㡰ᗎࠕӌࠖ࡟ᚑࡗ࡚ᑠࡉ࠸ࢹ࣮ࢱ ࠿ࡽ኱ࡁ࠸ࢹ࣮ࢱ࡬࡜୪࡭᭰࠼ࡿࡇ࡜㸦᪼㡰࡜࠸࠺㸧㸪࠶ࡿ࠸ࡣ㏫࡟㸪኱ࡁ࠸ࢹ࣮ࢱ࠿ࡽᑠࡉ࠸ࢹ࣮ࢱ ࡟୪࡭᭰࠼ࡿࡇ࡜㸦㝆㡰࡜࠸࠺㸧ࢆ࠸࠸㸪ࢯ࣮ࢺ㸪ࢯ࣮ࢸ࢕ࣥࢢ࡜ࡶ࠸࠺ࠋࢡ࢖ࢵࢡࢯ࣮ࢺ㸪ࣂࣈࣝࢯ ࣮ࢺ㸪ᤄධࢯ࣮ࢺ࡞࡝㸪ᩚิࡢࡓࡵࡢᩘከࡃࡢ࢔ࣝࢦࣜࢬ࣒ࡀ◊✲ࡉࢀ࡚࠸ࡿࡀ㸪ࡇࢀࡽࢆ⥲⛠ࡋ࡚ᩚ ิ࢔ࣝࢦࣜࢬ࣒࡜࠸࠺㸦[14], [15]㸧ࠋᡃࠎࡢ㌟㏆࡞࡜ࡇࢁ࡟ࡣ㸪ᵝࠎ࡞ከࡃࡢࢹ࣮ࢱࡀᏑᅾࡍࡿࠋࡇࢀ ࡽࡢࢹ࣮ࢱࡢ୰࡟ࡣ㸪඲㡰ᗎӌ࡛㡰ᗎ௜ࡅࡽࢀࡿࡶࡢࡀᩘከࡃ࠶ࡿࠋ౛࠼ࡤ㸪ᩚᩘࡸᐇᩘࡢᩘ್ࢹ࣮ࢱ ࡛࠶ࢀࡤ኱ᑠ㡰㸪᪥ᮏㄒࡸⱥㄒࡢ༢ㄒ࡞࡝ࡢᩥᏐࢹ࣮ࢱ࡛࠶ࢀࡤ㸪஬༑㡢㡰ࡸ࢔ࣝࣇ࢓࣋ࢵࢺ㡰࡞࡝ ࡢ㎡᭩ᘧ㡰ᗎ࡛኱ᑠ㛵ಀࡀ୚࠼ࡽࢀࡿࠋࢹ࣮ࢱࢆຠ⋡Ⰻࡃᩚิࡉࡏࡿ᪉ἲࡣࢹ࣮ࢱࢆ᥈⣴ࡍࡿ᪉ἲ࡞ ࡝࡜୪ࢇ࡛ᇶᮏⓗ࡞ၥ㢟࡛࠶ࡾ㸪௨๓࠿ࡽከࡃࡢ◊✲ࡀ࡞ࡉࢀ࡚࠸ࡿ㸦౛࠼ࡤ㸪[1], [2], [3], [6], [12]㸧ࠋ ᮏㄽᩥ࡛ࡣ㸪ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢ୰࡛ࡶ௦⾲ⓗ࡞ 6 ✀㢮ࡢ࢔ࣝࢦࣜࢬ࣒࡟ὀ┠ࡋ㸪ྛ࢔ࣝࢦࣜࢬ࣒ ࡢᴫせࡢㄝ᫂࡜ࡑࡢC ゝㄒ࡟ࡼࡿ㛵ᩘᙧ㸦ࣉࣟࢢ࣒ࣛ㸧ࡸ࢔ࣝࢦࣜࢬ࣒ࡢ⌮ㄽⓗ࡞ᛶ㉁ࢆ♧ࡋ㸪ᐇ㝿 ࡟኱つᶍ࡞ᩘ್ࢹ࣮ࢱ࡟ᑐࡋ࡚㸪ࡇࢀࡽࡢࣉࣟࢢ࣒ࣛࢆᐇ⾜ࡉࡏࡿᩘ್ᐇ㦂࡟ࡼࡾྛᩚิ࢔ࣝࢦࣜࢬ ࣒ࡢຠ⋡ᛶࡢホ౯ࢆ⾜࠺ࠋᚋ༙࡛ࡣ㸪ᤄධࢯ࣮ࢺ࡜ࡇࢀࢆᨵⰋࡋࡓ஧ศ᥈⣴ᤄධࢯ࣮ࢺࢆᥦ᱌ࡋ㸪C ゝ ㄒ࡛ࡢࣉࣟࢢ࣒ࣛ໬ࡢ᪉ἲ㸪ᐇ㝿ࡢࢹ࣮ࢱࢆ⏝࠸ࡓᩘ್ᐇ㦂࡟ࡼࡿຠ⋡ᛶࡢホ౯ࢆ⾜࠺ࠋ ➨ 2 ⠇࡛ࡣ㸪࢔ࣝࢦࣜࢬ࣒ࡢホ౯᪉ἲ࡛࠶ࡿ᫬㛫ィ⟬㔞࡜ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢᏳᐃᛶ࡟ࡘ࠸࡚㏙࡭ ࡿࠋ➨3㸪4 ⠇࡛ࡣ㸪௦⾲ⓗ࡞ 6 ✀㢮ࡢᩚิ࢔ࣝࢦࣜࢬ࣒࡟ὀ┠ࡋ㸪ࡇࢀࡽࢆ᫬㛫ィ⟬㔞࡟ࡼࡾ 2 ࡘࡢ ࢢ࣮ࣝࣉ࡟ศ㢮ࡍࡿࠋࡑࡋ࡚㸪ྛᩚิ࢔ࣝࢦࣜࢬ࣒ࡢᴫせࡢㄝ᫂㸪C ゝㄒࢆ⏝࠸࡚ࡢࣉࣟࢢ࣒ࣛ໬㸪ᩘ ್ᐇ㦂࡟ࡼࡿຠ⋡ᛶࡢẚ㍑ホ౯ࢆ⾜࠺ࠋ➨5㸪6 ⠇࡛ࡣ㸪஧ศ᥈⣴ᤄධࢯ࣮ࢺࢆᥦ᱌ࡋ㸪ᩘ್ᐇ㦂࡟ࡼ ࡾ㸪ᚑ᮶ࡢᤄධࢯ࣮ࢺ࡜ࡢຠ⋡ᛶࡢẚ㍑ホ౯ࢆ⾜࠺ࠋ➨㸵⠇ࡣࡲ࡜ࡵ࡜௒ᚋࡢㄢ㢟࡛࠶ࡿࠋ

㸰 ᫬㛫ィ⟬㔞࡜ࢯ࣮ࢺࡢᏳᐃᛶ

㸰㸯㸬࢔ࣝࢦࣜࢬ࣒ࡢ᫬㛫ィ⟬㔞

࢔ࣝࢦࣜࢬ࣒ࡢィ⟬㔞ࡣಶࠎࡢၥ㢟౛࡟ࡼࡗ࡚ኚ໬ࡍࡿࡢ࡛㸪඲యⓗ࡞ホ౯ࢆ⾜࠺࡟ࡣၥ㢟౛ࡢつ ᛂࡌࡓ㐺ษ࡞ሙᡤࢆぢࡘࡅ࡚ࡑࡇ࡬ࢹ࣮ࢱࢆᤄධࡋ࡚࠸ࡃ᪉ἲ࡛࠶ࡾ㸪ࡇࢀࢆ᭱ᚋᑿࡲ࡛⧞ࡾ㏉ࡍ࡜ ࢯ࣮ࢺࡀ᏶஢ࡍࡿࠋࣉࣟࢢ࣒࡛ࣛグ㏙ࡍࡿ࡜㸪௨ୗࡢࡼ࠺࡟࡞ࡿࠋ ࣉࣟࢢ࣒ࣛ2 ᤄධࢯ࣮ࢺ 1 2 3 4 5 6 7 8 9 10 11 12 // --- ᤄධࢯ࣮ࢺ --- // void Insertsort(int D[], int n){

int i, j, x; for(i = 1 ; i < n ; i++){ x = D[i]; j = i; while ((D[j-1] > x) && (j > 0)){ D[j] = D[j-1]; j = j-1; } D[j] = x; } return; } ࣉࣟࢢ࣒ࣛ2 ࡟ࡘ࠸࡚ㄝ᫂ࡍࡿࠋ4 ⾜┠࡛ ݅ ൌ ͳ ࡜ึᮇ໬ࡋ㸪݅ ൏ ݊ ࡀᡂ❧ࡋ࡞ࡃ࡞ࡿࡲ࡛ ݅ ࢆ࢖ ࣥࢡ࣓ࣜࣥࢺࡋ࡞ࡀࡽfor ᩥࢆᐇ⾜ࡍࡿࠋ5 ⾜┠࡛᪂ࡋ࠸ࢹ࣮ࢱࢆ ݔ ൌ ሾ݅ሿ㸪݆ ൌ ݅ ࡜ࡋ㸪ḟࡢ while ᩥ ࡟⛣ືࡍࡿࠋሾ݆ െ ͳሿ ൐ ݔ ࠿ࡘ ݆ ൐ Ͳ ࡢሙྜ㸪7 ⾜┠ࢆᐇ⾜ࡋ࡚ ሾŒ െ ͳሿ ࡢ್ࢆྑ࡟ࡎࡽࡋ㸪݆ ࢆࢹ ࢕ࢡ࣓ࣜࣥࢺࡉࡏࡿࠋࡑࢀ௨እࡢሙྜ㸪ࡘࡲࡾ㸪ᤄධࡍࡿ್ ݔ ࡀẚ㍑ࡍࡿ್ࡼࡾ኱ࡁ࠸ሙྜ㸪ࡲࡓࡣ ሾͲሿ ࡼࡾࡶ್ࡀᑠࡉ࠸ሙྜ࡟㸪ࢹ࣮ࢱࡢ᱁⣡ࡉࢀࡿሙᡤࡀ D[j]࡛࠶ࡿࡢ࡛㸪9 ⾜┠ࢆᐇ⾜ࡍࡿࠋ ᤄධࢯ࣮ࢺࡢ᫬㛫ィ⟬㔞࡟ࡘ࠸࡚⪃࠼ࡿࠋࣉࣟࢢ࣒ࣛ2 ࡣ for ᩥ࡜ࡑࢀ࡟ྵࡲࢀࡿ while ᩥ࡛ᵓᡂࡉ ࢀ࡚࠾ࡾ㸪ධຊ࡟ࡼࡗ࡚᫬㛫ィ⟬㔞ࡣኚ໬ࡍࡿࠋ᭱ࡶ࢔ࣝࢦࣜࢬ࣒ࡢືసࡀ㧗㏿࡞ሙྜࡣ㸪୚࠼ࡽࢀࡓ ࢹ࣮ࢱࡀ᪤࡟ࢯ࣮ࢺࡉࢀ࡚࠸ࡿሙྜ࡛࠶ࡿࠋࡇࡢሙྜ㸪6 ⾜┠ࡢ ሾ݆ െ ͳሿ ൐ ݔ ࡜࠸࠺᮲௳ࡣ୍ᗘࡶᡂ ❧ࡋ࡞࠸ࠋࡋࡓࡀࡗ࡚㸪while ᩥࡢ୰ࡢฎ⌮ࡣ୍ᗘࡶᐇ⾜ࡉࢀࡎ㸪for ᩥࡢฎ⌮ࡣ ݊ െ ͳ ᅇࡋ࠿ᐇ⾜ࡉ ࢀ࡞࠸ࡇ࡜࡟࡞ࡿࠋfor ᩥࡢ୰ࡢ᧯సࡣ㸪ᩘᅇࡢẚ㍑࣭௦ධ᧯సࡢࡳ࡛࠶ࡾ㸪ߍሺͳሻ ࡜࡞ࡿࠋࡼࡗ࡚㸪᭱ Ⰻ᫬㛫ィ⟬㔞ࡣḟࡢࡼ࠺࡟࡞ࡿࠋ ܶሺ݊ሻ ൌ ሺ݊ െ ͳሻ ൈ ߍሺͳሻ ൌ ߍሺ݊ሻ ୍᪉㸪࢔ࣝࢦࣜࢬ࣒ࡀ᭱ࡶ㐜࠸ሙྜࡣ㸪ධຊࢹ࣮ࢱࡀ㝆㡰࡟ࢯ࣮ࢺࡉࢀ࡚࠸ࡿሙྜ࡛࠶ࡿࠋࡇࡢሙ ྜ㸪ᤄධࡍࡿࢹ࣮ࢱࡢ᱁⣡ሙᡤࡣᖖ࡟㓄ิࡢᕥ➃࡜࡞ࡾ㸪 ݅ ൌ ݇ ࡢሙྜ㸪while ᩥࡣ ݇ ᅇ⧞ࡾ㏉ࡉࢀ ࡿࠋwhile ᩥࡢ୰ࡢ᧯సࡶᩘᅇࡢẚ㍑࣭௦ධ᧯సࡢࡳ࡛࠶ࡾ㸪 ߍሺͳሻ ࡜࡞ࡿࠋୖࡢ᭱Ⰻ᫬㛫ィ⟬㔞࡛㏙ ࡭ࡓࡼ࠺࡟㸪while ᩥࢆ㝖࠸ࡓ for ᩥࡢ᫬㛫ィ⟬㔞ࡣ ߍሺ݊ሻ ࡛࠶ࡿࡢ࡛㸪᭱ᝏ᫬㛫ィ⟬㔞ࡣ ܶሺ݊ሻ ൌ ߍሺͳሻ ൈ ෍ ݇ ௡ ௞ୀଵ ൅ ߍሺ݊ሻ ൌ ܱሺ݊ଶ ࡛࠶ࡿࠋࡲࡓ㸪ド᫂ࡣ┬␎ࡍࡿࡀ㸪ᖹᆒ᫬㛫ィ⟬㔞ࡶ ߍሺ݊ሻ ࡛࠶ࡿࡇ࡜ࡀศ࠿ࡗ࡚࠸ࡿࠋ ᤄධࢯ࣮ࢺࡢᏳᐃᛶࡣ㸪ྠࡌ್ࡀྵࡲࢀ࡚࠸ࡿ᫬ࡣ㸪ධຊࡢ㡰ᗎ࡝࠾ࡾ࡟୪࡭᭰࠼ࡽࢀࡿࡓࡵᡂ❧ ࡋ࡚࠸ࡿࠋࡇࢀࡣ㸪ࣉࣟࢢ࣒ࣛ2 ࡢ 6 ⾜┠ࡀ㛵ಀࡋ࡚࠸ࡿࠋሾ݆ െ ͳሿ ൐ ݔ ࡢሙྜࡣ್ࢆྑ࡟ࡎࡽࡋ㸪ࡑ ࢀ௨እࡢሙྜࡣ ሾ݆ሿ ࡟್ࢆᤄධ㸪ࡘࡲࡾ㸪ሾ݆ െ ͳሿ ൌ ݔ ࡢሙྜࡣ ሾ݆ െ ͳሿ ࡢᚋ࡟ ݔ ࢆᤄධࡍࡿࠋ௨ ୖ࠿ࡽ㸪ᐃ⩏3 ࡢ᮲௳(b)ࢆ‶ࡓࡍࡓࡵ㸪ᤄධࢯ࣮ࢺࡣᏳᐃ࡞ᩚิ࢔ࣝࢦࣜࢬ࣒࡛࠶ࡿ࡜ゝ࠼ࡿࠋ (3) 㑅ᢥࢯ࣮ࢺ 㑅ᢥࢯ࣮ࢺ(Selection sort) ࡜ࡣ㸪ධຊࢹ࣮ࢱ࠿ࡽ᭱኱್ࢆぢࡘࡅ࡚᭱ᚋᑿ࡬㸪ṧࡾࡢࢹ࣮ࢱ࡟ᑐࡋ࡚ ࡶྠᵝࡢ᧯సࢆ⧞ࡾ㏉ࡋ࡚ࢯ࣮ࢺࡍࡿ᪉ἲ࡛࠶ࡿࠋ㑅ᢥࢯ࣮ࢺࡢ࢔ࣝࢦࣜࢬ࣒ࢆࣉࣟࢢ࣒࡛ࣛグ㏙ࡍ ࡿ࡜௨ୗࡢࡼ࠺࡟࡞ࡿࠋ ࣉࣟࢢ࣒ࣛ3 㑅ᢥࢯ࣮ࢺ 1 2 3 // --- 㑅ᢥࢯ࣮ࢺ --- // void Selectionsort (int D[] , int n){

int max, max_index, i, j;

(3)

(6)

ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢẚ㍑ホ౯࡜஧ศ᥈⣴ᤄධࢯ࣮ࢺࡢᥦ᱌

Comparative Evaluation of Some Sorting Algorithms and Proposal of

Binary Search Insertion Sort

৿ਁ रҲ ʀߧ୫ ᴶߵ̏ʥ

Shuichi SHINMORI1)ː, Haruka ARATANI1)

 㮵ඣᓥ኱Ꮫ኱Ꮫ㝔⌮ᕤᏛ◊✲⛉ᩘ⌮᝟ሗ⛉Ꮫᑓᨷ

1)Graduate School of Science Engineering, Kagoshima University, Kagoshima 890-0065 * ੻೜ஸं e-mail address : [email protected]

Abstract: We studied on typical sorting algorithms, compared their efficiencies, and proposed and evaluated a new

sorting algorithm. In this paper, for some typical sorting algorithms, the principle and properties of each algorithm are described, and a function-type program in C language is given. Numerical experiments were used to compare and evaluate the execution times of the three O(n2) sorting algorithms (i.e., Bubble sort, Insertion sort and Selection sort)

and the three O(nlogn) sorting algorithms ( i.e., Quicksort, Heaposrt and Mergesort). Furthermore, we proposed a new algorithm called Binary search insertion sort, compared it with the conventional Insertion sort, and confirmed that it can sort at a high speed of about 68%.

Keywords: Sorting algorithm, Insertion sort, Quicksort, Data structure, Numerical experiments.

 ࡣࡌࡵ࡟

ᩚิ(sorting) ࡜ࡣ㸪඲㡰ᗎ㞟ྜࡢせ⣲࡛࠶ࡿ」ᩘࡢࢹ࣮ࢱࢆ㸪඲㡰ᗎࠕӌࠖ࡟ᚑࡗ࡚ᑠࡉ࠸ࢹ࣮ࢱ ࠿ࡽ኱ࡁ࠸ࢹ࣮ࢱ࡬࡜୪࡭᭰࠼ࡿࡇ࡜㸦᪼㡰࡜࠸࠺㸧㸪࠶ࡿ࠸ࡣ㏫࡟㸪኱ࡁ࠸ࢹ࣮ࢱ࠿ࡽᑠࡉ࠸ࢹ࣮ࢱ ࡟୪࡭᭰࠼ࡿࡇ࡜㸦㝆㡰࡜࠸࠺㸧ࢆ࠸࠸㸪ࢯ࣮ࢺ㸪ࢯ࣮ࢸ࢕ࣥࢢ࡜ࡶ࠸࠺ࠋࢡ࢖ࢵࢡࢯ࣮ࢺ㸪ࣂࣈࣝࢯ ࣮ࢺ㸪ᤄධࢯ࣮ࢺ࡞࡝㸪ᩚิࡢࡓࡵࡢᩘከࡃࡢ࢔ࣝࢦࣜࢬ࣒ࡀ◊✲ࡉࢀ࡚࠸ࡿࡀ㸪ࡇࢀࡽࢆ⥲⛠ࡋ࡚ᩚ ิ࢔ࣝࢦࣜࢬ࣒࡜࠸࠺㸦[14], [15]㸧ࠋᡃࠎࡢ㌟㏆࡞࡜ࡇࢁ࡟ࡣ㸪ᵝࠎ࡞ከࡃࡢࢹ࣮ࢱࡀᏑᅾࡍࡿࠋࡇࢀ ࡽࡢࢹ࣮ࢱࡢ୰࡟ࡣ㸪඲㡰ᗎӌ࡛㡰ᗎ௜ࡅࡽࢀࡿࡶࡢࡀᩘከࡃ࠶ࡿࠋ౛࠼ࡤ㸪ᩚᩘࡸᐇᩘࡢᩘ್ࢹ࣮ࢱ ࡛࠶ࢀࡤ኱ᑠ㡰㸪᪥ᮏㄒࡸⱥㄒࡢ༢ㄒ࡞࡝ࡢᩥᏐࢹ࣮ࢱ࡛࠶ࢀࡤ㸪஬༑㡢㡰ࡸ࢔ࣝࣇ࢓࣋ࢵࢺ㡰࡞࡝ ࡢ㎡᭩ᘧ㡰ᗎ࡛኱ᑠ㛵ಀࡀ୚࠼ࡽࢀࡿࠋࢹ࣮ࢱࢆຠ⋡Ⰻࡃᩚิࡉࡏࡿ᪉ἲࡣࢹ࣮ࢱࢆ᥈⣴ࡍࡿ᪉ἲ࡞ ࡝࡜୪ࢇ࡛ᇶᮏⓗ࡞ၥ㢟࡛࠶ࡾ㸪௨๓࠿ࡽከࡃࡢ◊✲ࡀ࡞ࡉࢀ࡚࠸ࡿ㸦౛࠼ࡤ㸪[1], [2], [3], [6], [12]㸧ࠋ ᮏㄽᩥ࡛ࡣ㸪ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢ୰࡛ࡶ௦⾲ⓗ࡞ 6 ✀㢮ࡢ࢔ࣝࢦࣜࢬ࣒࡟ὀ┠ࡋ㸪ྛ࢔ࣝࢦࣜࢬ࣒ ࡢᴫせࡢㄝ᫂࡜ࡑࡢC ゝㄒ࡟ࡼࡿ㛵ᩘᙧ㸦ࣉࣟࢢ࣒ࣛ㸧ࡸ࢔ࣝࢦࣜࢬ࣒ࡢ⌮ㄽⓗ࡞ᛶ㉁ࢆ♧ࡋ㸪ᐇ㝿 ࡟኱つᶍ࡞ᩘ್ࢹ࣮ࢱ࡟ᑐࡋ࡚㸪ࡇࢀࡽࡢࣉࣟࢢ࣒ࣛࢆᐇ⾜ࡉࡏࡿᩘ್ᐇ㦂࡟ࡼࡾྛᩚิ࢔ࣝࢦࣜࢬ ࣒ࡢຠ⋡ᛶࡢホ౯ࢆ⾜࠺ࠋᚋ༙࡛ࡣ㸪ᤄධࢯ࣮ࢺ࡜ࡇࢀࢆᨵⰋࡋࡓ஧ศ᥈⣴ᤄධࢯ࣮ࢺࢆᥦ᱌ࡋ㸪C ゝ ㄒ࡛ࡢࣉࣟࢢ࣒ࣛ໬ࡢ᪉ἲ㸪ᐇ㝿ࡢࢹ࣮ࢱࢆ⏝࠸ࡓᩘ್ᐇ㦂࡟ࡼࡿຠ⋡ᛶࡢホ౯ࢆ⾜࠺ࠋ ➨ 2 ⠇࡛ࡣ㸪࢔ࣝࢦࣜࢬ࣒ࡢホ౯᪉ἲ࡛࠶ࡿ᫬㛫ィ⟬㔞࡜ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢᏳᐃᛶ࡟ࡘ࠸࡚㏙࡭ ࡿࠋ➨3㸪4 ⠇࡛ࡣ㸪௦⾲ⓗ࡞ 6 ✀㢮ࡢᩚิ࢔ࣝࢦࣜࢬ࣒࡟ὀ┠ࡋ㸪ࡇࢀࡽࢆ᫬㛫ィ⟬㔞࡟ࡼࡾ 2 ࡘࡢ ࢢ࣮ࣝࣉ࡟ศ㢮ࡍࡿࠋࡑࡋ࡚㸪ྛᩚิ࢔ࣝࢦࣜࢬ࣒ࡢᴫせࡢㄝ᫂㸪C ゝㄒࢆ⏝࠸࡚ࡢࣉࣟࢢ࣒ࣛ໬㸪ᩘ ್ᐇ㦂࡟ࡼࡿຠ⋡ᛶࡢẚ㍑ホ౯ࢆ⾜࠺ࠋ➨5㸪6 ⠇࡛ࡣ㸪஧ศ᥈⣴ᤄධࢯ࣮ࢺࢆᥦ᱌ࡋ㸪ᩘ್ᐇ㦂࡟ࡼ ࡾ㸪ᚑ᮶ࡢᤄධࢯ࣮ࢺ࡜ࡢຠ⋡ᛶࡢẚ㍑ホ౯ࢆ⾜࠺ࠋ➨㸵⠇ࡣࡲ࡜ࡵ࡜௒ᚋࡢㄢ㢟࡛࠶ࡿࠋ

㸰 ᫬㛫ィ⟬㔞࡜ࢯ࣮ࢺࡢᏳᐃᛶ

㸰㸯㸬࢔ࣝࢦࣜࢬ࣒ࡢ᫬㛫ィ⟬㔞

࢔ࣝࢦࣜࢬ࣒ࡢィ⟬㔞ࡣಶࠎࡢၥ㢟౛࡟ࡼࡗ࡚ኚ໬ࡍࡿࡢ࡛㸪඲యⓗ࡞ホ౯ࢆ⾜࠺࡟ࡣၥ㢟౛ࡢつ 4 5 6 7 8 9 10 11 12 13 14 for(i = n-1 ; i > 0 ; i--){ max = D[0]; max_index = 0; for(j = 1 ; j <= i ; j++){ if(D[j] >= max){ max = D[j]; max_index = j; } } swap(D[max_index], D[i]); } return; } ࣉࣟࢢ࣒ࣛ3 ࡟㛵ࡋ࡚ㄝ᫂ࡍࡿࠋࢧ࢖ࢬ ݊ ࡢ㓄ิ D ࢆࢯ࣮ࢺࡍࡿࡓࡵ㸪㛵ᩘࡢ௬ᘬᩘࡣ㓄ิ D ࡜ ࢹ࣮ࢱࡢಶᩘ ݊ ࡛࠶ࡿࠋ4 ⾜┠࡛ ݅ ൌ ݊ െ ͳ ࡜ึᮇ໬ࡋ㸪݅ ൐ Ͳ ࡀᡂ❧ࡋ࡞ࡃ࡞ࡿࡲ࡛ ݅ ࢆࢹ࢕ࢡࣜ ࣓ࣥࢺࡋ࡞ࡀࡽfor ᩥࡢ୰㌟ࢆᐇ⾜ࡉࡏࡿࠋ5 ⾜┠࡛ ݉ܽݔ ൌ ሾͲሿ㸪݉ܽݔ௜௡ௗ௘௫ ൌ Ͳ ࡜ࡋ㸪6 ⾜┠ࡢ for ᩥ ࡟⛣ືࡍࡿࠋ݆ ൌ ͳ ࡜ึᮇ໬ࡋ㸪݆ ൑ ݅ ࡀᡂ❧ࡋ࡞ࡃ࡞ࡿࡲ࡛ ݆ ࢆ࢖ࣥࢡ࣓ࣜࣥࢺࡋ࡞ࡀࡽ for ᩥࡢ୰ ㌟ࢆᐇ⾜ࡉࡏࡿࠋ7 ⾜┠࡛ ሾ݆ሿ ࡜ ݉ܽݔ ࢆẚ㍑ࡋ㸪ሾ݆ሿ ൒ ݉ܽݔ ࡢሙྜࡣ 8 ⾜┠࡛ ݉ܽݔ ൌ ሾ݆ሿ㸪 ݉ܽݔ௜௡ௗ௘௫ ൌ ݆ ࢆᐇ⾜ࡍࡿࠋෆഃࡢ for ᩥࢆᐇ⾜ࡋ㸪ࢯ࣮ࢺࡉࢀ࡚࠸࡞࠸㓄ิࡢ୰࡛᭱኱್ࢆぢࡘࡅࡓ ࡽ㸪11 ⾜┠ࡢ swap 㛵ᩘ࡛㸪᭱኱್࡜ࢯ࣮ࢺࡉࢀ࡚࠸࡞࠸㓄ิࡢྑ➃ࡢ್ࢆ஺᥮ࡍࡿࠋࡇࢀࢆ ݅ ൐ Ͳ ࡀᡂ❧ࡋ࡞ࡃ࡞ࡿࡲ࡛⧞ࡾ㏉ࡍࡇ࡜࡟ࡼࡾ㸪㑅ᢥࢯ࣮ࢺࡣ᏶஢ࡍࡿࠋ 㑅ᢥࢯ࣮ࢺࡢ᫬㛫ィ⟬㔞࡟ࡘ࠸࡚⪃࠼ࡿࠋࣉࣟࢢ࣒ࣛ 3 ࡣ㸪ධຊ࡟ࡼࡗ࡚࢔ࣝࢦࣜࢬ࣒ࡢᐇ⾜᫬㛫 㸦ẚ㍑ᅇᩘ㸧ࡀኚ໬ࡋ࡞࠸ࡓࡵ㸪᭱Ⰻ᫬㛫ィ⟬㔞࡜᭱ᝏ᫬㛫ィ⟬㔞ࡣ➼ࡋ࠸ࠋ࡜࠸࠺ࡢࡣ㸪᭱኱್ࢆ᥈ ⣴ࡍࡿ㝿࡟㸪ࢯ࣮ࢺࡉࢀ࡚࠸࡞࠸㓄ิࢆ඲࡚ᬻᐃࡢ᭱኱್࡜ᚲࡎẚ㍑ࡍࡿࡓࡵ࡛࠶ࡿࠋ ࡛ࡣ㸪᫬㛫ィ⟬㔞࡟ࡘ࠸࡚⪃࠼࡚࠸ࡃࠋࣉࣟࢢ࣒ࣛ3 ࡣ஧㔜ࡢ for ᩥ࡟ࡼࡾᵓᡂࡉࢀ࡚࠸ࡿࠋእഃࡢ for ᩥࡢ⧞ࡾ㏉ࡋᅇᩘࡣ ݊ െ ͳ ᅇ࡛࠶ࡾ㸪ෆഃࡢ for ᩥࡣእഃࡢ for ᩥࡢኚᩘ ݅ ࡟౫Ꮡࡋ㸪݇ ൌ ݅ ࡢሙ ྜ㸪for ᩥࡣ ݇ ᅇ⧞ࡾ㏉ࡉࢀࡿࠋྛ for ᩥࡢ୰ࡢ᧯సࡣ㸪ᩘᅇࡢẚ㍑࣭௦ධ᧯సࡢࡳ࡛࠶ࡾ㸪ߍሺͳሻ ࡜ ࡞ࡿࠋࡼࡗ࡚㸪᫬㛫ィ⟬㔞ࡣḟࡢࡼ࠺࡟࡞ࡿࠋ ܶሺ݊ሻ ൌ ෍ ݇ ൈ ߍሺͳሻ ൌ ܱሺͳሻ ൈ݊ሺ݊ െ ͳሻʹ ௡ିଵ ௞ୀଵ ൌ ܱሺ݊ଶ ḟ࡟㸪㑅ᢥࢯ࣮ࢺࡢᏳᐃᛶ࡟ࡘ࠸࡚ㄝ᫂ࡍࡿࠋ㑅ᢥࢯ࣮ࢺࡣ᭱኱್࡜ྑ➃ࡢ್ࢆ஺᥮ࡍࡿࡓࡵ㸪ࢹ࣮ ࢱࡢ㡰ᗎࡣࣂࣛࣂࣛ࡟࡞ࡿࠋࡼࡗ࡚㸪୍⯡ⓗ࡟ࡣ㑅ᢥࢯ࣮ࢺࡣᏳᐃᛶࡢ᮲௳ࢆ‶ࡓࡋ࡚࠸࡞࠸ࡢ࡛㸪Ᏻ ᐃ࡞ࢯ࣮ࢺ࡟ࡍࡿࡓࡵ࡟ࡣᕤኵࡀᚲせ࡜࡞ࡿࠋ

.㸰㸬

O(nlog n)

ࡢᩚิ࢔ࣝࢦࣜࢬ࣒

O(n

log n

)

ࡢᩚิ࢔ࣝࢦࣜࢬ࣒࡜ࡋ࡚㸪ࢡ࢖ࢵࢡࢯ࣮ࢺ㸦[2]㸧㸪ࣄ࣮ࣉࢯ࣮ࢺ㸦[3]㸧㸪࣐࣮ࢪࢯ࣮ࢺ 㸦[7]㸧ࡢ㡰࡟㏙࡭ࡿࠋࡑࢀࡒࢀᩚิ࢔ࣝࢦࣜࢬ࣒ࡢᴫせ࡜ C ゝㄒ࡟ࡼࡿ㛵ᩘᙧᘧࡢࣉࣟࢢ࣒ࣛࢆ♧ࡋ㸪 ྛᩚิ࢔ࣝࢦ࣒ࣜࡢヲ⣽࡟ࡘ࠸࡚ㄝ᫂ࡍࡿࠋ (1) ࢡ࢖ࢵࢡࢯ࣮ࢺ ࢡ࢖ࢵࢡࢯ࣮ࢺ(Quick sort)࡜ࡣ㸪࠶ࡿࢹ࣮ࢱࢆᇶ‽್(ࣆ࣎ࢵࢺ)࡜ࡋ㸪ᑐ㇟࡜࡞ࡿࢹ࣮ࢱ㞟ྜࢆᇶ‽ ್ࡼࡾ኱ࡁ࠸ࢢ࣮ࣝࣉ࡜ᑠࡉ࠸ࢢ࣮ࣝࣉ࡟ศ㢮ࡋ㸪ྛࢢ࣮ࣝࣉ࡟ࡘ࠸࡚ྠᵝࡢ᧯సࢆ⧞ࡾ㏉ࡍࡇ࡜࡟ ࡼࡗ࡚ࢯ࣮ࢺࡍࡿ᪉ἲ࡛࠶ࡾ㸪ࡇࢀࢆྛࢢ࣮ࣝࣉ࡟෌ᖐⓗ࡟ᐇ⾜ࡍࡿࡇ࡜࡟ࡼࡾࢯ࣮ࢺࡀ᏶஢ࡍࡿࠋ ࡇࡢ෌ᖐⓗ࡞㒊ศࢆᐇ⌧ࡍࡿࣉࣟࢢ࣒ࣛࡣ௨ୗࡢࡼ࠺࡟࡞ࡿࠋ ࣉࣟࢢ࣒ࣛ4-1 ࢡ࢖ࢵࢡࢯ࣮ࢺࡢ෌ᖐ㒊ศ 1 2 3 // --- Quick sort 㛵ᩘ --- // void Quicksort(int D[], int left, int right){

int pivot_index;

(7)

ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢẚ㍑ホ౯࡜஧ศ᥈⣴ᤄධࢯ࣮ࢺࡢᥦ᱌

Comparative Evaluation of Some Sorting Algorithms and Proposal of

Binary Search Insertion Sort

৿ਁ रҲ ʀߧ୫ ᴶߵ̏ʥ

Shuichi SHINMORI1)ː, Haruka ARATANI1)

 㮵ඣᓥ኱Ꮫ኱Ꮫ㝔⌮ᕤᏛ◊✲⛉ᩘ⌮᝟ሗ⛉Ꮫᑓᨷ

1)Graduate School of Science Engineering, Kagoshima University, Kagoshima 890-0065 * ੻೜ஸं e-mail address : [email protected]

Abstract: We studied on typical sorting algorithms, compared their efficiencies, and proposed and evaluated a new

sorting algorithm. In this paper, for some typical sorting algorithms, the principle and properties of each algorithm are described, and a function-type program in C language is given. Numerical experiments were used to compare and evaluate the execution times of the three O(n2) sorting algorithms (i.e., Bubble sort, Insertion sort and Selection sort)

and the three O(nlogn) sorting algorithms ( i.e., Quicksort, Heaposrt and Mergesort). Furthermore, we proposed a new algorithm called Binary search insertion sort, compared it with the conventional Insertion sort, and confirmed that it can sort at a high speed of about 68%.

Keywords: Sorting algorithm, Insertion sort, Quicksort, Data structure, Numerical experiments.

 ࡣࡌࡵ࡟

ᩚิ(sorting) ࡜ࡣ㸪඲㡰ᗎ㞟ྜࡢせ⣲࡛࠶ࡿ」ᩘࡢࢹ࣮ࢱࢆ㸪඲㡰ᗎࠕӌࠖ࡟ᚑࡗ࡚ᑠࡉ࠸ࢹ࣮ࢱ ࠿ࡽ኱ࡁ࠸ࢹ࣮ࢱ࡬࡜୪࡭᭰࠼ࡿࡇ࡜㸦᪼㡰࡜࠸࠺㸧㸪࠶ࡿ࠸ࡣ㏫࡟㸪኱ࡁ࠸ࢹ࣮ࢱ࠿ࡽᑠࡉ࠸ࢹ࣮ࢱ ࡟୪࡭᭰࠼ࡿࡇ࡜㸦㝆㡰࡜࠸࠺㸧ࢆ࠸࠸㸪ࢯ࣮ࢺ㸪ࢯ࣮ࢸ࢕ࣥࢢ࡜ࡶ࠸࠺ࠋࢡ࢖ࢵࢡࢯ࣮ࢺ㸪ࣂࣈࣝࢯ ࣮ࢺ㸪ᤄධࢯ࣮ࢺ࡞࡝㸪ᩚิࡢࡓࡵࡢᩘከࡃࡢ࢔ࣝࢦࣜࢬ࣒ࡀ◊✲ࡉࢀ࡚࠸ࡿࡀ㸪ࡇࢀࡽࢆ⥲⛠ࡋ࡚ᩚ ิ࢔ࣝࢦࣜࢬ࣒࡜࠸࠺㸦[14], [15]㸧ࠋᡃࠎࡢ㌟㏆࡞࡜ࡇࢁ࡟ࡣ㸪ᵝࠎ࡞ከࡃࡢࢹ࣮ࢱࡀᏑᅾࡍࡿࠋࡇࢀ ࡽࡢࢹ࣮ࢱࡢ୰࡟ࡣ㸪඲㡰ᗎӌ࡛㡰ᗎ௜ࡅࡽࢀࡿࡶࡢࡀᩘከࡃ࠶ࡿࠋ౛࠼ࡤ㸪ᩚᩘࡸᐇᩘࡢᩘ್ࢹ࣮ࢱ ࡛࠶ࢀࡤ኱ᑠ㡰㸪᪥ᮏㄒࡸⱥㄒࡢ༢ㄒ࡞࡝ࡢᩥᏐࢹ࣮ࢱ࡛࠶ࢀࡤ㸪஬༑㡢㡰ࡸ࢔ࣝࣇ࢓࣋ࢵࢺ㡰࡞࡝ ࡢ㎡᭩ᘧ㡰ᗎ࡛኱ᑠ㛵ಀࡀ୚࠼ࡽࢀࡿࠋࢹ࣮ࢱࢆຠ⋡Ⰻࡃᩚิࡉࡏࡿ᪉ἲࡣࢹ࣮ࢱࢆ᥈⣴ࡍࡿ᪉ἲ࡞ ࡝࡜୪ࢇ࡛ᇶᮏⓗ࡞ၥ㢟࡛࠶ࡾ㸪௨๓࠿ࡽከࡃࡢ◊✲ࡀ࡞ࡉࢀ࡚࠸ࡿ㸦౛࠼ࡤ㸪[1], [2], [3], [6], [12]㸧ࠋ ᮏㄽᩥ࡛ࡣ㸪ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢ୰࡛ࡶ௦⾲ⓗ࡞ 6 ✀㢮ࡢ࢔ࣝࢦࣜࢬ࣒࡟ὀ┠ࡋ㸪ྛ࢔ࣝࢦࣜࢬ࣒ ࡢᴫせࡢㄝ᫂࡜ࡑࡢC ゝㄒ࡟ࡼࡿ㛵ᩘᙧ㸦ࣉࣟࢢ࣒ࣛ㸧ࡸ࢔ࣝࢦࣜࢬ࣒ࡢ⌮ㄽⓗ࡞ᛶ㉁ࢆ♧ࡋ㸪ᐇ㝿 ࡟኱つᶍ࡞ᩘ್ࢹ࣮ࢱ࡟ᑐࡋ࡚㸪ࡇࢀࡽࡢࣉࣟࢢ࣒ࣛࢆᐇ⾜ࡉࡏࡿᩘ್ᐇ㦂࡟ࡼࡾྛᩚิ࢔ࣝࢦࣜࢬ ࣒ࡢຠ⋡ᛶࡢホ౯ࢆ⾜࠺ࠋᚋ༙࡛ࡣ㸪ᤄධࢯ࣮ࢺ࡜ࡇࢀࢆᨵⰋࡋࡓ஧ศ᥈⣴ᤄධࢯ࣮ࢺࢆᥦ᱌ࡋ㸪C ゝ ㄒ࡛ࡢࣉࣟࢢ࣒ࣛ໬ࡢ᪉ἲ㸪ᐇ㝿ࡢࢹ࣮ࢱࢆ⏝࠸ࡓᩘ್ᐇ㦂࡟ࡼࡿຠ⋡ᛶࡢホ౯ࢆ⾜࠺ࠋ ➨ 2 ⠇࡛ࡣ㸪࢔ࣝࢦࣜࢬ࣒ࡢホ౯᪉ἲ࡛࠶ࡿ᫬㛫ィ⟬㔞࡜ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢᏳᐃᛶ࡟ࡘ࠸࡚㏙࡭ ࡿࠋ➨3㸪4 ⠇࡛ࡣ㸪௦⾲ⓗ࡞ 6 ✀㢮ࡢᩚิ࢔ࣝࢦࣜࢬ࣒࡟ὀ┠ࡋ㸪ࡇࢀࡽࢆ᫬㛫ィ⟬㔞࡟ࡼࡾ 2 ࡘࡢ ࢢ࣮ࣝࣉ࡟ศ㢮ࡍࡿࠋࡑࡋ࡚㸪ྛᩚิ࢔ࣝࢦࣜࢬ࣒ࡢᴫせࡢㄝ᫂㸪C ゝㄒࢆ⏝࠸࡚ࡢࣉࣟࢢ࣒ࣛ໬㸪ᩘ ್ᐇ㦂࡟ࡼࡿຠ⋡ᛶࡢẚ㍑ホ౯ࢆ⾜࠺ࠋ➨5㸪6 ⠇࡛ࡣ㸪஧ศ᥈⣴ᤄධࢯ࣮ࢺࢆᥦ᱌ࡋ㸪ᩘ್ᐇ㦂࡟ࡼ ࡾ㸪ᚑ᮶ࡢᤄධࢯ࣮ࢺ࡜ࡢຠ⋡ᛶࡢẚ㍑ホ౯ࢆ⾜࠺ࠋ➨㸵⠇ࡣࡲ࡜ࡵ࡜௒ᚋࡢㄢ㢟࡛࠶ࡿࠋ

㸰 ᫬㛫ィ⟬㔞࡜ࢯ࣮ࢺࡢᏳᐃᛶ

㸰㸯㸬࢔ࣝࢦࣜࢬ࣒ࡢ᫬㛫ィ⟬㔞

࢔ࣝࢦࣜࢬ࣒ࡢィ⟬㔞ࡣಶࠎࡢၥ㢟౛࡟ࡼࡗ࡚ኚ໬ࡍࡿࡢ࡛㸪඲యⓗ࡞ホ౯ࢆ⾜࠺࡟ࡣၥ㢟౛ࡢつ 4 5 6 7 8

if(left >= right) return;

pivot_index = partition(D, left, right); Quicksort(D, left, pivot_index - 1); Quicksort(D, pivot_index + 1, right); } ࣉࣟࢢ࣒ࣛ4-1 ࢆㄝ᫂ࡍࡿࠋࡲࡎ㸪㓄ิ D ࡢᕥ➃ࡢῧᏐࢆ left㸪ྑ➃ࡢῧᏐࢆ right ࡜タᐃࡋ࡚࠸ࡿࠋ 4 ⾜┠࡛ left ࡜ right ࡢ್ࢆẚ㍑ࡋ㸪އˆ– ൒ ”‹‰Š– ࡘࡲࡾ㸪㓄ิ D ࡟ྵࡲࢀࡿ್ࡀ 1 ࡘ௨ୗࡢሙྜࡣ࢔ࣝ ࢦࣜࢬ࣒⤊஢ࡍࡿࠋ5 ⾜┠ࡢ partition 㛵ᩘ࡛ᇶ‽್ࡀ᱁⣡ࡉࢀ࡚࠸ࡿ㓄ิࡢῧᏐ pivot_index ࢆ⟬ฟࡍ ࡿࠋpartition 㛵ᩘෆ࡛ࡣ㸪ᇶ‽್ࢆỴᐃࡍࡿ᧯స㸪㓄ิ D ࢆ 2 ࡘ࡟ศࡅࡿ᧯సࢆ⾜ࡗ࡚࠸ࡿࡀ㸪ヲࡋࡃ ࡣᚋ㏙ࡍࡿࠋpartition 㛵ᩘෆ࡛㓄ิ D ࢆᇶ‽್ᮍ‶ࡢࢹ࣮ࢱࡢ㞟ྜ  㸪ᇶ‽್௨ୖࡢࢹ࣮ࢱࡢ㞟ྜ ଶ ࡟ศࡅࡿࡇ࡜ࡀ࡛ࡁࡿࡓࡵ㸪ࡇࢀࢆྛࠎ6, 7 ⾜┠࡛෌ᖐⓗ࡟ᐇ⾜ࡉࡏ࡚࠸ࡿࠋ ࢡ࢖ࢵࢡࢯ࣮ࢺࡣ㸪ḟ࡟ヲ㏙ࡍࡿ partition 㛵ᩘࡀ㔜せ࡞ᙺ๭ࢆᣢࡗ࡚࠾ࡾ㸪ࡇࡢ㛵ᩘࡢࣉࣟࢢ࣒ࣛ ࡣ௨ୗࡢࡼ࠺࡟࡞ࡿࠋ ࣉࣟࢢ࣒ࣛ4-2 partition 㛵ᩘ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 // --- partition 㛵ᩘ --- // int partition(int D[], int left, int right){

int k, i, j;

k = rand() % (right - left + 1) + left; swap(D[k],D[right]);

i = left; j = right - 1; while(i <= j){

while(D[i] < D[right]) i++;

while((D[j] >= D[right]) && (j>=i)) j--; if(i < j){ swap(D[i],D[j]);} } swap(D[i],D[right]); return i; } ࣉࣟࢢ࣒ࣛ4-2 ࡟ࡘ࠸࡚ㄝ᫂ࡍࡿࠋ4 ⾜┠࡛ ሾ݈݂݁ݐሿ ࠿ࡽ ሾݎ݄݅݃ݐሿ ࡢ㛫࡛ᇶ‽್ࢆࣛࣥࢲ࣒࡟Ỵࡵ ࡿࠋ5 ⾜┠࡛ᇶ‽್࡜ྑ➃ࡢ್ࢆධࢀ᭰࠼㸪6 ⾜┠࡛ ݅ ࡜ ݆ ࡢ್ࢆึᮇ໬ࡍࡿࠋ7 ⾜┠࠿ࡽ 11 ⾜┠ࡢ while ᩥ࡛ᇶ‽್ᮍ‶ࡢࢹ࣮ࢱࢆᕥ࡟㸪ᇶ‽್௨ୖࡢࢹ࣮ࢱࢆྑ࡟⛣ືࡉࡏࡿࠋ݅ ൐ ݆ ࡜࡞ࡗ࡚ while ᩥ ࢆᢤࡅฟࡋ㸪12 ⾜┠࡛ ሾ݅ሿ㸪ࡘࡲࡾᇶ‽್௨ୖࡢࢹ࣮ࢱࡢ㞟ྜࡢᕥ➃࡜ᇶ‽್ࡀ᱁⣡ࡉࢀ࡚࠸ࡿ ሾݎ݄݅݃ݐሿ ࢆ஺᥮ࡍࡿࠋ13 ⾜┠࡛ᇶ‽್ࡀ᱁⣡ࡉࢀ࡚࠸ࡿῧᏐ ݅ ࢆ Quicksort 㛵ᩘ࡟㏉ࡍࠋ ࢡ࢖ࢵࢡࢯ࣮ࢺࡢ᫬㛫ィ⟬㔞࡟ࡘ࠸࡚⪃࠼ࡿࠋࡲࡎ㸪partition 㛵ᩘࡢ㓄ิࡢࢧ࢖ࢬࢆ ሺݎ݄݅݃ݐ െ ݈݂݁ݐ ൅ ͳሻ ൌ ݊ ࡜௬ᐃࡍࡿࠋࡇࡢ㛵ᩘࡣ୺࡟ 2 㔜ࡢ while ᩥ࡛ᵓᡂࡉࢀ࡚࠸ࡿࡀ㸪እഃࡢ while ᩥࡢ⧞ࡾ㏉ࡋ ᅇᩘ࡟㛵ࢃࡽࡎ㸪ෆഃࡢ2 ࡘࡢ while ᩥࡢ⧞ࡾ㏉ࡋᅇᩘࡣྜࢃࡏ࡚᭱኱ ݊ െ ͳ ᅇ࡛࠶ࡿࠋ࡞ࡐ࡞ࡽ㸪 ᭱ึ ݊ െ ͳ ࡔࡅ㞳ࢀ࡚࠸ࡿ ݅ ࡜ ݆ ࡣෆഃࡢ while ᩥࢆᐇ⾜ࡍࡿࡓࡧ࡟ 1 ࡘࡎࡘ㏆࡙ࡁ㸪᭱኱ ݊ െ ͳ ᅇࡢᐇ⾜࡟ࡼࡾ ݅ ൐ ݆ ࡜࡞ࡾ㸪እഃࡢ while ᩥࡢ⤊஢᮲௳ࡀ‶ࡓࡉࢀࡿ࠿ࡽ࡛࠶ࡿࠋwhile ᩥ௨እࡢ᧯ సࡣᐃᩘ᫬㛫࡛ᐇ⾜ྍ⬟࡞ࡢ࡛㸪partition 㛵ᩘࡢ᭱኱᫬㛫ィ⟬㔞ࡣ ܶሺ݊ሻ ൌ ߍሺͳሻ ή ሺ݊ െ ͳሻ ൌ ߍሺ݊ሻ ࡜࡞ࡿࠋ௨ୗ㸪᫬㛫ィ⟬㔞ࢆィ⟬ࡋࡸࡍࡃࡍࡿࡓࡵ㸪ࢧ࢖ࢬ ݊ ࡢሙྜࡢ partition 㛵ᩘࡢ᫬㛫ィ⟬㔞ࡣ㸪 ᐃᩘ ܿ ࢆ⏝࠸࡚ ߍሺ݊ሻ ൌ ܿ݊ ࡜ࡍࡿࠋ ḟ࡟㸪ࢡ࢖ࢵࢡࢯ࣮ࢺࡢ෌ᖐࢆ㋃ࡲ࠼࡚᫬㛫ィ⟬㔞ࢆ⪃࠼ࡿࠋࢡ࢖ࢵࢡࢯ࣮ࢺࡣ㸪ᇶ‽್ࡢ㑅ࡧ᪉࡟ ࡼࡗ࡚᫬㛫ィ⟬㔞ࡀኚ໬ࡍࡿࠋ࢔ࣝࢦࣜࢬ࣒ࡀ᭱ࡶ㐜࠸ሙྜࡣ㸪ᇶ‽್࡜ࡋ࡚ᖖ࡟᭱ᑠ್ࡶࡋࡃࡣ᭱ ኱್ࡀ㑅ࡤࢀࡿሙྜ࡛࠶ࡿࠋࡇࡢ࡜ࡁ㸪㓄ิD ࡣ“ᇶ‽್࡜ᇶ‽್௨ୖࡢࢹ࣮ࢱ”ࡶࡋࡃࡣ“ᇶ‽್࡜ᇶ ‽್௨ୗࡢࢹ࣮ࢱ”࡟ศ๭ࡉࢀࡿࠋ௒ᅇࡣ㸪ᇶ‽್࡟ẖᅇ᭱ᑠ್ࡀ㑅ࡤࢀࡿሙྜࢆ⪃࠼ࡿࠋヲ⣽ࡣ┬␎ ࡍࡿࡀ㸪ࡇࡢሙྜࡢ෌ᖐᮌࢆᵓ⠏ࡋ㸪෌ᖐᮌ࠿ࡽ᫬㛫ィ⟬㔞ࢆồࡵࡿ࡜෌ᖐᮌࡢ㧗ࡉࡣ ݊ ࡛࠶ࡾ㸪⠇ (6)

(8)

ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢẚ㍑ホ౯࡜஧ศ᥈⣴ᤄධࢯ࣮ࢺࡢᥦ᱌

Comparative Evaluation of Some Sorting Algorithms and Proposal of

Binary Search Insertion Sort

৿ਁ रҲ ʀߧ୫ ᴶߵ̏ʥ

Shuichi SHINMORI1)ː, Haruka ARATANI1)

 㮵ඣᓥ኱Ꮫ኱Ꮫ㝔⌮ᕤᏛ◊✲⛉ᩘ⌮᝟ሗ⛉Ꮫᑓᨷ

1)Graduate School of Science Engineering, Kagoshima University, Kagoshima 890-0065 * ੻೜ஸं e-mail address : [email protected]

Abstract: We studied on typical sorting algorithms, compared their efficiencies, and proposed and evaluated a new

sorting algorithm. In this paper, for some typical sorting algorithms, the principle and properties of each algorithm are described, and a function-type program in C language is given. Numerical experiments were used to compare and evaluate the execution times of the three O(n2) sorting algorithms (i.e., Bubble sort, Insertion sort and Selection sort)

and the three O(nlogn) sorting algorithms ( i.e., Quicksort, Heaposrt and Mergesort). Furthermore, we proposed a new algorithm called Binary search insertion sort, compared it with the conventional Insertion sort, and confirmed that it can sort at a high speed of about 68%.

Keywords: Sorting algorithm, Insertion sort, Quicksort, Data structure, Numerical experiments.

 ࡣࡌࡵ࡟

ᩚิ(sorting) ࡜ࡣ㸪඲㡰ᗎ㞟ྜࡢせ⣲࡛࠶ࡿ」ᩘࡢࢹ࣮ࢱࢆ㸪඲㡰ᗎࠕӌࠖ࡟ᚑࡗ࡚ᑠࡉ࠸ࢹ࣮ࢱ ࠿ࡽ኱ࡁ࠸ࢹ࣮ࢱ࡬࡜୪࡭᭰࠼ࡿࡇ࡜㸦᪼㡰࡜࠸࠺㸧㸪࠶ࡿ࠸ࡣ㏫࡟㸪኱ࡁ࠸ࢹ࣮ࢱ࠿ࡽᑠࡉ࠸ࢹ࣮ࢱ ࡟୪࡭᭰࠼ࡿࡇ࡜㸦㝆㡰࡜࠸࠺㸧ࢆ࠸࠸㸪ࢯ࣮ࢺ㸪ࢯ࣮ࢸ࢕ࣥࢢ࡜ࡶ࠸࠺ࠋࢡ࢖ࢵࢡࢯ࣮ࢺ㸪ࣂࣈࣝࢯ ࣮ࢺ㸪ᤄධࢯ࣮ࢺ࡞࡝㸪ᩚิࡢࡓࡵࡢᩘከࡃࡢ࢔ࣝࢦࣜࢬ࣒ࡀ◊✲ࡉࢀ࡚࠸ࡿࡀ㸪ࡇࢀࡽࢆ⥲⛠ࡋ࡚ᩚ ิ࢔ࣝࢦࣜࢬ࣒࡜࠸࠺㸦[14], [15]㸧ࠋᡃࠎࡢ㌟㏆࡞࡜ࡇࢁ࡟ࡣ㸪ᵝࠎ࡞ከࡃࡢࢹ࣮ࢱࡀᏑᅾࡍࡿࠋࡇࢀ ࡽࡢࢹ࣮ࢱࡢ୰࡟ࡣ㸪඲㡰ᗎӌ࡛㡰ᗎ௜ࡅࡽࢀࡿࡶࡢࡀᩘከࡃ࠶ࡿࠋ౛࠼ࡤ㸪ᩚᩘࡸᐇᩘࡢᩘ್ࢹ࣮ࢱ ࡛࠶ࢀࡤ኱ᑠ㡰㸪᪥ᮏㄒࡸⱥㄒࡢ༢ㄒ࡞࡝ࡢᩥᏐࢹ࣮ࢱ࡛࠶ࢀࡤ㸪஬༑㡢㡰ࡸ࢔ࣝࣇ࢓࣋ࢵࢺ㡰࡞࡝ ࡢ㎡᭩ᘧ㡰ᗎ࡛኱ᑠ㛵ಀࡀ୚࠼ࡽࢀࡿࠋࢹ࣮ࢱࢆຠ⋡Ⰻࡃᩚิࡉࡏࡿ᪉ἲࡣࢹ࣮ࢱࢆ᥈⣴ࡍࡿ᪉ἲ࡞ ࡝࡜୪ࢇ࡛ᇶᮏⓗ࡞ၥ㢟࡛࠶ࡾ㸪௨๓࠿ࡽከࡃࡢ◊✲ࡀ࡞ࡉࢀ࡚࠸ࡿ㸦౛࠼ࡤ㸪[1], [2], [3], [6], [12]㸧ࠋ ᮏㄽᩥ࡛ࡣ㸪ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢ୰࡛ࡶ௦⾲ⓗ࡞ 6 ✀㢮ࡢ࢔ࣝࢦࣜࢬ࣒࡟ὀ┠ࡋ㸪ྛ࢔ࣝࢦࣜࢬ࣒ ࡢᴫせࡢㄝ᫂࡜ࡑࡢC ゝㄒ࡟ࡼࡿ㛵ᩘᙧ㸦ࣉࣟࢢ࣒ࣛ㸧ࡸ࢔ࣝࢦࣜࢬ࣒ࡢ⌮ㄽⓗ࡞ᛶ㉁ࢆ♧ࡋ㸪ᐇ㝿 ࡟኱つᶍ࡞ᩘ್ࢹ࣮ࢱ࡟ᑐࡋ࡚㸪ࡇࢀࡽࡢࣉࣟࢢ࣒ࣛࢆᐇ⾜ࡉࡏࡿᩘ್ᐇ㦂࡟ࡼࡾྛᩚิ࢔ࣝࢦࣜࢬ ࣒ࡢຠ⋡ᛶࡢホ౯ࢆ⾜࠺ࠋᚋ༙࡛ࡣ㸪ᤄධࢯ࣮ࢺ࡜ࡇࢀࢆᨵⰋࡋࡓ஧ศ᥈⣴ᤄධࢯ࣮ࢺࢆᥦ᱌ࡋ㸪C ゝ ㄒ࡛ࡢࣉࣟࢢ࣒ࣛ໬ࡢ᪉ἲ㸪ᐇ㝿ࡢࢹ࣮ࢱࢆ⏝࠸ࡓᩘ್ᐇ㦂࡟ࡼࡿຠ⋡ᛶࡢホ౯ࢆ⾜࠺ࠋ ➨ 2 ⠇࡛ࡣ㸪࢔ࣝࢦࣜࢬ࣒ࡢホ౯᪉ἲ࡛࠶ࡿ᫬㛫ィ⟬㔞࡜ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢᏳᐃᛶ࡟ࡘ࠸࡚㏙࡭ ࡿࠋ➨3㸪4 ⠇࡛ࡣ㸪௦⾲ⓗ࡞ 6 ✀㢮ࡢᩚิ࢔ࣝࢦࣜࢬ࣒࡟ὀ┠ࡋ㸪ࡇࢀࡽࢆ᫬㛫ィ⟬㔞࡟ࡼࡾ 2 ࡘࡢ ࢢ࣮ࣝࣉ࡟ศ㢮ࡍࡿࠋࡑࡋ࡚㸪ྛᩚิ࢔ࣝࢦࣜࢬ࣒ࡢᴫせࡢㄝ᫂㸪C ゝㄒࢆ⏝࠸࡚ࡢࣉࣟࢢ࣒ࣛ໬㸪ᩘ ್ᐇ㦂࡟ࡼࡿຠ⋡ᛶࡢẚ㍑ホ౯ࢆ⾜࠺ࠋ➨5㸪6 ⠇࡛ࡣ㸪஧ศ᥈⣴ᤄධࢯ࣮ࢺࢆᥦ᱌ࡋ㸪ᩘ್ᐇ㦂࡟ࡼ ࡾ㸪ᚑ᮶ࡢᤄධࢯ࣮ࢺ࡜ࡢຠ⋡ᛶࡢẚ㍑ホ౯ࢆ⾜࠺ࠋ➨㸵⠇ࡣࡲ࡜ࡵ࡜௒ᚋࡢㄢ㢟࡛࠶ࡿࠋ

㸰 ᫬㛫ィ⟬㔞࡜ࢯ࣮ࢺࡢᏳᐃᛶ

㸰㸯㸬࢔ࣝࢦࣜࢬ࣒ࡢ᫬㛫ィ⟬㔞

࢔ࣝࢦࣜࢬ࣒ࡢィ⟬㔞ࡣಶࠎࡢၥ㢟౛࡟ࡼࡗ࡚ኚ໬ࡍࡿࡢ࡛㸪඲యⓗ࡞ホ౯ࢆ⾜࠺࡟ࡣၥ㢟౛ࡢつ Ⅼࡢ᫬㛫ィ⟬㔞ࡣ ܿ݊ǡ ܿሺ݊ െ ͳሻǡ ڮ ǡ ܿ ࡜࡞ࡗ࡚࠸ࡿࠋ෌ᖐ࢔ࣝࢦࣜࢬ࣒ࡢ᫬㛫ィ⟬㔞ࡣ㸪෌ᖐᮌࡢࡍ࡭ ࡚ࡢ⠇Ⅼࡀ⾲ࡍ᫬㛫ィ⟬㔞ࡢ࿴࡟➼ࡋ࠸ࡓࡵ㸪ࢡ࢖ࢵࢡࢯ࣮ࢺࡢ᭱ᝏ᫬㛫ィ⟬㔞ࡣḟࡢࡼ࠺࡟࡞ࡿࠋ ܶሺ݊ሻ ൌ ෍ ܿሺ݊ െ ݅ሻ ൌ ෍ ܿ݅ ௡ ௜ୀଵ ൌ ܿ݊ሺ݊ ൅ ͳሻʹ ൌ ߍሺ݊ଶ ௡ିଵ ௜ୀ଴ ୍᪉㸪᭱ࡶ࢔ࣝࢦࣜࢬ࣒ࡢືసࡀ㧗㏿࡞ሙྜࡣ㸪ᇶ‽್࡜ࡋ࡚ᖖ࡟୰ኸ್ࡀ㑅ࡤࢀࡿሙྜ࡛࠶ࡿࠋࡇ ࡢሙྜ㸪“ᇶ‽್ᮍ‶ࡢࢹ࣮ࢱ”࡜“ᇶ‽್௨ୖࡢࢹ࣮ࢱ”ࡢࢧ࢖ࢬࡣ࡯ࡰ➼ࡋࡃ࡞ࡾ㸪ᆒ➼࡟ศ๭ࡉࢀࡿࠋ ࡇࡢሙྜࡢ෌ᖐᮌࢆᵓ⠏ࡍࡿ࡜㸪ⴥࡢᩘࡣࢹ࣮ࢱᩘ ݊ ࡛࠶ࡾ㸪ᮌࡢ㧗ࡉࡣ ߍሺŽ‘‰ ݊ሻ ࡛࠶ࡿࠋࡲࡓ㸪 ෌ᖐᮌࡢྛࣞ࣋ࣝࡢ⠇Ⅼ࡟ྵࡲࢀࡿ᫬㛫ィ⟬㔞ࡢ࿴ࡣࡍ࡭࡚ ܿ݊ ࡛࠶ࡿࠋࡼࡗ࡚㸪᭱Ⰻ᫬㛫ィ⟬㔞ࡣ ܶሺ݊ሻ ൌ ܿ݊ ή ߍሺŽ‘‰ ݊ሻ ൌ ߍሺ݊ Ž‘‰ ݊ሻ ࡛࠶ࡿࠋࡲࡓ㸪݊ ಶࡢࢹ࣮ࢱ࠿ࡽᇶ‽್ࢆ㑅ࡪ᪉ἲࡣࣛࣥࢲ࣒࡞ࡓࡵ㸪ᇶ‽್ࡀྛῧᏐ࡟᱁⣡ࡉࢀࡿ ☜⋡ࡣ ͳ ݊Τ  ࡛࠶ࡿࠋࡇࡢࡇ࡜࠿ࡽ㸪ᖹᆒ᫬㛫ィ⟬㔞ࡶ ߍሺ݊ Ž‘‰ ݊ሻ ࡛࠶ࡿࡇ࡜ࡀド᫂ࡉࢀ࡚࠸ࡿࠋ ࢡ࢖ࢵࢡࢯ࣮ࢺࡢᏳᐃᛶ࡟ࡘ࠸࡚⪃࠼ࡿࠋࢡ࢖ࢵࢡࢯ࣮ࢺࡢሙྜ㸪ࢹ࣮ࢱࡀ࡝ࡢ఩⨨࡟᱁⣡ࡉࢀ࡚ ࠸ࡿ࠿ࢆ⪃៖ࡏࡎ࡟஺᥮ࡢ᧯సࢆ⾜࠺ࡓࡵ㸪ྠࡌ್ࡢࢹ࣮ࢱࡀධຊࡢ㡰ᗎ࡝࠾ࡾ࡟୪࡭᭰ࡽࢀࡿ࡜ࡣ ࠸࠼࡞࠸ࠋࡑࡢࡓࡵ㸪୍⯡ⓗ࡟ࢡ࢖ࢵࢡࢯ࣮ࢺࡣᏳᐃᛶࡢ᮲௳ࢆ‶ࡓࡋ࡚࠸࡞࠸ࡢ࡛㸪Ᏻᐃ࡞ࢯ࣮ࢺ࡟ ࡍࡿࡓࡵ࡟ࡣᕤኵࡀᚲせ࡜࡞ࡿࠋ (2) ࣄ࣮ࣉࢯ࣮ࢺ ࣄ࣮ࣉࢯ࣮ࢺ(Heapsort)࡜ࡣ㸪2 ศᮌࡢྛ⠇Ⅼ࡟ࣄ࣮ࣉࡢ᮲௳ࢆ‶ࡓࡍࡼ࠺࡟ࢹ࣮ࢱࢆ᱁⣡ࡋ㸪᰿࡟ ࠶ࡿ᭱኱್ࡢࢹ࣮ࢱࢆྲྀࡾฟࡋࡓᚋ㸪ࣄ࣮ࣉࡢ෌ᵓᡂࢆ⾜࠺ࠋࡇࢀࢆ⧞ࡾ㏉ࡍࡇ࡜࡟ࡼࡾࢯ࣮ࢺࡍࡿ ᪉ἲ࡛࠶ࡿࠋࣄ࣮ࣉ࡜ࡣࢹ࣮ࢱᵓ㐀ࡢࡦ࡜ࡘ࡛㸪௨ୗࡢࡼ࠺࡟ᐃ⩏ࡉࢀࡿࠋ ᐃ⩏4 㸦ࣄ࣮ࣉ㸧 ḟࡢ 2 ࡘࡢ᮲௳ࢆ‶ࡓࡍ 2 ศᮌࢆࣄ࣮ࣉ࡜࿧ࡪࠋ (a) 2 ศᮌࡢ᭱኱ࡢࣞ࣋ࣝࢆ ݈௠ ࡜ࡍࡿ࡜㸪Ͳ ൑ ݇ ൑ ݈௠െ ͳ ࢆ‶ࡓࡍྛࣞ࣋ࣝ ݇ ࡟ࡣ㸪ʹ௞ ಶࡢ⠇ ⅬࡀᏑᅾࡋ㸪ࣞ࣋ࣝ ݈௠࡟Ꮡᅾࡍࡿⴥࡣࡑࡢࣞ࣋ࣝ࡟ᕥワࡵࡉࢀ࡚࠸ࡿࠋ (b) ྛ⠇Ⅼ࡟ಖᏑࡉࢀ࡚࠸ࡿࢹ࣮ࢱࡣ㸪ࡑࡢᏊ࡟ಖᏑࡉࢀࡿࢹ࣮ࢱࡼࡾ኱ࡁ࠸ࠋ ᐃ⩏4 ࡟ࡋࡓࡀࡗ࡚ 2 ศᮌ࡟ࢹ࣮ࢱࢆಖᏑࡋ࡚࠸ࡃ࡜㸪ᚲࡎ᰿࡟᭱኱್ࡀ᱁⣡ࡉࢀࡿࡇ࡜࡟࡞ࡿࠋ ࡇࡢࣄ࣮ࣉࢆ฼⏝ࡋ㸪௨ୗࡢ2 ࡘࡢ㛵ᩘࢆ⏝࠸ࡿࡇ࡜࡛ࢯ࣮ࢺࡀ᏶஢ࡍࡿࠋ (a) push_heap 㛵ᩘ 㓄ิ࡟᱁⣡ࡉࢀࡓ ݊ ಶࡢࢹ࣮ࢱ࡟ࡘ࠸࡚㸪ᐃ⩏ 4 (b) ࡢ᮲௳ࢆ‶ࡓࡍࡼ࠺࡟୪࡭᭰࠼ࡢ᧯స ࢆ⾜࠸㸪ࣄ࣮ࣉࡢᛶ㉁ࢆ‶ࡓࡍ2 ศᮌࢆసᡂࡍࡿࠋ (b) delete_maximum 㛵ᩘ (a) ࡛సᡂࡉࢀࡓࣄ࣮ࣉࢆ⾲ࡍ 2 ศᮌ࡟ᑐࡋ࡚㸪᰿ࡢ್ࡢฟຊ࡜ࣄ࣮ࣉࡢ෌ᵓᡂࢆ ݊ ᅇ⧞ࡾ ㏉ࡋ㸪ࢹ࣮ࢱࢆྲྀࡾฟࡋࡓ㡰࡟୪࡭ࡿࠋ ୖࡢ࢔ࣝࢦࣜࢬ࣒ࢆࣉࣟࢢ࣒࡛ࣛグ㏙ࡍࡿ࡜௨ୗࡢࡼ࠺࡟࡞ࡿࠋ ࣉࣟࢢ࣒ࣛ5-1 ࣄ࣮ࣉࢯ࣮ࢺ 1 2 3 4 5 6 7 8 9 10 // --- Heapsort 㛵ᩘ --- // void Heapsort(int D[], int n){

int i, *T; // ࣏࢖ࣥࢱᆺࡢኚᩘࢆసᡂ (int ᆺ) T = (int*)malloc(sizeof(int)*n+1);

push_heap(D, T, n); for(i = n-1 ; i >= 0 ; i--){

D[i] = delete_maximum(T, i+1); }

free(T); }

(7)

(9)

ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢẚ㍑ホ౯࡜஧ศ᥈⣴ᤄධࢯ࣮ࢺࡢᥦ᱌

Comparative Evaluation of Some Sorting Algorithms and Proposal of

Binary Search Insertion Sort

৿ਁ रҲ ʀߧ୫ ᴶߵ̏ʥ

Shuichi SHINMORI1)ː, Haruka ARATANI1)

 㮵ඣᓥ኱Ꮫ኱Ꮫ㝔⌮ᕤᏛ◊✲⛉ᩘ⌮᝟ሗ⛉Ꮫᑓᨷ

1)Graduate School of Science Engineering, Kagoshima University, Kagoshima 890-0065 * ੻೜ஸं e-mail address : [email protected]

Abstract: We studied on typical sorting algorithms, compared their efficiencies, and proposed and evaluated a new

sorting algorithm. In this paper, for some typical sorting algorithms, the principle and properties of each algorithm are described, and a function-type program in C language is given. Numerical experiments were used to compare and evaluate the execution times of the three O(n2) sorting algorithms (i.e., Bubble sort, Insertion sort and Selection sort)

and the three O(nlogn) sorting algorithms ( i.e., Quicksort, Heaposrt and Mergesort). Furthermore, we proposed a new algorithm called Binary search insertion sort, compared it with the conventional Insertion sort, and confirmed that it can sort at a high speed of about 68%.

Keywords: Sorting algorithm, Insertion sort, Quicksort, Data structure, Numerical experiments.

 ࡣࡌࡵ࡟

ᩚิ(sorting) ࡜ࡣ㸪඲㡰ᗎ㞟ྜࡢせ⣲࡛࠶ࡿ」ᩘࡢࢹ࣮ࢱࢆ㸪඲㡰ᗎࠕӌࠖ࡟ᚑࡗ࡚ᑠࡉ࠸ࢹ࣮ࢱ ࠿ࡽ኱ࡁ࠸ࢹ࣮ࢱ࡬࡜୪࡭᭰࠼ࡿࡇ࡜㸦᪼㡰࡜࠸࠺㸧㸪࠶ࡿ࠸ࡣ㏫࡟㸪኱ࡁ࠸ࢹ࣮ࢱ࠿ࡽᑠࡉ࠸ࢹ࣮ࢱ ࡟୪࡭᭰࠼ࡿࡇ࡜㸦㝆㡰࡜࠸࠺㸧ࢆ࠸࠸㸪ࢯ࣮ࢺ㸪ࢯ࣮ࢸ࢕ࣥࢢ࡜ࡶ࠸࠺ࠋࢡ࢖ࢵࢡࢯ࣮ࢺ㸪ࣂࣈࣝࢯ ࣮ࢺ㸪ᤄධࢯ࣮ࢺ࡞࡝㸪ᩚิࡢࡓࡵࡢᩘከࡃࡢ࢔ࣝࢦࣜࢬ࣒ࡀ◊✲ࡉࢀ࡚࠸ࡿࡀ㸪ࡇࢀࡽࢆ⥲⛠ࡋ࡚ᩚ ิ࢔ࣝࢦࣜࢬ࣒࡜࠸࠺㸦[14], [15]㸧ࠋᡃࠎࡢ㌟㏆࡞࡜ࡇࢁ࡟ࡣ㸪ᵝࠎ࡞ከࡃࡢࢹ࣮ࢱࡀᏑᅾࡍࡿࠋࡇࢀ ࡽࡢࢹ࣮ࢱࡢ୰࡟ࡣ㸪඲㡰ᗎӌ࡛㡰ᗎ௜ࡅࡽࢀࡿࡶࡢࡀᩘከࡃ࠶ࡿࠋ౛࠼ࡤ㸪ᩚᩘࡸᐇᩘࡢᩘ್ࢹ࣮ࢱ ࡛࠶ࢀࡤ኱ᑠ㡰㸪᪥ᮏㄒࡸⱥㄒࡢ༢ㄒ࡞࡝ࡢᩥᏐࢹ࣮ࢱ࡛࠶ࢀࡤ㸪஬༑㡢㡰ࡸ࢔ࣝࣇ࢓࣋ࢵࢺ㡰࡞࡝ ࡢ㎡᭩ᘧ㡰ᗎ࡛኱ᑠ㛵ಀࡀ୚࠼ࡽࢀࡿࠋࢹ࣮ࢱࢆຠ⋡Ⰻࡃᩚิࡉࡏࡿ᪉ἲࡣࢹ࣮ࢱࢆ᥈⣴ࡍࡿ᪉ἲ࡞ ࡝࡜୪ࢇ࡛ᇶᮏⓗ࡞ၥ㢟࡛࠶ࡾ㸪௨๓࠿ࡽከࡃࡢ◊✲ࡀ࡞ࡉࢀ࡚࠸ࡿ㸦౛࠼ࡤ㸪[1], [2], [3], [6], [12]㸧ࠋ ᮏㄽᩥ࡛ࡣ㸪ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢ୰࡛ࡶ௦⾲ⓗ࡞ 6 ✀㢮ࡢ࢔ࣝࢦࣜࢬ࣒࡟ὀ┠ࡋ㸪ྛ࢔ࣝࢦࣜࢬ࣒ ࡢᴫせࡢㄝ᫂࡜ࡑࡢC ゝㄒ࡟ࡼࡿ㛵ᩘᙧ㸦ࣉࣟࢢ࣒ࣛ㸧ࡸ࢔ࣝࢦࣜࢬ࣒ࡢ⌮ㄽⓗ࡞ᛶ㉁ࢆ♧ࡋ㸪ᐇ㝿 ࡟኱つᶍ࡞ᩘ್ࢹ࣮ࢱ࡟ᑐࡋ࡚㸪ࡇࢀࡽࡢࣉࣟࢢ࣒ࣛࢆᐇ⾜ࡉࡏࡿᩘ್ᐇ㦂࡟ࡼࡾྛᩚิ࢔ࣝࢦࣜࢬ ࣒ࡢຠ⋡ᛶࡢホ౯ࢆ⾜࠺ࠋᚋ༙࡛ࡣ㸪ᤄධࢯ࣮ࢺ࡜ࡇࢀࢆᨵⰋࡋࡓ஧ศ᥈⣴ᤄධࢯ࣮ࢺࢆᥦ᱌ࡋ㸪C ゝ ㄒ࡛ࡢࣉࣟࢢ࣒ࣛ໬ࡢ᪉ἲ㸪ᐇ㝿ࡢࢹ࣮ࢱࢆ⏝࠸ࡓᩘ್ᐇ㦂࡟ࡼࡿຠ⋡ᛶࡢホ౯ࢆ⾜࠺ࠋ ➨ 2 ⠇࡛ࡣ㸪࢔ࣝࢦࣜࢬ࣒ࡢホ౯᪉ἲ࡛࠶ࡿ᫬㛫ィ⟬㔞࡜ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢᏳᐃᛶ࡟ࡘ࠸࡚㏙࡭ ࡿࠋ➨3㸪4 ⠇࡛ࡣ㸪௦⾲ⓗ࡞ 6 ✀㢮ࡢᩚิ࢔ࣝࢦࣜࢬ࣒࡟ὀ┠ࡋ㸪ࡇࢀࡽࢆ᫬㛫ィ⟬㔞࡟ࡼࡾ 2 ࡘࡢ ࢢ࣮ࣝࣉ࡟ศ㢮ࡍࡿࠋࡑࡋ࡚㸪ྛᩚิ࢔ࣝࢦࣜࢬ࣒ࡢᴫせࡢㄝ᫂㸪C ゝㄒࢆ⏝࠸࡚ࡢࣉࣟࢢ࣒ࣛ໬㸪ᩘ ್ᐇ㦂࡟ࡼࡿຠ⋡ᛶࡢẚ㍑ホ౯ࢆ⾜࠺ࠋ➨5㸪6 ⠇࡛ࡣ㸪஧ศ᥈⣴ᤄධࢯ࣮ࢺࢆᥦ᱌ࡋ㸪ᩘ್ᐇ㦂࡟ࡼ ࡾ㸪ᚑ᮶ࡢᤄධࢯ࣮ࢺ࡜ࡢຠ⋡ᛶࡢẚ㍑ホ౯ࢆ⾜࠺ࠋ➨㸵⠇ࡣࡲ࡜ࡵ࡜௒ᚋࡢㄢ㢟࡛࠶ࡿࠋ

㸰 ᫬㛫ィ⟬㔞࡜ࢯ࣮ࢺࡢᏳᐃᛶ

㸰㸯㸬࢔ࣝࢦࣜࢬ࣒ࡢ᫬㛫ィ⟬㔞

࢔ࣝࢦࣜࢬ࣒ࡢィ⟬㔞ࡣಶࠎࡢၥ㢟౛࡟ࡼࡗ࡚ኚ໬ࡍࡿࡢ࡛㸪඲యⓗ࡞ホ౯ࢆ⾜࠺࡟ࡣၥ㢟౛ࡢつ ࣉࣟࢢ࣒ࣛ5-1 ࡟㛵ࡋ࡚ㄝ᫂ࡍࡿࠋࡲࡎ㸪4 ⾜┠࡛ malloc 㛵ᩘࢆ⏝࠸࡚ࢧ࢖ࢬ ݊ ൅ ͳ ࡢ㓄ิ  ࢆ⏕ ᡂࡍࡿࠋ5 ⾜┠࡛ push_heap 㛵ᩘࢆ࿧ࡧฟࡋ࡚ࣄ࣮ࣉࢆ⏕ᡂࡋ㸪6 ⾜┠࡟⛣ࡿࠋfor ᩥෆࡢ᮲௳ࢆ‶ࡓࡉ ࡞ࡃ࡞ࡿࡲ࡛7 ⾜┠ࢆᐇ⾜ࡉࡏࡿࠋ7 ⾜┠࡛ delete_maximum 㛵ᩘࢆ࿧ࡧฟࡋ㸪ሾ݅ሿ ࡟᰿ࡢ್ࢆ௦ධࡍ ࡿࠋࡲࡓ㸪push_heap 㛵ᩘࡣ௨ୗࡢࡼ࠺࡟࡞ࡿࠋ ࣉࣟࢢ࣒ࣛ5-2 push_heap 㛵ᩘ 1 2 3 4 5 6 7 8 9 10 11 12 // --- push_heap 㛵ᩘ --- // void push_heap(int D[], int T[], int n){

int k, i, size = 1; for(i = 0 ; i < n ; i++){ T[size] = D[i]; k = size; while((T[k] > T[k/2])&&(k > 1)){ swap(T[k], T[k/2]); k = k/2; } size++; } } ࣉࣟࢢ࣒ࣛ5-2 ࡟㛵ࡋ࡚㸪ࡲࡎ 4 ⾜┠ࡢ for ᩥෆࡢ᮲௳ࢆ‶ࡓࡉ࡞ࡃ࡞ࡿࡲ࡛ 5 ⾜┠࠿ࡽ 11 ⾜┠ࡲ ࡛ࢆᐇ⾜ࡉࡏࡿࠋ5 ⾜┠࡛ࢹ࣮ࢱࡢ㏣ຍ㸪6 ⾜┠࡛ῧᏐࡢึᮇ໬ࢆ⾜࠸㸪7 ⾜┠࡛ぶ࡜Ꮚࡢ್ࢆẚ㍑ࡍ ࡿࠋᏊ (ሾ݇ሿ) ൐ ぶ (ሾہ݇ ʹΤ ۂሿ) ࠿ࡘ ݇ ൐ ͳ ࡀᡂ❧ࡋ࡞ࡃ࡞ࡿࡲ࡛ 8 ⾜┠ࡢ஺᥮࣭ῧᏐࡢ᭦᪂ࢆ⾜࠺ࠋ while ᩥࢆᢤࡅࡓࡽ 10 ⾜┠࡛ ݏ݅ݖ݁ ࢆ࢖ࣥࢡ࣓ࣜࣥࢺࡋ㸪ࡇࢀࢆ⧞ࡾ㏉ࡍࡇ࡜࡛ࣄ࣮ࣉࢆసᡂࡍࡿࠋ ࣉࣟࢢ࣒ࣛ5-1 ࡢ 7 ⾜┠ࡢ delete_maximum 㛵ᩘࡣ௨ୗࡢࡼ࠺࡟࡞ࡿࠋ ࣉࣟࢢ࣒ࣛ5-3 delete_maximum 㛵ᩘ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 // --- delete_maximum 㛵ᩘ --- // int delete_maximum(int T[], int i){

int k, size, big; size = i; T[0] = T[1]; T[1] = T[size]; k = 1; while(2*k <= size){ if(2*k == size){ if(T[k] < T[2*k]){ swap(T[k], T[2*k]); k = 2*k; } else{ break; } } else{ if(T[2*k] < T[2*k+1]) { big = 2 * k + 1; } else{ big = 2 * k; } if(T[k] < T[big]){

swap(T[k], T[big]); k = big; } else{ break; } } } return T[0]; } ࣉࣟࢢ࣒ࣛ5-3 ࡟㛵ࡋ࡚ㄝ᫂ࡍࡿࠋ4㹼7 ⾜┠࡛᭱኱್ࡢྲྀࡾฟࡋ㸪್ࡢ⛣ືࢆ⾜࠸㸪8 ⾜┠࠿ࡽࣄ࣮ ࣉࡢ෌ᵓᡂࢆ⾜࠺ࠋ8 ⾜┠࡛Ꮚࡢ᭷↓ࢆ☜ㄆࡋ㸪Ꮚࡀ࠶ࡿሙྜࡣ 9 ⾜┠࡟⛣ືࡍࡿࠋ9 ⾜┠ࡢ if ᩥࡀᡂ

(10)

ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢẚ㍑ホ౯࡜஧ศ᥈⣴ᤄධࢯ࣮ࢺࡢᥦ᱌

Comparative Evaluation of Some Sorting Algorithms and Proposal of

Binary Search Insertion Sort

৿ਁ रҲ ʀߧ୫ ᴶߵ̏ʥ

Shuichi SHINMORI1)ː, Haruka ARATANI1)

 㮵ඣᓥ኱Ꮫ኱Ꮫ㝔⌮ᕤᏛ◊✲⛉ᩘ⌮᝟ሗ⛉Ꮫᑓᨷ

1)Graduate School of Science Engineering, Kagoshima University, Kagoshima 890-0065 * ੻೜ஸं e-mail address : [email protected]

Abstract: We studied on typical sorting algorithms, compared their efficiencies, and proposed and evaluated a new

sorting algorithm. In this paper, for some typical sorting algorithms, the principle and properties of each algorithm are described, and a function-type program in C language is given. Numerical experiments were used to compare and evaluate the execution times of the three O(n2) sorting algorithms (i.e., Bubble sort, Insertion sort and Selection sort)

and the three O(nlogn) sorting algorithms ( i.e., Quicksort, Heaposrt and Mergesort). Furthermore, we proposed a new algorithm called Binary search insertion sort, compared it with the conventional Insertion sort, and confirmed that it can sort at a high speed of about 68%.

Keywords: Sorting algorithm, Insertion sort, Quicksort, Data structure, Numerical experiments.

 ࡣࡌࡵ࡟

ᩚิ(sorting) ࡜ࡣ㸪඲㡰ᗎ㞟ྜࡢせ⣲࡛࠶ࡿ」ᩘࡢࢹ࣮ࢱࢆ㸪඲㡰ᗎࠕӌࠖ࡟ᚑࡗ࡚ᑠࡉ࠸ࢹ࣮ࢱ ࠿ࡽ኱ࡁ࠸ࢹ࣮ࢱ࡬࡜୪࡭᭰࠼ࡿࡇ࡜㸦᪼㡰࡜࠸࠺㸧㸪࠶ࡿ࠸ࡣ㏫࡟㸪኱ࡁ࠸ࢹ࣮ࢱ࠿ࡽᑠࡉ࠸ࢹ࣮ࢱ ࡟୪࡭᭰࠼ࡿࡇ࡜㸦㝆㡰࡜࠸࠺㸧ࢆ࠸࠸㸪ࢯ࣮ࢺ㸪ࢯ࣮ࢸ࢕ࣥࢢ࡜ࡶ࠸࠺ࠋࢡ࢖ࢵࢡࢯ࣮ࢺ㸪ࣂࣈࣝࢯ ࣮ࢺ㸪ᤄධࢯ࣮ࢺ࡞࡝㸪ᩚิࡢࡓࡵࡢᩘከࡃࡢ࢔ࣝࢦࣜࢬ࣒ࡀ◊✲ࡉࢀ࡚࠸ࡿࡀ㸪ࡇࢀࡽࢆ⥲⛠ࡋ࡚ᩚ ิ࢔ࣝࢦࣜࢬ࣒࡜࠸࠺㸦[14], [15]㸧ࠋᡃࠎࡢ㌟㏆࡞࡜ࡇࢁ࡟ࡣ㸪ᵝࠎ࡞ከࡃࡢࢹ࣮ࢱࡀᏑᅾࡍࡿࠋࡇࢀ ࡽࡢࢹ࣮ࢱࡢ୰࡟ࡣ㸪඲㡰ᗎӌ࡛㡰ᗎ௜ࡅࡽࢀࡿࡶࡢࡀᩘከࡃ࠶ࡿࠋ౛࠼ࡤ㸪ᩚᩘࡸᐇᩘࡢᩘ್ࢹ࣮ࢱ ࡛࠶ࢀࡤ኱ᑠ㡰㸪᪥ᮏㄒࡸⱥㄒࡢ༢ㄒ࡞࡝ࡢᩥᏐࢹ࣮ࢱ࡛࠶ࢀࡤ㸪஬༑㡢㡰ࡸ࢔ࣝࣇ࢓࣋ࢵࢺ㡰࡞࡝ ࡢ㎡᭩ᘧ㡰ᗎ࡛኱ᑠ㛵ಀࡀ୚࠼ࡽࢀࡿࠋࢹ࣮ࢱࢆຠ⋡Ⰻࡃᩚิࡉࡏࡿ᪉ἲࡣࢹ࣮ࢱࢆ᥈⣴ࡍࡿ᪉ἲ࡞ ࡝࡜୪ࢇ࡛ᇶᮏⓗ࡞ၥ㢟࡛࠶ࡾ㸪௨๓࠿ࡽከࡃࡢ◊✲ࡀ࡞ࡉࢀ࡚࠸ࡿ㸦౛࠼ࡤ㸪[1], [2], [3], [6], [12]㸧ࠋ ᮏㄽᩥ࡛ࡣ㸪ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢ୰࡛ࡶ௦⾲ⓗ࡞ 6 ✀㢮ࡢ࢔ࣝࢦࣜࢬ࣒࡟ὀ┠ࡋ㸪ྛ࢔ࣝࢦࣜࢬ࣒ ࡢᴫせࡢㄝ᫂࡜ࡑࡢC ゝㄒ࡟ࡼࡿ㛵ᩘᙧ㸦ࣉࣟࢢ࣒ࣛ㸧ࡸ࢔ࣝࢦࣜࢬ࣒ࡢ⌮ㄽⓗ࡞ᛶ㉁ࢆ♧ࡋ㸪ᐇ㝿 ࡟኱つᶍ࡞ᩘ್ࢹ࣮ࢱ࡟ᑐࡋ࡚㸪ࡇࢀࡽࡢࣉࣟࢢ࣒ࣛࢆᐇ⾜ࡉࡏࡿᩘ್ᐇ㦂࡟ࡼࡾྛᩚิ࢔ࣝࢦࣜࢬ ࣒ࡢຠ⋡ᛶࡢホ౯ࢆ⾜࠺ࠋᚋ༙࡛ࡣ㸪ᤄධࢯ࣮ࢺ࡜ࡇࢀࢆᨵⰋࡋࡓ஧ศ᥈⣴ᤄධࢯ࣮ࢺࢆᥦ᱌ࡋ㸪C ゝ ㄒ࡛ࡢࣉࣟࢢ࣒ࣛ໬ࡢ᪉ἲ㸪ᐇ㝿ࡢࢹ࣮ࢱࢆ⏝࠸ࡓᩘ್ᐇ㦂࡟ࡼࡿຠ⋡ᛶࡢホ౯ࢆ⾜࠺ࠋ ➨ 2 ⠇࡛ࡣ㸪࢔ࣝࢦࣜࢬ࣒ࡢホ౯᪉ἲ࡛࠶ࡿ᫬㛫ィ⟬㔞࡜ᩚิ࢔ࣝࢦࣜࢬ࣒ࡢᏳᐃᛶ࡟ࡘ࠸࡚㏙࡭ ࡿࠋ➨3㸪4 ⠇࡛ࡣ㸪௦⾲ⓗ࡞ 6 ✀㢮ࡢᩚิ࢔ࣝࢦࣜࢬ࣒࡟ὀ┠ࡋ㸪ࡇࢀࡽࢆ᫬㛫ィ⟬㔞࡟ࡼࡾ 2 ࡘࡢ ࢢ࣮ࣝࣉ࡟ศ㢮ࡍࡿࠋࡑࡋ࡚㸪ྛᩚิ࢔ࣝࢦࣜࢬ࣒ࡢᴫせࡢㄝ᫂㸪C ゝㄒࢆ⏝࠸࡚ࡢࣉࣟࢢ࣒ࣛ໬㸪ᩘ ್ᐇ㦂࡟ࡼࡿຠ⋡ᛶࡢẚ㍑ホ౯ࢆ⾜࠺ࠋ➨5㸪6 ⠇࡛ࡣ㸪஧ศ᥈⣴ᤄධࢯ࣮ࢺࢆᥦ᱌ࡋ㸪ᩘ್ᐇ㦂࡟ࡼ ࡾ㸪ᚑ᮶ࡢᤄධࢯ࣮ࢺ࡜ࡢຠ⋡ᛶࡢẚ㍑ホ౯ࢆ⾜࠺ࠋ➨㸵⠇ࡣࡲ࡜ࡵ࡜௒ᚋࡢㄢ㢟࡛࠶ࡿࠋ

㸰 ᫬㛫ィ⟬㔞࡜ࢯ࣮ࢺࡢᏳᐃᛶ

㸰㸯㸬࢔ࣝࢦࣜࢬ࣒ࡢ᫬㛫ィ⟬㔞

࢔ࣝࢦࣜࢬ࣒ࡢィ⟬㔞ࡣಶࠎࡢၥ㢟౛࡟ࡼࡗ࡚ኚ໬ࡍࡿࡢ࡛㸪඲యⓗ࡞ホ౯ࢆ⾜࠺࡟ࡣၥ㢟౛ࡢつ ࡾ❧ࡘሙྜ㸪ࡘࡲࡾ㸪Ꮚࡀ1 ࡘࡢሙྜࡣ 10㹼14 ⾜┠ࢆᐇ⾜ࡋ㸪஺᥮࣭௦ධ᧯సࡶࡋࡃࡣ 24 ⾜┠࡟⛣ ືࡍࡿࠋ9 ⾜┠ࡢ᮲௳ࡀᡂࡾ❧ࡓ࡞࠸ሙྜ㸪ࡘࡲࡾ㸪Ꮚࡀ 2 ࡘࡢሙྜࡣ 16㹼21 ⾜┠ࢆᐇ⾜ࡋ㸪஺᥮࣭ ௦ධ᧯సࡶࡋࡃࡣ24 ⾜┠࡟⛣ືࡍࡿࠋ8㹼23 ⾜┠ࢆ⧞ࡾ㏉ࡍࡇ࡜࡟ࡼࡾࣄ࣮ࣉࡀ෌ᵓᡂࡉࢀࡓࡽ㸪24 ⾜┠ࢆᐇ⾜ࡋ ሾͲሿ ࡢ್ࢆ Heapsort 㛵ᩘࡢ 7 ⾜┠࡟㏉ࡍࠋ ࣄ࣮ࣉࢯ࣮ࢺࡢ᭱ᝏ᫬㛫ィ⟬㔞࡟ࡘ࠸࡚⪃࠼ࡿࠋࡲࡎ㸪Heapsort 㛵ᩘࡣ୺࡟ 1 ᅇࡢ push_heap 㛵ᩘࡢ ࿧ࡧฟࡋ࡜ ݊ ᅇࡢ delete_maximum 㛵ᩘࡢ࿧ࡧฟࡋ࡛ᵓᡂࡉࢀ࡚࠸ࡿࠋpush_heap 㛵ᩘࡣ㸪for ᩥ࡟ࡼ ࡿ⧞ࡾ㏉ࡋᅇᩘࡀ ݊ ᅇ㸪for ᩥࡢኚᩘ ݅ ࡟౫Ꮡࡋ࡚࠸ࡿ while ᩥࡢ⧞ࡾ㏉ࡋᅇᩘࡣ㸪݇ ൌ ݅ ࡜ࡍࡿ࡜᭱ Ž‘‰݇ ᅇ࡛࠶ࡿࠋఱᨾ࡞ࡽ㸪while ᩥࡣⴥ࡟᱁⣡ࡉࢀࡓ್ࢆ᭱ᝏࡢሙྜࡣ᰿ࡲ࡛⛣ືࡉࡏࡿࡶࡢ࡞ ࡢ࡛㸪ᮌࡢࣞ࣋ࣝࡀwhile ᩥࡢ⧞ࡾ㏉ࡋᅇᩘ࡟࡞ࡿ࠿ࡽ࡛࠶ࡿࠋࡲࡓ㸪delete_maximum 㛵ᩘࡣ heapsort 㛵ᩘࡢfor ᩥࡢኚᩘ ݅ ࡟౫Ꮡࡋ࡚࠸ࡿࠋ݇ ൌ ݅ ࡜ࡍࡿ࡜㸪while ᩥࡢ⧞ࡾ㏉ࡋᅇᩘࡣ᭱኱ Ž‘‰݇ ᅇ࡛࠶ ࡿࠋࡇࢀࡣ㸪push_heap 㛵ᩘ࡜ྠᵝ࡛㸪while ᩥࡣ᰿࡟⛣ືࡋࡓ್ࢆ᭱ᝏࡢሙྜࡣⴥࡲ࡛⛣ືࡉࡏࡿࡶ ࡢ࡞ࡢ࡛㸪ᮌࡢࣞ࣋ࣝࡀwhile ᩥࡢ⧞ࡾ㏉ࡋᅇᩘ࡟࡞ࡿ࠿ࡽ࡛࠶ࡿࠋ ௨ୖࡼࡾ㸪ࣄ࣮ࣉࢯ࣮ࢺࡢ᭱ᝏ᫬㛫ィ⟬㔞ࡣḟࡢࡼ࠺࡟࡞ࡾ㸪ド᫂ࡣ┬␎ࡍࡿࡀ㸪᭱Ⰻ᫬㛫ィ⟬㔞ࡶ ߍሺ݊ Ž‘‰ ݊ሻ ࡛࠶ࡿࡇ࡜ࡀศ࠿ࡗ࡚࠸ࡿࠋ ෍൫ߍሺͳሻ ൈ ሺŽ‘‰ଶ݇ ൅ Ž‘‰ଶ݇ሻ൯ ௡ିଵ ௞ୀଵ ൌ ߍሺͳሻ ൈ ʹ ෍ Ž‘‰ଶ݇ ௡ିଵ ௞ୀଵ ൑ ߍሺͳሻ ൈ ʹሺ݊ െ ͳሻ Ž‘‰ଶሺ݊ െ ͳሻ ൌ ߍሺ݊ Ž‘‰ ݊ሻ ᭱ᚋ࡟㸪ࣄ࣮ࣉࢯ࣮ࢺࡢᏳᐃᛶ࡟ࡘ࠸࡚⪃࠼ࡿࠋࣉࣟࢢ࣒ࣛࡢືస࠿ࡽศ࠿ࡿࡼ࠺࡟㸪ࣄ࣮ࣉࡣධຊ ࡢ㡰ᗎࢆ⪃៖ࡏࡎ࡟್ࡢ஺᥮ࢆ⾜࠺ࠋࡑࡢࡓࡵ㸪୍⯡ⓗ࡟ࣄ࣮ࣉࢯ࣮ࢺࡣᏳᐃᛶࡢ᮲௳ࢆ‶ࡓࡋ࡚࠸ ࡞࠸ࡢ࡛㸪Ᏻᐃ࡞ࢯ࣮ࢺ࡟ࡍࡿࡓࡵ࡟ࡣᕤኵࡀᚲせ࡜࡞ࡿࠋ (3) ࣐࣮ࢪࢯ࣮ࢺ ࣐࣮ࢪࢯ࣮ࢺ(Merge sort)࡜ࡣ㸪ධຊࡋࡓࢹ࣮ࢱࢆせ⣲ࡀ 1 ࡘ࡟࡞ࡿࡲ࡛ศ๭ࡋ㸪ࡑࡢᚋ㸪࣐࣮ࢪ㸦ే ྜ㸧࡜࠸࠺᧯స࡛ࢯ࣮ࢺࡋ࡞ࡀࡽ⤌ࡳྜࢃࡏࡿࡇ࡜࡟ࡼࡗ࡚ࢯ࣮ࢺࢆ᏶ᡂࡉࡏࡿ᪉ἲ࡛࠶ࡿࠋ ࡲࡎ㸪࣐࣮ࢪ࡜ࡣ㸪ࢯ࣮ࢺ῭ࡳࡢ2 ࡘࡢࢹ࣮ࢱิࢆ 1 ࡘࡢࢯ࣮ࢺࡉࢀࡓࢹ࣮ࢱิ࡟ేྜࡍࡿࡇ࡜࡛ ࠶ࡿࠋ࣐࣮ࢪࢯ࣮ࢺࡣ㸪௨ୗࡢ2 ࡘࡢ㛵ᩘࢆ⏝࠸ࡿࡇ࡜࡛ࢯ࣮ࢺࡀ᏶஢ࡍࡿࠋ (a) Mergesort 㛵ᩘ㸦ศ๭㸧 ධຊࡋࡓࢹ࣮ࢱࢆ㸪ൌ ൛݀ǡ ݀ǡ ڮ ǡ ݀௡ ଶΤ ିଵൟ㸪ൌ ൛݀௡ ଶΤ ǡ ݀௡ ଶΤ ାଵǡ ڮ ǡ ݀௡ିଵൟ ࡜࠸࠺ 2 ࡘࡢ㞟ྜ ࡟ศ๭ࡍࡿࠋせ⣲ࡀ1 ࡘ࡟࡞ࡿࡲ࡛෌ᖐ㛵ᩘࢆ⏝࠸࡚⧞ࡾ㏉ࡍࠋ (b) merge 㛵ᩘ 㞄ྠኈࡢ㞟ྜ  ࡜㞟ྜ  ࢆ࣐࣮ࢪࡍࡿࠋࡇࡢ᧯సࢆ㞟ྜࡀ 1 ࡘ࡟࡞ࡿࡲ࡛⧞ࡾ㏉ࡍࠋ ୖࡢศ๭ࡍࡿMergesort 㛵ᩘࢆࣉࣟࢢ࣒࡛ࣛグ㏙ࡍࡿ࡜௨ୗࡢࡼ࠺࡟࡞ࡿࠋ ࣉࣟࢢ࣒ࣛ6-1 Mergesort 㛵ᩘ 1 2 3 4 5 6 7 8 9 // --- Mergesort --- // void Mergesort(int D[], int left, int right){

int mid;

if(left >= right) return; mid = (left + right) / 2;

if(left<mid) Mergesort(D, left, mid);

if(mid+1<right) Mergesort(D, mid + 1, right); merge(D, left, mid, right);

}

ࣉࣟࢢ࣒ࣛ6-1 ࡟ࡘ࠸࡚ㄝ᫂ࡍࡿࠋ4 ⾜┠࡛㓄ิ  ࡟ྵࡲࢀࡿせ⣲ࡢᩘࢆ☜ㄆࡋ㸪せ⣲ᩘࡀ 2 ࡘ௨ ୖࡢሙྜ5 ⾜┠࡟㐍ࡴࠋ5 ⾜┠࡛ ݉݅݀ ࡢ್ࢆィ⟬ࡋ㸪6࣭7 ⾜┠࡛㓄ิࢆ 2 ࡘ࡟ศࡅࡿࠋࡇࡢ᧯సࢆ⧞

参照

関連したドキュメント

Proof of Theorem 2: The Push-and-Pull algorithm consists of the Initialization phase to generate an initial tableau that contains some basic variables, followed by the Push and

13 proposed a hybrid multiobjective evolutionary algorithm HMOEA that incorporates various heuristics for local exploitation in the evolutionary search and the concept of

Under certain assumptions on the sequence (P N ) N≥0 (which still allow for the standard probabilis- tic models of algorithms associated with binary search trees, digital search

If information about a suitable drawing (that is, the location of its vertices) of a graph is given, our results allow the computation of SSSP in O(sort (E)) I/Os on graphs

Using right instead of left singular vectors for these examples leads to the same number of blocks in the first example, although of different size and, hence, with a different

In order to demonstrate that the CAB algorithm provides a better performance, it has been compared to other optimization approaches such as metaheuristic algorithms Section 4.2

For performance comparison of PSO-based hybrid search algorithm, that is, PSO and noising-method-based local search, using proposed encoding/decoding technique with those reported

In the special case of a Boolean algebra, the resulting SJB is orthogonal with respect to the standard inner product and, moreover, we can write down an explicit formula for the