解析エンジニアの自動化 blog

コツコツと自動化した方法を残す blog

C# の同じ値がある List で BinarySearch するとどうなるのか



こんにちは。
仕事の自動化にやりがいと達成感を感じるガッくんです。


この記事の目次



背景・目的


C# の List で BinarySearch を使っていて同じ値があった時はどんな動作をするのかなと思って実験してみました。

ちなみに、 BinarySearch は配列や List の値を検索して Index を返す関数です。



動作環境


Windows 7
Visual Studio 2017



プログラム

異なる要素の List で2パターンの実験を行いました。

実験ソースコード


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace How_to_use_List_BinarySearch
{
    class Program
    {
        static void Main(string[] args)
        {
            // List を用意する
            List<int> test = new List<int>();
 
            // List に値を格納する
            for (int i = 1; i <= 10; i++)
            {
                if (i == 7)
                {
                    test.Add((i - 1) * (i - 1));
                }
                else
                {
                    test.Add(i * i);
                }
            }
 
            // List の出力
            Console.WriteLine(" List の中身");
            Console.WriteLine(string.Format("{0,15}", "Index") + "," + string.Format("{0,15}", "List Value"));
            for (int i = 0; i < test.Count; i++)
            {
                Console.WriteLine(string.Format("{0,15}", i) + "," + string.Format("{0,15}", test[i]));
            }
 
            // 36 の値が入っている List の Index を バイナリサーチ で調べる
            int ret = test.BinarySearch(36);
 
            Console.WriteLine("");
            Console.WriteLine(" List Value = 36 の Index = " + ret);
 
            // 何かキーが押されるまで待つ
            Console.ReadKey();
        }
    }
}


実験ソースコード


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace How_to_use_List_BinarySearch
{
    class Program
    {
        static void Main(string[] args)
        {
            // List を用意する
            List<int> test = new List<int>();
 
            // List に値を格納する
            for (int i = 1; i <= 10; i++)
            {
                if (i == 5)
                {
                    test.Add((i - 1) * (i - 1));
                }
                else
                {
                    test.Add(i * i);
                }
            }
 
            // List の出力
            Console.WriteLine(" List の中身");
            Console.WriteLine(string.Format("{0,15}", "Index") + "," + string.Format("{0,15}", "List Value"));
            for (int i = 0; i < test.Count; i++)
            {
                Console.WriteLine(string.Format("{0,15}", i) + "," + string.Format("{0,15}", test[i]));
            }
 
            // 16 の値が入っている List の Index を バイナリサーチ で調べる
            int ret = test.BinarySearch(16);
 
            Console.WriteLine("");
            Console.WriteLine(" List Value = 16 の Index = " + ret);
 
            // 何かキーが押されるまで待つ
            Console.ReadKey();
        }
    }
}



結果

BinarySearch 関数は同じ値の要素があっても Index がちゃんと返ってきます。

しかし、図1 、図2 にある通り、1つしか Index が返ってこないので、同じ値の要素が何個あるのか、他の要素の Index も分かりません。


図1 実験ソースコード①検索結果



図2 実験ソースコード②検索結果



コメント

BinarySearch を使えば、高速な検索が可能です。

重複する値がある場合は、どの要素けは分からないけど、高速な検索が可能みたいです。



以上