blog/content/post/2008-05-10-00000916.md

216 lines
16 KiB
Markdown
Raw Normal View History

2019-03-31 11:00:21 +00:00
---
title: 研修であつかったアルゴリズムを黙々と書くよ
author: kazu634
date: 2008-05-10
url: /2008/05/10/_986/
wordtwit_post_info:
- 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:3999;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- C
- Programming
---
<div class="section">
<p>
 随時追加していく。
</p>
<h4>
二分探索
</h4>
<pre class="syntax-highlight">
<span class="synPreProc">#include </span><span class="synConstant">&#60;stdio.h&#62;</span>
<span class="synType">int</span> main(<span class="synType">void</span>);
<span class="synType">int</span> bin_search(<span class="synType">const</span> <span class="synType">int</span> a[], <span class="synType">int</span> num_a, <span class="synType">int</span> key);
<span class="synType">int</span> bin_search(<span class="synType">const</span> <span class="synType">int</span> a[], <span class="synType">int</span> num_a, <span class="synType">int</span> key)
{
<span class="synType">int</span> pof = <span class="synConstant">1</span>; <span class="synComment">/* 二分探索の始点(Position of First) */</span>
<span class="synType">int</span> pol = num_a - <span class="synConstant">1</span>; <span class="synComment">/* 二分探索の終点(Position of Last) */</span>
<span class="synType">int</span> poc = (pof + pol) / <span class="synConstant">2</span>; <span class="synComment">/* 二分探索の中央(Position of Center)*/</span>
<span class="synType">int</span> p = -<span class="synConstant">1</span>; <span class="synComment">/* keyが発見された要素の添え字 */</span>
<span class="synStatement">while</span> (p == -<span class="synConstant">1</span> &#38;&#38; pof &#60;= pol) {
<span class="synComment">/* 中央の値がkeyと一致した */</span>
<span class="synStatement">if</span> (a[poc] == key) {
p = poc;
<span class="synStatement">return</span>(p);
} <span class="synStatement">else</span> <span class="synStatement">if</span> (a[poc] &#60; key) {
<span class="synComment">/* 中央の値よりもkeyの値の方が大きい */</span>
pof = poc + <span class="synConstant">1</span>;
poc = (pof + pol) / <span class="synConstant">2</span>;
} <span class="synStatement">else</span> <span class="synStatement">if</span> (key &#60; a[poc]) {
<span class="synComment">/* keyの値よりも中央の値の方が大きい*/</span>
pol = poc - <span class="synConstant">1</span>;
poc = (pof + pol) / <span class="synConstant">2</span>;
}
}
<span class="synStatement">return</span>(p);
}
<span class="synType">int</span> main(<span class="synType">void</span>)
{
<span class="synType">int</span> array[<span class="synConstant">8</span>] = {<span class="synConstant"></span>, <span class="synConstant">10</span>, <span class="synConstant">20</span>, <span class="synConstant">30</span>, <span class="synConstant">40</span>, <span class="synConstant">50</span>, <span class="synConstant">60</span>, <span class="synConstant">70</span>};
<span class="synType">int</span> num_array = <span class="synStatement">sizeof</span>(array) / <span class="synStatement">sizeof</span>(array[<span class="synConstant"></span>]);
printf (<span class="synConstant">&#34;</span><span class="synSpecial">%d\n</span><span class="synConstant">&#34;</span>, bin_search(array, num_array, <span class="synConstant">100</span>));
<span class="synStatement">return</span>(<span class="synConstant"></span>);
}
</pre>
<h4>
基本選択法
</h4>
<p>
 基本選択法は整列対象のデータの中から一番大きいデータを選び出して、整列対象の最後のデータと交換する。そして整列対象の最後のデータをのぞいて、もう一度同じ処理を行う。この走査を整列対象のデータ数が残り一個になるまで繰り返して行うことによって、値が大きなデータを次第に右端の方に移していく方法である。
</p>
<pre class="syntax-highlight">
<span class="synPreProc">#include </span><span class="synConstant">&#60;stdio.h&#62;</span>
<span class="synType">int</span> main(<span class="synType">void</span>);
<span class="synComment">/* sortだとすでに使われてそうだから適当に名前付け */</span>
<span class="synType">void</span> m_sort(<span class="synType">int</span> a[], <span class="synType">const</span> <span class="synType">int</span> num_a);
<span class="synType">void</span> prt_int_array(<span class="synType">const</span> <span class="synType">int</span> a[], <span class="synType">int</span> num_a);
<span class="synType">void</span> prt_int_array(<span class="synType">const</span> <span class="synType">int</span> a[], <span class="synType">int</span> num_a)
{
<span class="synType">int</span> i;
<span class="synStatement">for</span> (i = <span class="synConstant"></span>;i &#60; num_a;i++) {
printf (<span class="synConstant">&#34;</span><span class="synSpecial">%d</span><span class="synConstant">, &#34;</span>, a[i]);
}
printf (<span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
}
<span class="synType">void</span> m_sort(<span class="synType">int</span> a[], <span class="synType">const</span> <span class="synType">int</span> num_a)
{
<span class="synType">int</span> curwork = num_a - <span class="synConstant">1</span>; <span class="synComment">/* 確定させたい配列の添え字 */</span>
<span class="synType">int</span> max_a_num; <span class="synComment">/* 未確定の要素の中で最大値の要素の添え字 */</span>
<span class="synType">int</span> i, tmp;
<span class="synComment">/* 確定させたい配列の要素数が 2 になるまで */</span>
<span class="synStatement">while</span> (<span class="synConstant"></span> &#60; curwork) {
prt_int_array(a, num_a);
<span class="synComment">/* とりあえず確定させたい配列の添え字を最大値と仮定 */</span>
max_a_num = curwork;
printf (<span class="synConstant">&#34;確定させたい要素の添え字: </span><span class="synSpecial">%d\n</span><span class="synConstant">&#34;</span>, curwork);
printf (<span class="synConstant">&#34;0... </span><span class="synSpecial">%d</span><span class="synConstant"> までの範囲を探索するよ。</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>, curwork - <span class="synConstant">1</span>);
<span class="synComment">/* i を「確定させたい要素の添え字 - 1」までまわす */</span>
<span class="synStatement">for</span> (i = <span class="synConstant"></span>;i &#60; curwork;i++) {
<span class="synComment">/* それまでの最大値よりもa[i]が大きければ */</span>
<span class="synStatement">if</span> (a[max_a_num] &#60; a[i]) {
printf (<span class="synConstant">&#34;a[</span><span class="synSpecial">%d</span><span class="synConstant">] &#60; a[</span><span class="synSpecial">%i</span><span class="synConstant">]</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>, max_a_num, i);
<span class="synComment">/* 最大値の要素の添え字を max_a_num に代入 */</span>
max_a_num = i;
}
}
printf (<span class="synConstant">&#34;Change </span><span class="synSpecial">%d</span><span class="synConstant">(a[</span><span class="synSpecial">%d</span><span class="synConstant">]) --&#62; </span><span class="synSpecial">%d</span><span class="synConstant">(a[</span><span class="synSpecial">%d</span><span class="synConstant">])</span><span class="synSpecial">\n\n</span><span class="synConstant">&#34;</span>, a[max_a_num], max_a_num, a[curwork], curwork);
<span class="synComment">/* 最大値を a[curwork] に代入 */</span>
tmp = a[curwork];
a[curwork] = a[max_a_num];
a[max_a_num] = tmp;
curwork--;
}
}
<span class="synType">int</span> main(<span class="synType">void</span>)
{
<span class="synType">int</span> array[<span class="synConstant">8</span>] = {<span class="synConstant">100</span>, <span class="synConstant">95</span>, <span class="synConstant">900</span>, <span class="synConstant">85</span>, <span class="synConstant">9980</span>, <span class="synConstant">795</span>, <span class="synConstant">70</span>, <span class="synConstant">695</span>};
<span class="synType">int</span> num_array = <span class="synStatement">sizeof</span>(array) / <span class="synStatement">sizeof</span>(array[<span class="synConstant"></span>]);
m_sort(array, num_array);
prt_int_array(array, num_array);
<span class="synStatement">return</span>(<span class="synConstant"></span>);
}
</pre>
<h4>
バブルソート
</h4>
<pre class="syntax-highlight">
<span class="synPreProc">#include </span><span class="synConstant">&#60;stdio.h&#62;</span>
<span class="synType">int</span> main(<span class="synType">void</span>);
<span class="synType">void</span> bubble_sort(<span class="synType">int</span> a[], <span class="synType">const</span> <span class="synType">int</span> num_a);
<span class="synType">void</span> prt_int_array(<span class="synType">const</span> <span class="synType">int</span> a[], <span class="synType">int</span> num_a);
<span class="synType">void</span> prt_int_array(<span class="synType">const</span> <span class="synType">int</span> a[], <span class="synType">int</span> num_a)
{
<span class="synType">int</span> i;
<span class="synStatement">for</span> (i = <span class="synConstant"></span>;i &#60; num_a;i++) {
printf (<span class="synConstant">&#34;</span><span class="synSpecial">%d</span><span class="synConstant">, &#34;</span>, a[i]);
}
printf (<span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
}
<span class="synType">void</span> bubble_sort(<span class="synType">int</span> a[], <span class="synType">const</span> <span class="synType">int</span> num_a)
{
<span class="synType">int</span> curwork = num_a - <span class="synConstant">1</span>; <span class="synComment">/* 確定させたい配列の添え字 */</span>
<span class="synType">int</span> i, tmp;
<span class="synComment">/* 確定させたい配列の要素数が 2 になるまで */</span>
<span class="synStatement">while</span> (<span class="synConstant"></span> &#60; curwork) {
prt_int_array(a, num_a);
<span class="synComment">/* curwork - 1までずっとiの値を変化させる */</span>
<span class="synStatement">for</span> (i = <span class="synConstant"></span>;i &#60; curwork;i++) {
<span class="synComment">/* a[i] よりも a[i+1] が小さければ交換 */</span>
<span class="synStatement">if</span> (a[i+<span class="synConstant">1</span>] &#60; a[i]) {
tmp = a[i];
a[i] = a[i+<span class="synConstant">1</span>];
a[i+<span class="synConstant">1</span>] = tmp;
}
}
curwork--;
}
}
<span class="synType">int</span> main(<span class="synType">void</span>)
{
<span class="synType">int</span> array[<span class="synConstant">8</span>] = {<span class="synConstant">100</span>, <span class="synConstant">95</span>, <span class="synConstant">900</span>, <span class="synConstant">85</span>, <span class="synConstant">9980</span>, <span class="synConstant">795</span>, <span class="synConstant">70</span>, <span class="synConstant">695</span>};
<span class="synType">int</span> num_array = <span class="synStatement">sizeof</span>(array) / <span class="synStatement">sizeof</span>(array[<span class="synConstant"></span>]);
bubble_sort(array, num_array);
prt_int_array(array, num_array);
<span class="synStatement">return</span>(<span class="synConstant"></span>);
}
</pre>
<h4>
基本挿入法
</h4>
<pre class="syntax-highlight">
<span class="synPreProc">#include </span><span class="synConstant">&#60;stdio.h&#62;</span>
<span class="synType">int</span> main(<span class="synType">void</span>);
<span class="synType">void</span> m__sort(<span class="synType">int</span> a[], <span class="synType">const</span> <span class="synType">int</span> num_a);
<span class="synType">void</span> prt_int_array(<span class="synType">const</span> <span class="synType">int</span> a[], <span class="synType">int</span> num_a);
<span class="synType">void</span> prt_int_array(<span class="synType">const</span> <span class="synType">int</span> a[], <span class="synType">int</span> num_a)
{
<span class="synType">int</span> i;
<span class="synStatement">for</span> (i = <span class="synConstant"></span>;i &#60; num_a;i++) {
printf (<span class="synConstant">&#34;</span><span class="synSpecial">%d</span><span class="synConstant">, &#34;</span>, a[i]);
}
printf (<span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
}
<span class="synType">void</span> m_sort(<span class="synType">int</span> a[], <span class="synType">const</span> <span class="synType">int</span> num_a)
{
<span class="synType">int</span> arranged = <span class="synConstant"></span>; <span class="synComment">/* 整列済みの最終要素の添え字 */</span>
<span class="synType">int</span> curwork = <span class="synConstant">1</span>; <span class="synComment">/* 追加データ要素の添え字 */</span>
<span class="synType">int</span> i, tmp;
<span class="synComment">/* 最後まで整列するまで */</span>
<span class="synStatement">while</span> (arranged &#60; num_a - <span class="synConstant">1</span>) {
printf (<span class="synConstant">&#34;</span><span class="synSpecial">%d</span><span class="synConstant"> まで整列済み。 a[</span><span class="synSpecial">%d</span><span class="synConstant">] を追加データと考えるよ。</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>, arranged, curwork);
printf (<span class="synConstant">&#34;</span><span class="synSpecial">%d</span><span class="synConstant"> ... </span><span class="synSpecial">%d</span><span class="synConstant"> までを </span><span class="synSpecial">%d</span><span class="synConstant"> と比較するよ!</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>, <span class="synConstant"></span>, arranged, a[curwork]);
prt_int_array(a, num_a);
printf (<span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
<span class="synComment">/* 追加データまで走査する */</span>
<span class="synStatement">for</span> (i = curwork;<span class="synConstant"></span> &#60; i;i--) {
<span class="synComment">/* もし右側の要素が左側よりも小さければ交換する */</span>
<span class="synStatement">if</span> (a[i] &#60; a[i-<span class="synConstant">1</span>]) {
tmp = a[i-<span class="synConstant">1</span>];
a[i-<span class="synConstant">1</span>] = a[i];
a[i] = tmp;
}
}
<span class="synComment">/* 整列済みの添え字と追加データの添え字をインクリメント */</span>
arranged++;
curwork++;
}
}
<span class="synType">int</span> main(<span class="synType">void</span>)
{
<span class="synType">int</span> array[<span class="synConstant">8</span>] = {<span class="synConstant">100</span>, <span class="synConstant">95</span>, <span class="synConstant">900</span>, <span class="synConstant">85</span>, <span class="synConstant">9980</span>, <span class="synConstant">795</span>, <span class="synConstant">70</span>, <span class="synConstant">695</span>};
<span class="synType">int</span> num_array = <span class="synStatement">sizeof</span>(array) / <span class="synStatement">sizeof</span>(array[<span class="synConstant"></span>]);
m_sort(array, num_array);
prt_int_array(array, num_array);
<span class="synStatement">return</span>(<span class="synConstant"></span>);
}
</pre>
</div>