Enviar pesquisa
Carregar
アルゴリズムのお勉強 マージソート 試験対策
•
0 gostou
•
748 visualizações
H
hixi365
Seguir
試験対策用 足りなかった部分 マージソート 立ち絵素材 臼井の会様
Leia menos
Leia mais
Educação
Denunciar
Compartilhar
Denunciar
Compartilhar
1 de 53
Recomendados
第五回
第五回
evesanta1224
アルゴリズムのお勉強 アルゴリズムとデータ構造 [素数・文字列探索・簡単なソート]
アルゴリズムのお勉強 アルゴリズムとデータ構造 [素数・文字列探索・簡単なソート]
hixi365
アルゴリズムのお勉強 ダイクストラ
アルゴリズムのお勉強 ダイクストラ
hixi365
シェーダー伝道師 第一回
シェーダー伝道師 第一回
hixi365
upload test slide
upload test slide
hixi365
Kancolle
Kancolle
givemegohan
角錐や円錐が、角柱や円柱の体積の3分の1であることを積分・極限抜きで証明してみる
角錐や円錐が、角柱や円柱の体積の3分の1であることを積分・極限抜きで証明してみる
Hiro H.
その文字列検索、std::string::findだけで大丈夫ですか?【Sapporo.cpp 第8回勉強会(2014.12.27)】
その文字列検索、std::string::findだけで大丈夫ですか?【Sapporo.cpp 第8回勉強会(2014.12.27)】
Hiro H.
Recomendados
第五回
第五回
evesanta1224
アルゴリズムのお勉強 アルゴリズムとデータ構造 [素数・文字列探索・簡単なソート]
アルゴリズムのお勉強 アルゴリズムとデータ構造 [素数・文字列探索・簡単なソート]
hixi365
アルゴリズムのお勉強 ダイクストラ
アルゴリズムのお勉強 ダイクストラ
hixi365
シェーダー伝道師 第一回
シェーダー伝道師 第一回
hixi365
upload test slide
upload test slide
hixi365
Kancolle
Kancolle
givemegohan
角錐や円錐が、角柱や円柱の体積の3分の1であることを積分・極限抜きで証明してみる
角錐や円錐が、角柱や円柱の体積の3分の1であることを積分・極限抜きで証明してみる
Hiro H.
その文字列検索、std::string::findだけで大丈夫ですか?【Sapporo.cpp 第8回勉強会(2014.12.27)】
その文字列検索、std::string::findだけで大丈夫ですか?【Sapporo.cpp 第8回勉強会(2014.12.27)】
Hiro H.
My Inspire High Award 2024 「孤独は敵なのか?」
My Inspire High Award 2024 「孤独は敵なのか?」
inspirehighstaff03
My Inspire High Award 2024「老いることは不幸なこと?」
My Inspire High Award 2024「老いることは不幸なこと?」
inspirehighstaff03
【ゲーム理論入門】ChatGPTが作成した ゲーム理論の問題を解く #3 Slide
【ゲーム理論入門】ChatGPTが作成した ゲーム理論の問題を解く #3 Slide
ssusere0a682
Establishment and operation of medical corporations.pdf
Establishment and operation of medical corporations.pdf
oganekyokoi
My Inspire High Award2024「外国人が日本のテーブルマナーに驚く理由は?」
My Inspire High Award2024「外国人が日本のテーブルマナーに驚く理由は?」
inspirehighstaff03
3年前期 交通基盤工学 第一回 ガイダンス 交通基盤工学の概要 パワーポイント
3年前期 交通基盤工学 第一回 ガイダンス 交通基盤工学の概要 パワーポイント
shu1108hina1020
My Inspire High Award 2024「なぜ、好きなことにいつかは飽きるの」
My Inspire High Award 2024「なぜ、好きなことにいつかは飽きるの」
inspirehighstaff03
My Inspire High Award 2024「他者と自分、対立を防ぐには?」
My Inspire High Award 2024「他者と自分、対立を防ぐには?」
inspirehighstaff03
My Inspire High Award 2024「スーパーマーケットで回収されたキャベツ外葉は廃棄されているの?」
My Inspire High Award 2024「スーパーマーケットで回収されたキャベツ外葉は廃棄されているの?」
inspirehighstaff03
Divorce agreements in administrative work.pdf
Divorce agreements in administrative work.pdf
oganekyokoi
My Inspire High Award 2024「なぜ議会への関心が低いのか?」
My Inspire High Award 2024「なぜ議会への関心が低いのか?」
inspirehighstaff03
My Inspire High Award 2024 「本当の『悪者』って何?」
My Inspire High Award 2024 「本当の『悪者』って何?」
inspirehighstaff03
My Inspire High Award 2024 「家族とは何か」
My Inspire High Award 2024 「家族とは何か」
inspirehighstaff03
My Inspire High Award 2024 「AIと仲良くなるには?」
My Inspire High Award 2024 「AIと仲良くなるには?」
inspirehighstaff03
International Politics I - Lecture 1
International Politics I - Lecture 1
Toru Oga
What I did before opening my business..pdf
What I did before opening my business..pdf
oganekyokoi
My Inspire High Award 2024「なぜ人は他人と違うところがあってもそれをなかなか誇れないのか?」
My Inspire High Award 2024「なぜ人は他人と違うところがあってもそれをなかなか誇れないのか?」
inspirehighstaff03
My Inspire High Award 2024「Yakushima Islandってなんか変じゃない?」.pdf
My Inspire High Award 2024「Yakushima Islandってなんか変じゃない?」.pdf
inspirehighstaff03
My Inspire High Award 2024 「正義って存在するの?」
My Inspire High Award 2024 「正義って存在するの?」
inspirehighstaff03
My Inspire High Award 2024「世の中の流行はどのようにして生まれるのか」
My Inspire High Award 2024「世の中の流行はどのようにして生まれるのか」
inspirehighstaff03
2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
Mais conteúdo relacionado
Último
My Inspire High Award 2024 「孤独は敵なのか?」
My Inspire High Award 2024 「孤独は敵なのか?」
inspirehighstaff03
My Inspire High Award 2024「老いることは不幸なこと?」
My Inspire High Award 2024「老いることは不幸なこと?」
inspirehighstaff03
【ゲーム理論入門】ChatGPTが作成した ゲーム理論の問題を解く #3 Slide
【ゲーム理論入門】ChatGPTが作成した ゲーム理論の問題を解く #3 Slide
ssusere0a682
Establishment and operation of medical corporations.pdf
Establishment and operation of medical corporations.pdf
oganekyokoi
My Inspire High Award2024「外国人が日本のテーブルマナーに驚く理由は?」
My Inspire High Award2024「外国人が日本のテーブルマナーに驚く理由は?」
inspirehighstaff03
3年前期 交通基盤工学 第一回 ガイダンス 交通基盤工学の概要 パワーポイント
3年前期 交通基盤工学 第一回 ガイダンス 交通基盤工学の概要 パワーポイント
shu1108hina1020
My Inspire High Award 2024「なぜ、好きなことにいつかは飽きるの」
My Inspire High Award 2024「なぜ、好きなことにいつかは飽きるの」
inspirehighstaff03
My Inspire High Award 2024「他者と自分、対立を防ぐには?」
My Inspire High Award 2024「他者と自分、対立を防ぐには?」
inspirehighstaff03
My Inspire High Award 2024「スーパーマーケットで回収されたキャベツ外葉は廃棄されているの?」
My Inspire High Award 2024「スーパーマーケットで回収されたキャベツ外葉は廃棄されているの?」
inspirehighstaff03
Divorce agreements in administrative work.pdf
Divorce agreements in administrative work.pdf
oganekyokoi
My Inspire High Award 2024「なぜ議会への関心が低いのか?」
My Inspire High Award 2024「なぜ議会への関心が低いのか?」
inspirehighstaff03
My Inspire High Award 2024 「本当の『悪者』って何?」
My Inspire High Award 2024 「本当の『悪者』って何?」
inspirehighstaff03
My Inspire High Award 2024 「家族とは何か」
My Inspire High Award 2024 「家族とは何か」
inspirehighstaff03
My Inspire High Award 2024 「AIと仲良くなるには?」
My Inspire High Award 2024 「AIと仲良くなるには?」
inspirehighstaff03
International Politics I - Lecture 1
International Politics I - Lecture 1
Toru Oga
What I did before opening my business..pdf
What I did before opening my business..pdf
oganekyokoi
My Inspire High Award 2024「なぜ人は他人と違うところがあってもそれをなかなか誇れないのか?」
My Inspire High Award 2024「なぜ人は他人と違うところがあってもそれをなかなか誇れないのか?」
inspirehighstaff03
My Inspire High Award 2024「Yakushima Islandってなんか変じゃない?」.pdf
My Inspire High Award 2024「Yakushima Islandってなんか変じゃない?」.pdf
inspirehighstaff03
My Inspire High Award 2024 「正義って存在するの?」
My Inspire High Award 2024 「正義って存在するの?」
inspirehighstaff03
My Inspire High Award 2024「世の中の流行はどのようにして生まれるのか」
My Inspire High Award 2024「世の中の流行はどのようにして生まれるのか」
inspirehighstaff03
Último
(20)
My Inspire High Award 2024 「孤独は敵なのか?」
My Inspire High Award 2024 「孤独は敵なのか?」
My Inspire High Award 2024「老いることは不幸なこと?」
My Inspire High Award 2024「老いることは不幸なこと?」
【ゲーム理論入門】ChatGPTが作成した ゲーム理論の問題を解く #3 Slide
【ゲーム理論入門】ChatGPTが作成した ゲーム理論の問題を解く #3 Slide
Establishment and operation of medical corporations.pdf
Establishment and operation of medical corporations.pdf
My Inspire High Award2024「外国人が日本のテーブルマナーに驚く理由は?」
My Inspire High Award2024「外国人が日本のテーブルマナーに驚く理由は?」
3年前期 交通基盤工学 第一回 ガイダンス 交通基盤工学の概要 パワーポイント
3年前期 交通基盤工学 第一回 ガイダンス 交通基盤工学の概要 パワーポイント
My Inspire High Award 2024「なぜ、好きなことにいつかは飽きるの」
My Inspire High Award 2024「なぜ、好きなことにいつかは飽きるの」
My Inspire High Award 2024「他者と自分、対立を防ぐには?」
My Inspire High Award 2024「他者と自分、対立を防ぐには?」
My Inspire High Award 2024「スーパーマーケットで回収されたキャベツ外葉は廃棄されているの?」
My Inspire High Award 2024「スーパーマーケットで回収されたキャベツ外葉は廃棄されているの?」
Divorce agreements in administrative work.pdf
Divorce agreements in administrative work.pdf
My Inspire High Award 2024「なぜ議会への関心が低いのか?」
My Inspire High Award 2024「なぜ議会への関心が低いのか?」
My Inspire High Award 2024 「本当の『悪者』って何?」
My Inspire High Award 2024 「本当の『悪者』って何?」
My Inspire High Award 2024 「家族とは何か」
My Inspire High Award 2024 「家族とは何か」
My Inspire High Award 2024 「AIと仲良くなるには?」
My Inspire High Award 2024 「AIと仲良くなるには?」
International Politics I - Lecture 1
International Politics I - Lecture 1
What I did before opening my business..pdf
What I did before opening my business..pdf
My Inspire High Award 2024「なぜ人は他人と違うところがあってもそれをなかなか誇れないのか?」
My Inspire High Award 2024「なぜ人は他人と違うところがあってもそれをなかなか誇れないのか?」
My Inspire High Award 2024「Yakushima Islandってなんか変じゃない?」.pdf
My Inspire High Award 2024「Yakushima Islandってなんか変じゃない?」.pdf
My Inspire High Award 2024 「正義って存在するの?」
My Inspire High Award 2024 「正義って存在するの?」
My Inspire High Award 2024「世の中の流行はどのようにして生まれるのか」
My Inspire High Award 2024「世の中の流行はどのようにして生まれるのか」
Destaque
2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
Skeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
Introduction to Data Science
Introduction to Data Science
Christy Abraham Joy
Time Management & Productivity - Best Practices
Time Management & Productivity - Best Practices
Vit Horky
The six step guide to practical project management
The six step guide to practical project management
MindGenius
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
Destaque
(20)
2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
Skeleton Culture Code
Skeleton Culture Code
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Getting into the tech field. what next
Getting into the tech field. what next
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
How to have difficult conversations
How to have difficult conversations
Introduction to Data Science
Introduction to Data Science
Time Management & Productivity - Best Practices
Time Management & Productivity - Best Practices
The six step guide to practical project management
The six step guide to practical project management
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
アルゴリズムのお勉強 マージソート 試験対策
1.
アルゴリズムのお勉強 兼 前期中間対策 マージソート分
2.
※注意書き • 確実にテスト対策になる確証はありません • あくまでお勉強用です •
過去の経験やノートを参考にしています • あと変なノリが含まれます • あと、責任は取りません…というか取れません それでもいいなら進めていってください。
3.
マージソート • ソートアルゴリズムの一種 • 分割統治法
ということで…マージソートも テスト範囲だったので追加説明です。
4.
マージソート • ソートアルゴリズムの一種 • 分割統治法
マージソートもクイックソート同様に 分割統治法の一種です。
5.
マージソート • ソートアルゴリズムの一種 • 分割統治法
クイックソートは要素を 小さいものと大きいものに分けてましたが
6.
マージソート • ソートアルゴリズムの一種 • 分割統治法
マージソートは単純に半分に分けます。 そのあと連結させるときに秘密があります。
7.
マージソート 入力 : 整数列A(A1,
A2, …, An) 出力 : 入力列を昇順に整列した列B • 0 データが1つならば終了する; • 1 列のn/2番目までをP1, それ以降をP2とする; • 2 P1とP2をそれぞれ独立に整列する; • 3 for i ← 1 to n do • 4 | if head(P1) < head(P2) • 5 | | Bi ← head(P1); • 6 | | head(P1)をP1から除去する; • 7 | end • 8 | else • 9 | | Bi ← head(P2); • 10 | | head(P2)をP2から除去する; • 11 | end • 12 end • 13 BiのすべてをAiに入れる それでは早速。 こちらがマージソートの疑似言語です。
8.
マージソート 入力 : 整数列A(A1,
A2, …, An) 出力 : 入力列を昇順に整列した列B • 0 データが1つならば終了する; • 1 列のn/2番目までをP1, それ以降をP2とする; • 2 P1とP2をそれぞれ独立に整列する; • 3 for i ← 1 to n do • 4 | if head(P1) < head(P2) • 5 | | Bi ← head(P1); • 6 | | head(P1)をP1から除去する; • 7 | end • 8 | else • 9 | | Bi ← head(P2); • 10 | | head(P2)をP2から除去する; • 11 | end • 12 end • 13 BiのすべてをAiに入れる 入力、出力ともに配列ですね。
9.
マージソート 入力 : 整数列A(A1,
A2, …, An) 出力 : 入力列を昇順に整列した列B • 0 データが1つならば終了する; • 1 列のn/2番目までをP1, それ以降をP2とする; • 2 P1とP2をそれぞれ独立に整列する; • 3 for i ← 1 to n do • 4 | if head(P1) < head(P2) • 5 | | Bi ← head(P1); • 6 | | head(P1)をP1から除去する; • 7 | end • 8 | else • 9 | | Bi ← head(P2); • 10 | | head(P2)をP2から除去する; • 11 | end • 12 end • 13 BiのすべてをAiに入れる マージソートも分割統治法なので、 ここが再帰的に呼び出しを行う部分です。
10.
マージソート 入力 : 整数列A(A1,
A2, …, An) 出力 : 入力列を昇順に整列した列B • 0 データが1つならば終了する; • 1 列のn/2番目までをP1, それ以降をP2とする; • 2 P1とP2をそれぞれ独立に整列する; • 3 for i ← 1 to n do • 4 | if head(P1) < head(P2) • 5 | | Bi ← head(P1); • 6 | | head(P1)をP1から除去する; • 7 | end • 8 | else • 9 | | Bi ← head(P2); • 10 | | head(P2)をP2から除去する; • 11 | end • 12 end • 13 BiのすべてをAiに入れる その終了条件はこちらになりますね。
11.
マージソート 入力 : 整数列A(A1,
A2, …, An) 出力 : 入力列を昇順に整列した列B • 0 データが1つならば終了する; • 1 列のn/2番目までをP1, それ以降をP2とする; • 2 P1とP2をそれぞれ独立に整列する; • 3 for i ← 1 to n do • 4 | if head(P1) < head(P2) • 5 | | Bi ← head(P1); • 6 | | head(P1)をP1から除去する; • 7 | end • 8 | else • 9 | | Bi ← head(P2); • 10 | | head(P2)をP2から除去する; • 11 | end • 12 end • 13 BiのすべてをAiに入れる P1とP2は配列の前か、後ろかだけなので、 P2の中にP1の要素より小さい要素が 存在する可能性があります。
12.
マージソート 入力 : 整数列A(A1,
A2, …, An) 出力 : 入力列を昇順に整列した列B • 0 データが1つならば終了する; • 1 列のn/2番目までをP1, それ以降をP2とする; • 2 P1とP2をそれぞれ独立に整列する; • 3 for i ← 1 to n do • 4 | if head(P1) < head(P2) • 5 | | Bi ← head(P1); • 6 | | head(P1)をP1から除去する; • 7 | end • 8 | else • 9 | | Bi ← head(P2); • 10 | | head(P2)をP2から除去する; • 11 | end • 12 end • 13 BiのすべてをAiに入れる その比較を行っているのがこちらになります。
13.
マージソート 入力 : 整数列A(A1,
A2, …, An) 出力 : 入力列を昇順に整列した列B • 0 データが1つならば終了する; • 1 列のn/2番目までをP1, それ以降をP2とする; • 2 P1とP2をそれぞれ独立に整列する; • 3 for i ← 1 to n do • 4 | if head(P1) < head(P2) • 5 | | Bi ← head(P1); • 6 | | head(P1)をP1から除去する; • 7 | end • 8 | else • 9 | | Bi ← head(P2); • 10 | | head(P2)をP2から除去する; • 11 | end • 12 end • 13 BiのすべてをAiに入れる まず、前提としてこの行により P1の中身、P2の中身は それぞれで整列されています。
14.
マージソート 入力 : 整数列A(A1,
A2, …, An) 出力 : 入力列を昇順に整列した列B • 0 データが1つならば終了する; • 1 列のn/2番目までをP1, それ以降をP2とする; • 2 P1とP2をそれぞれ独立に整列する; • 3 for i ← 1 to n do • 4 | if head(P1) < head(P2) • 5 | | Bi ← head(P1); • 6 | | head(P1)をP1から除去する; • 7 | end • 8 | else • 9 | | Bi ← head(P2); • 10 | | head(P2)をP2から除去する; • 11 | end • 12 end • 13 BiのすべてをAiに入れる 繰り返し回数は全体の要素の数ですね。
15.
マージソート 入力 : 整数列A(A1,
A2, …, An) 出力 : 入力列を昇順に整列した列B • 0 データが1つならば終了する; • 1 列のn/2番目までをP1, それ以降をP2とする; • 2 P1とP2をそれぞれ独立に整列する; • 3 for i ← 1 to n do • 4 | if head(P1) < head(P2) • 5 | | Bi ← head(P1); • 6 | | head(P1)をP1から除去する; • 7 | end • 8 | else • 9 | | Bi ← head(P2); • 10 | | head(P2)をP2から除去する; • 11 | end • 12 end • 13 BiのすべてをAiに入れる このheadというのはそれぞれの配列の先頭 の要素を返す関数です。 つまり配列の最小値を返します。
16.
マージソート 入力 : 整数列A(A1,
A2, …, An) 出力 : 入力列を昇順に整列した列B • 0 データが1つならば終了する; • 1 列のn/2番目までをP1, それ以降をP2とする; • 2 P1とP2をそれぞれ独立に整列する; • 3 for i ← 1 to n do • 4 | if head(P1) < head(P2) • 5 | | Bi ← head(P1); • 6 | | head(P1)をP1から除去する; • 7 | end • 8 | else • 9 | | Bi ← head(P2); • 10 | | head(P2)をP2から除去する; • 11 | end • 12 end • 13 BiのすべてをAiに入れる つまり一番小さいもの同士を 比較することになりますね。
17.
マージソート 入力 : 整数列A(A1,
A2, …, An) 出力 : 入力列を昇順に整列した列B • 0 データが1つならば終了する; • 1 列のn/2番目までをP1, それ以降をP2とする; • 2 P1とP2をそれぞれ独立に整列する; • 3 for i ← 1 to n do • 4 | if head(P1) < head(P2) • 5 | | Bi ← head(P1); • 6 | | head(P1)をP1から除去する; • 7 | end • 8 | else • 9 | | Bi ← head(P2); • 10 | | head(P2)をP2から除去する; • 11 | end • 12 end • 13 BiのすべてをAiに入れる といことは、すべての要素で 一番小さい要素を求めることができます。
18.
マージソート 入力 : 整数列A(A1,
A2, …, An) 出力 : 入力列を昇順に整列した列B • 0 データが1つならば終了する; • 1 列のn/2番目までをP1, それ以降をP2とする; • 2 P1とP2をそれぞれ独立に整列する; • 3 for i ← 1 to n do • 4 | if head(P1) < head(P2) • 5 | | Bi ← head(P1); • 6 | | head(P1)をP1から除去する; • 7 | end • 8 | else • 9 | | Bi ← head(P2); • 10 | | head(P2)をP2から除去する; • 11 | end • 12 end • 13 BiのすべてをAiに入れる 小さかった方を、答えの配列に追加して…
19.
マージソート 入力 : 整数列A(A1,
A2, …, An) 出力 : 入力列を昇順に整列した列B • 0 データが1つならば終了する; • 1 列のn/2番目までをP1, それ以降をP2とする; • 2 P1とP2をそれぞれ独立に整列する; • 3 for i ← 1 to n do • 4 | if head(P1) < head(P2) • 5 | | Bi ← head(P1); • 6 | | head(P1)をP1から除去する; • 7 | end • 8 | else • 9 | | Bi ← head(P2); • 10 | | head(P2)をP2から除去する; • 11 | end • 12 end • 13 BiのすべてをAiに入れる その要素は除去、 つまり吐き出したことになります。
20.
マージソート 入力 : 整数列A(A1,
A2, …, An) 出力 : 入力列を昇順に整列した列B • 0 データが1つならば終了する; • 1 列のn/2番目までをP1, それ以降をP2とする; • 2 P1とP2をそれぞれ独立に整列する; • 3 for i ← 1 to n do • 4 | if head(P1) < head(P2) • 5 | | Bi ← head(P1); • 6 | | head(P1)をP1から除去する; • 7 | end • 8 | else • 9 | | Bi ← head(P2); • 10 | | head(P2)をP2から除去する; • 11 | end • 12 end • 13 BiのすべてをAiに入れる 次の比較では、先ほどの一番小さい要素を 除いて行われます。 つまり2番目に小さい要素を求めます。
21.
マージソート 入力 : 整数列A(A1,
A2, …, An) 出力 : 入力列を昇順に整列した列B • 0 データが1つならば終了する; • 1 列のn/2番目までをP1, それ以降をP2とする; • 2 P1とP2をそれぞれ独立に整列する; • 3 for i ← 1 to n do • 4 | if head(P1) < head(P2) • 5 | | Bi ← head(P1); • 6 | | head(P1)をP1から除去する; • 7 | end • 8 | else • 9 | | Bi ← head(P2); • 10 | | head(P2)をP2から除去する; • 11 | end • 12 end • 13 BiのすべてをAiに入れる あとはこれを繰り返していけば… 最終的にすべての要素を 並べることができますね。
22.
マージソート 入力 : 整数列A(A1,
A2, …, An) 出力 : 入力列を昇順に整列した列B • 0 データが1つならば終了する; • 1 列のn/2番目までをP1, それ以降をP2とする; • 2 P1とP2をそれぞれ独立に整列する; • 3 for i ← 1 to n do • 4 | if head(P1) < head(P2) • 5 | | Bi ← head(P1); • 6 | | head(P1)をP1から除去する; • 7 | end • 8 | else • 9 | | Bi ← head(P2); • 10 | | head(P2)をP2から除去する; • 11 | end • 12 end • 13 BiのすべてをAiに入れる 最後に、出力列を入力列に戻します。
23.
マージソート head関数について説明します。 head関数は配列要素の 先頭の値を返します。
24.
マージソート ここで、要素が空だった場合は∞を返します。 これにより同じ処理で、比較できるわけです。
25.
マージソート それでは、 マージソートの動作を確認してみましょう。 5134 2
26.
マージソート ノートの方は2分割の都合上 要素数が8ですが… 要素数5でやってみましょう。 5134 2
27.
マージソート 疑似言語より、要素数が5なので 5/2=2 添字<2をP1としましょう。 5134 2 列のn/2番目までをP1, それ以降をP2とする;
28.
マージソート 8だった場合は 8/2=4 添字<4でちょうど半分ですね。 5134 2 列のn/2番目までをP1, それ以降をP2とする;
29.
マージソート まず、添え字が2未満をP1 それ以降をP2とします。 5134 2 134 5
2
30.
マージソート 左側では要素数2より2/2=1、 添字<1をP1、 それ以降をP2としますね。 5134 2 134 5
2 4 3
31.
マージソート 右側は要素数3より3/2=1、 添字<1をP1、 それ以降をP2とします。 5134 2 134 5
2 1 5 24 3
32.
マージソート 右側はさらに分割できるのでこうなりますね。 5134 2 134 5
2 1 5 24 3 5 2
33.
マージソート まず5と2から計算していきましょう。 5134 2 134 5
2 1 5 24 3 5 2
34.
マージソート 5<2で2を追加します。 5 2
35.
マージソート こうなりますね。 ここで2を除去します。 2 5 2
36.
マージソート P2は空集合なのでheadは∞になります。 つまり5<∞の比較で5が入ります。 2 5
37.
マージソート これで、一回分の併合(マージ)が 完了しました。 2 5
38.
マージソート どんどん行きましょう。 1と2の比較ですね。 1 2 5
39.
マージソート 左は空なので∞となります。 2が入って…1 2 5
40.
マージソート 5も∞と比較されて… 1 2 5
41.
マージソート こうなりました。 1 2 5
42.
マージソート 同様に4と3も併合されます。 143 2 5
43.
マージソート では、最後もやってみましょう。 143 2 5
44.
マージソート 3と1で1ですね。 143 2 5
45.
マージソート 3と2で2ですね。 1 43 2 5
46.
マージソート 3と5で3ですね。 21 43 5
47.
マージソート 4と5で4ですね。 321 4 5
48.
マージソート 最後は5ですね。 4321 5
49.
マージソート できました。 これがマージソートです。 4321 5
50.
以上 以上簡単ですが、 マージソートとなります。
51.
教科書 • アルゴリズムとデータ構造 数理工学社
藤田 聡 2013/3/10 教科書のため、ところどころ引用になってる場所があります
52.
立ち絵素材 • 臼井の会様 http://usui.moo.jp/frame2.html
53.
ご視聴ありがとうございました。 頑張ってくださいね。